TransWikia.com

Mi arreglo borra los valores tras ser llenado

Stack Overflow en español Asked by Marcelo Avendaño on November 17, 2021

Mi código sirve para reservar los asientos de un vuelo determinado y que luego otros asientos sean llenados todo esta en una clase que selecciona los asientos y luego funciona con otra clase que selecciona los vuelos. Lo que hace la clase de asientos es generar un array para cada columna de asientos llenarlos de letras ‘E’ y tras los input del usuario llenar el determinado asiento con una ‘R’.

Se guarda el valor en el array

Este proceso funciona bien para el primer input sin embargo cuando el usuario quiere llenar otro asiento se pierden los valores almacenados en el array.
El valor se pierde

    void seat_initializer (){

        static bool initialized;
        if (!initialized ){
            initialized = true;
            for (int i = 0; i <= 4; ++i) {
                column_A_NYCT13[i] = 'E';
                column_B_NYCT13[i] = 'E';
                column_C_NYCT13[i] = 'E';
                column_D_NYCT13[i] = 'E';
            }
        }
    }
    void seat_printer(){

        cout << "x AB | CD " << endl;
        cout << "0 " << column_A_NYCT13[0] << column_B_NYCT13[0] << " | " << column_C_NYCT13[0] << column_D_NYCT13[0] << endl;
        cout << "1 " << column_A_NYCT13[1] << column_B_NYCT13[1] << " | " << column_C_NYCT13[1] << column_D_NYCT13[1] << endl;
        cout << "2 " << column_A_NYCT13[2] << column_B_NYCT13[2] << " | " << column_C_NYCT13[2] << column_D_NYCT13[2] << endl;
        cout << "3 " << column_A_NYCT13[3] << column_B_NYCT13[3] << " | " << column_C_NYCT13[3] << column_D_NYCT13[3] << endl;
        cout << "4 " << column_A_NYCT13[4] << column_B_NYCT13[4] << " | " << column_C_NYCT13[4] << column_D_NYCT13[4] << endl;

    }
    void instruction_for_seats(){
        cout << "Now please see the order of the seats. nThe columns A and D represents the window seats. nThe columns B and C represents the hall seats.nThe seats with the letter E represents the seats that are empty.n"<< endl;
        seat_printer();
        cout <<endl;
        cout << "Please choose the correspondent letter for the column you want the seat for the flight NYCT13"<< endl;
    }

    void set_seats_window(char control_window) {
        int chosen_seat;
        if (control_window == 'A' || control_window == 'a') {
            for (int return_to_number = 1;;) {
                cout << "Please enter the number of the seat you want" << endl;
                cin >> chosen_seat;
                if (chosen_seat <= 4) {
                    cout << "You have chosen the seat A" << chosen_seat << "nAre you sure you want to reserve this seat?nType A to accept or C to cancell." << endl;
                    cin >> seat_confirmation_NYCT13;
                    if (seat_confirmation_NYCT13 == 'A' || seat_confirmation_NYCT13 == 'a'){
                        cout <<"Your selection was saved."<<endl;
                        column_A_NYCT13[chosen_seat] = 'R';
                        break;
                    }
                    if (seat_confirmation_NYCT13 == 'C' || seat_confirmation_NYCT13 == 'c'){
                        cout << "Your selection was cancelled"<<endl;
                    }
                }else {
                    cout << "Invalid entry.a"<<endl;
                    continue;
                }
            }
        }
        if (control_window == 'D' || control_window == 'd') {
            for (int return_to_number = 1;;) {
                cout << "Please enter the number of the seat you want" << endl;
                cin >> chosen_seat;
                if (chosen_seat <= 4) {
                    cout << "You have chosen the seat A" << chosen_seat << "nAre you sure you want to reserve this seat?nType A to accept or C to cancell." << endl;
                    cin >> seat_confirmation_NYCT13;
                    if (seat_confirmation_NYCT13 == 'A' || seat_confirmation_NYCT13 == 'a'){
                        cout <<"Your selection was saved."<<endl;
                        column_D_NYCT13[chosen_seat] = 'R';
                        break;
                    }
                    if (seat_confirmation_NYCT13 == 'C' || seat_confirmation_NYCT13 == 'c'){
                        cout << "Your selection was cancelled"<<endl;
                    }
                }else {
                    cout << "Invalid entry.a"<<endl;
                    continue;
                }
            }
        }

    }
    void set_seats_hall(char control_hall){
        int chosen_seat;
        if (control_hall == 'B' || control_hall == 'b') {
            for (int return_to_number = 1;;) {
                cout << "Please enter the number of the seat you want" << endl;
                cin >> chosen_seat;
                column_B_NYCT13[chosen_seat] = 'R';
                if (chosen_seat <= 4) {
                    cout << "You have chosen the seat B" << chosen_seat << "nAre you sure you want to reserve this seat?nType A to accept or C to cancell."<< endl;
                    cin >> seat_confirmation_NYCT13;
                    if (seat_confirmation_NYCT13 == 'A' || seat_confirmation_NYCT13 == 'a') {
                        cout << "Your selection was saved for the reservation." <<endl;
                        break;
                    }
                    if (seat_confirmation_NYCT13 == 'C' || seat_confirmation_NYCT13 == 'c') {
                        cout << "Your selection was cancelled.";
                    }
                } else {
                    cout << "Invalid entry. a";
                    continue;
                }
            }
        }
        if (control_hall == 'C' || control_hall == 'c') {
            for (int return_to_number = 1;;) {
                cout << "Please enter the number of the seat you want" << endl;
                cin >> chosen_seat;
                column_C_NYCT13[chosen_seat] = 'R';
                if (chosen_seat <= 4) {
                    cout << "You have chosen the seat B" << chosen_seat << "nAre you sure you want to reserve this seat?nType A to accept or C to cancell."<< endl;
                    cin >> seat_confirmation_NYCT13;
                    if (seat_confirmation_NYCT13 == 'A' || seat_confirmation_NYCT13 == 'a') {
                        cout << "Your selection was saved for the reservation." <<endl;
                        break;
                    }
                    if (seat_confirmation_NYCT13 == 'C' || seat_confirmation_NYCT13 == 'c') {
                        cout << "Your selection was cancelled.";
                    }
                } else {
                    cout << "Invalid entry. a";
                    continue;
                }
            }
        }
    }





private:
    char column_A_NYCT13 [4];
    char column_B_NYCT13 [4];
    char column_C_NYCT13 [4];
    char column_D_NYCT13 [4];
    char seat_confirmation_NYCT13;

One Answer

No presentas un ejemplo mínimo, luego es complicado decirte todo lo que tienes mal... pero si declaras un array tal que

char column_A_NYCT13 [4];

entonces dicho array será accesible en los índices (0,3)... y tu estás accediendo a column_A_NYCT13[4] en múltiples ocasiones:

for (int i = 0; i <= 4; ++i) {
        column_A_NYCT13[i] = 'E';

cout << "4 " << column_A_NYCT13[4] << column_B_NYCT13[4] << " | " << column_C_NYCT13[4] << column_D_NYCT13[4] << endl;

// ...

Con eso solo consigues escribir fuera de los límites del array y modificar la memoria de otras variables... lo raro es que tu programa no se vuelva loco del todo.

Si necesitas que el array tenga 5 posiciones, cambia el 4 por el 5 en la declaración:

char column_A_NYCT13 [5];

De esta forma tu código dejará de sobreescribir memoria de otras variables.

Si, en cambio, resulta que el ejercicio te pide que gestiones 4 asientos, entonces tendrás que dejar de escribir en la posición 4 del array. Por ejemplo:

for (int i = 0; i < 4; ++i)
{
//              ~~~~~ El bucle ahora itera en el rango (0, 3)

    column_A_NYCT13[i] = 'E';

Answered by eferion on November 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP