1

I'm having some troubles in my exercise with C++ language: The exercise consists of: Have a bus with 55 sits for travelers, I have to be able to add, edit, and remove travelers from this bus.

So far my code works but I don't know why when I add or show the variable are not stored in my array properly.

#include <iostream>
using namespace std;

//Clase que representa un objeto de un viejero
class Viajero {
  public:
    string nombre;
};

//declaración de las funciones
void anadirViajero();
void mostraViajeros();

//declaración de las variables globales
static Viajero autobus[55];

int main() {

    anadirViajero();
    mostraViajeros();

    return 0;
}

void anadirViajero(){

    string nombre;
    bool check = false;

    for(Viajero viajero : autobus){
        if(viajero.nombre == "" && check == false){
            cout<<"Nombre Viajero: ";
            cin>>nombre;
            viajero.nombre = nombre;
            check = true;
        }
    }
}

void mostraViajeros(){
    int count = 0;
    for(Viajero viajero : autobus){
        count++;
        cout<<"Nombre: ";
        cout<<viajero.nombre;
    } 

    cout<<"Total de Viajeros: "<<count;
}

I'm not sure if my global variable is a correct approach to solve this exercise. I tried to put static in the declaration but still not storing the values.

  • 3
    Try to use english names, so (mostly) everyone can understand it – RoQuOTriX Dec 03 '19 at 15:11
  • 6
    The bug is here: `for(Viajero viajero : autobus){` in this range based for loop `viajero` is a copy of the element. You need to use a reference like this: `for(Viajer& viajero : autobus){` I would post this as an answer however I am sure there are duplicates for this. I have seen at least 1 similar question this week. – drescherjm Dec 03 '19 at 15:11
  • @drescherjm: If you turn that comment into an answer, it can be marked as the correct answer. – MSalters Dec 03 '19 at 15:13
  • 2
    global variables are (almost) never the answer – 463035818_is_not_an_ai Dec 03 '19 at 15:14
  • @drescherjm thanks! That solved the problem for me. – Gabriel Vendramini Dec 03 '19 at 15:22
  • Related: [https://stackoverflow.com/questions/51387535/c-range-based-for-loop-is-the-container-copied](https://stackoverflow.com/questions/51387535/c-range-based-for-loop-is-the-container-copied) also [https://stackoverflow.com/questions/15176104/c11-range-based-loop-get-item-by-value-or-reference-to-const](https://stackoverflow.com/questions/15176104/c11-range-based-loop-get-item-by-value-or-reference-to-const) – drescherjm Dec 03 '19 at 15:22

2 Answers2

2

this here:

if(viajero.nombre == "" && check == false){
            cout<<"Nombre Viajero: ";
            cin>>nombre;
            viajero.nombre = nombre;
            check = true;
        }

is incorrect , as soon as you add 1 user, you set the flag check to true, and this is blocking the condition to ad a new viajero

if a viajero has a state like checked or not then that should be defined in the class

like:

class Viajero {
  public:
    string nombre;
    bool check;

};
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

As well as the fact that you'll only ever read in one name (as pointed out by ΦXocę 웃 Пepeúpa ツ), you also have another problem: your line viajero.nombre = nombre; sets the name for a Vialero object that it entirely local to the for loop and never assign anything to the members of your array.

To fix this, make the 'iterator' variable a reference to the array, as follows (note the & symbol after the Viajero):

    for (Viajero& viajero : autobus) { // viajero MUST be a REFERENCE if you want to change the array element(s)
        if (viajero.nombre == "" && check == false) {
            cout << "Nombre Viajero: ";
            cin >> nombre;
            viajero.nombre = nombre;
        //  check = true; // If you leave this line in, only the first name will get read!
        }
    }
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83