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.