Hi guys, i really need help with my program so much that i had to register just to ask you guys for any advice.
So basically, my program lets the user input passengers (along with their name and number of baggage) into the plane. the plane can only hold up to 10 passengers. Then, if the user decides to "Take Off" it will continue to "claimBaggage()", The program should be able to display that the passengers have left and claimed their baggage at the counter. The thing is, the last passenger that the user had inputted must be the first one that goes and claim their baggage (Last In First Out).
My program have problems, when i'm on the part where i display the passengers that goes out in a LIFO basis, the first passenger has no name and has 0 baggage. but after that the program works fine.
Ex.
Input:
Passenger 1 = John, 5 baggage.
Passenger 2 = Robert, 2 baggage.
Passenger 3 = Carl, 1 baggage.
Current Output:
Passenger _, _ baggage.
Passenger 3 = Carl, 1 baggage.
Passenger 2 = Robert, 2 baggage
Output should be:
Passenger 3 = Carl, 1 baggage.
Passenger 2 = Robert, 2 baggage.
Passenger 1 = John, 5 baggage.
Here is program code:
Code:
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#define c cout
using namespace std;
struct PassengerInfo
{
char name[30];
int baggageCount;
};
int num;
PassengerInfo Passenger[10];
void load();
void takeOff();
void displaySeats();
void claimBaggage();
void main()
{
int choice;
do{
system("cls");
cout<<" _ "<<endl;
cout<<" -=\\`\\ "<<endl;
cout<<" |\\ ____\\_\\__ "<<endl;
cout<<" -=\\c`\"\"\"\"\"\"\" \"`) "<<endl;
cout<<" `~~~~~/ /~~`\ "<<endl;
cout<<" -==/ / "<<endl;
cout<<" '-' "<<endl;
c<<"\n [1] Load Passengers";
c<<"\n [2] Take Off";
c<<"\n [3] Display airplane status";
c<<"\n [4] Exit Program";
c<<"\n\n Select Option: ";
cin>>choice;
c<<endl;
switch(choice)
{
case 1: load(); break;
case 2: takeOff(); break;
case 3: displaySeats(); break;
};
}
while(choice!=4);
}
void load()
{
system("cls");
c<<"\n You are now loading passengers inside the plane. \n\n";
c<<"\n Occupied seats: "<<num;
int bagCtr = 5;
if(num != 10)
{
c<<"\n\n Enter of name of passenger: ";
cin>>Passenger[num].name;
do{
cout <<" Enter number of baggage: ";
cin>> Passenger[num].baggageCount;
//Greater than Baggage Limit
if(Passenger[num].baggageCount > bagCtr)
{
cout <<"\n Sorry, maximum of 5 baggages per person.\n\n";
system("pause");
}
//Invalid Baggage Limit Input
if(Passenger[num].baggageCount < 0)
{
cout << "\n Invalid Input\n\n";
system("pause");
}
//Satisfies the baggage limit
if(Passenger[num].baggageCount > 0 && Passenger[num].baggageCount < bagCtr)
{
//goes back to menu
break;
}
}while(Passenger[num].baggageCount < 0 || Passenger[num].baggageCount > bagCtr);
num++;
c<<"\n Passenger information successfuly added.\n\n";
getch();
}
else
{
cout <<"\n\n Sorry, cannot add any more passengers. The airplane is full\n\n";
system("pause");
}
}
void takeOff()
{
int option = 0;
//Amount of Passengers
if( num <= 10 && num !=0)
{
system("cls");
c<<"\n Are you sure you want to take off with this current amount of passengers?\n\n";
c<<" [1]Yes [2]No\n\n";\
c<<" Enter choice: ";
cin >>option;
//Option
if(option == 1)
{
system ("cls");
c << "\n Plane is ready to take off, please go to your respective seats.\n";
for (int i=0; i<3; i++)
{
c<< " ."<<endl;
Sleep(790);
}
system ("cls");
c << "\n We are now about to take off, please fasten your seat belts.\n";
for (int i=0; i<3; i++)
{
c<<" ."<<endl;
Sleep(790);
}
system ("cls");
c << "\n The plane has landed, passengers may now claim their baggage at the counter.\n\n";
system("pause");
claimBaggage();
}
else
{
main();
}
}
else
{
c <<" We cannot take off, you have no passengers in the plane\n\n";
system("pause");
main();
}
}
void displaySeats()
{
system("cls");
for (int ctr = 0; ctr < num; ctr++)
{
c<<"\n Seat No. "<<10-ctr;
c<<"\n\n Name of passenger: "<<Passenger[ctr].name;
c<<"\n Number of baggages: "<<Passenger[ctr].baggageCount;
c<<endl<<endl;
}
system("pause");
}
void claimBaggage()
{
system("cls");
c<<"\n We will now distribute the baggages of the passengers (LIFO Basis). \n\n";
getch();
for (int ctr = num; ctr > 0; ctr--)
{
system("cls");
c<<"\nMr./Ms. "<<Passenger[ctr].name<<" had left with "<<Passenger[ctr].baggageCount<<" baggages. \n\n";
system("pause");
}
memset(Passenger[num].name, 0, 10);
}
Here is the code where i display the passengers that left with their baggage:
Code:
void claimBaggage()
{
system("cls");
c<<"\n We will now distribute the baggages of the passengers (LIFO Basis). \n\n";
getch();
for (int ctr = num; ctr > 0; ctr--)
{
system("cls");
c<<"\nMr./Ms. "<<Passenger[ctr].name<<" had left with "<<Passenger[ctr].baggageCount<<" baggages. \n\n";
system("pause");
}
}
I also need to reset the array to 0, so when i can input Passengers again.
Thanks in advance.