Hi, im new to c++ and im coming across a problem where i am trying to add a class to an array, here is my code, its a bit mombo jombo because i had to remove some parts of the code but whats there is me trying to add an object to an array
so now the code:
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
string name;
void addPassenger();
void printPassenger();
Person();
Person& Person::operator = (Person &passenger1);
};
Person passenger;
void Person::addPassenger()
{
woop sorry i had removed a bit of code that made it work, in the first while loop its meant to be
{
passenger = new Person;
passengerDatabase[i].addPassenger();
}
int main()
{
string repeat;
int i = 0;
int j;
Person passengerDatabase[10];
do
{
cout << "Enter the number to execute the following: \n"
<< "- Press 0 to add another Passenger\n"
<< "- Press 1 to display most recent added Passenger\n"
<< "- Press 2 to view a Passenger on the allocated flight\n"
<< "- Press 3 to delete most recent Passenger\n"
<< "- Press 4 to delete a Passenger\n"
<< "- Press 5 to exit program\n\n"
<< "Number: ";
cin >> j;
switch (j)
{
case 0:
passengerDatabase[i].addPassenger(passenger);
i++;
break;
case 1:
passengerDatabase[i].printPassenger(passenger);
break;
case 2:
for (int j = 0; j < i; j++)
passengerDatabase[j].printPassenger(passenger);
break;
case 3:
break;
case 4:
break;
case 5:
exit(1);
break;
default:
cout << "Please enter an appropriate value!";
}
cout << "Would you like to repeat previous options? (Y/n)";
cin >> repeat;
if (repeat.compare("n") != 1 || repeat.compare("N") != 1)
{
exit(1);
}
cout << endl;
}
while(repeat.compare("n") != 0 || repeat.compare("N") != 0);
system("pause");
return 0;
}
i think it could be something to do with the iteration of my program but im a bit confused on how to get around to fixing it, the program adds an object, then if i add another, the first object that was input is replaced by the second object input, any ideas? :/
int main()
{
string repeat;
int i = 0;
int j;
Person passengerDatabase[10];
do
{
cout << "Enter the number to execute the following: \n"
<< "- Press 0 to add another Passenger\n"
<< "- Press 1 to display most recent added Passenger\n"
<< "- Press 2 to view a Passenger on the allocated flight\n"
<< "- Press 3 to delete most recent Passenger\n"
<< "- Press 4 to delete a Passenger\n"
<< "- Press 5 to exit program\n\n"
<< "Number: ";
cin >> j;
switch (j)
{
case 0:
passengerDatabase[i].addPassenger(passenger);
i++;
break;
case 1:
passengerDatabase[i].printPassenger(passenger);
break;
case 2:
for (int j = 0; j < i; j++)
passengerDatabase[j].printPassenger(passenger);
break;
case 3:
break;
case 4:
break;
case 5:
exit(1);
break;
default:
cout << "Please enter an appropriate value!";
}
cout << "Would you like to repeat previous options? (Y/n)";
cin >> repeat;
if (repeat.compare("n") != 1 || repeat.compare("N") != 1)
{
exit(1);
}
cout << endl;
}
while(repeat.compare("n") != 0 || repeat.compare("N") != 0);
system("pause");
return 0;
}
When you add an object it doesnt look like i changes so you keep adding to first array element.
You need to iterate through the array for an unused location before adding.
Sorry I was worng about the index. It looks like your not really adding the passenger to the array.
Your add function changes the values of the global passanger object but I dont think your adding it.
Change
1 2 3 4 5
case 0:
passengerDatabase[i].addPassenger(passenger);
i++;
break;
to
1 2 3 4 5 6
case 0:
passengerDatabase[i].addPassenger(passenger);
passengerDatabase[i] = passenger;
i++;
break;
But you still have issues with the print function.
naraku you were right, i had replaced a different case print message (silly me) but now i finally managed to make it work :) i owe you many thanks naraku and everyone else thank you so much :)
just if anyone is curious how the code works here it is:
class Person
{
public:
Person();
string forename;
string surname;
string passportNum;
int depDay;
int depMonth;
int depYear;
int arrDay;
int arrMonth;
int arrYear;
string seatRow;
int seatColumn;
string flightNumber;
double price;
string passengerClass;
void addPassenger(Person &passenger);
void printPassenger(Person passenger);
};