Hi guys, im making an airline reservation system where a user is able to choose a there own seat. But there are two classes, first class and second class. First class can hold seats from 1-5, second class is 6-10.
If a user picks seat number 6 in first class, i want it to tell the user there is an error and that the seating arrangements is 1-5.
I have written the code so far and at the moment, if a user picks a seat higher than 6, it will display the error message but it will also display the message saying "your seat number is; "
I want it to completely say no, you cannot pick this seat, im a beginner programmer and this is an assignment for work, this is the remaining part of the code so once i get this, the rest of my code is simple enough. Heres my code
#include<iostream>
usingnamespace std;
int f=1,s=6,ans, mainmenu;
bool firstclass [5]= {0};
bool secondclass [5]= {0};
int fspace = 5;
char check,con, position;
int main()
{
mainmenu:
cout <<"\t--Hello, welcome to AIRLINE RESERVATION--" << endl;
cout <<"--Please select the following:" << endl;
cout <<"--[1] For a First-class seat" << endl;
cout <<"--[2] For a Second-class seat" << endl;
cout <<"--[3] Quit program.\n\n" << endl;
do
{
cout<<"\tWhich class would you like?" << endl;
cin>>ans;
{
if (ans == 1)
goto firstClass;
}
{
if (ans == 2)
goto secondClass;
}
if (ans == 1 && f<=5)
{
firstClass:
cout << "\t--Welcome to first class--" << endl;
cout<<"\tPlease choose a seat"<< endl;
cin >> position;
f = position - 1;
if (firstclass [f] == 0)
{
firstclass [f] = 1;
cout << "Your seat number is: " << position << endl;
}
else
{
cout << "This seat is already booked" << endl;
}
if (f >= fspace)
{
cout << "The seating arrangements for this class is 1-5, please pick again." << endl;
}
cout<<"\t--Would You Like To Continue? (y/n)--\n";
cin>>check;
{
if (check == 'n')
goto mainmenu;
}
}
elseif (ans == 1 && f>5)
{
cout<< "\tUnfortunately, the First class is now full." << endl;
cout<<"\tDo you want a seat in the Second classs? (y/n)\n";
cin>>con;
if ((con == 'Y' || con =='y') && s<=10)
{
secondClass:
cout<<"Your seat number is "<<s<<".\n";
s++;
cout<<"Do you want to continue(y/n)\n";
cin>>check;
}
elseif ((con == 'Y' || con =='y') && s>10)
{
cout<<"Sorry,all seats are taken!!\n\n";
cout<<"The next flight leaves in 3 hours;)!\n";
return 0;
}
elseif (con != 'y' || con != 'Y')
{
cout <<"The next flight leaves in 3 hours!\n";
return 0;
}
}
if (ans == 2 && s<=10)
{
cout<<"Your seat number is "<<s<<".\n\n";
s++;
cout<<"Do you want to continue? (y/n)\n";
cin>>check;
}
elseif (ans == 2 && s>10)
{
cout<<"\t\tSorry, but the second class is now full!\n";
cout<<"\tWould you like a seat in first class?(y/n)\n";
cin>>con;
if ((con == 'Y' || con =='y') && f<=5)
{
cout<<"Your seat number is "<<f<<".\n";
cout<<"Do you want to continue?(y/n)\n";
cin>>check;
}
elseif ((con == 'Y' || con =='y') && f>5)
{
cout<<"Soryy,all seats are taken!!\n\n";
cout<<"The next flight leaves in 3 hours!\n";
return 0;
}
elseif (con != 'y' || con != 'Y')
{
cout<<"The next flight leaves in 3 hours!\n";
return 0;
}
}
elseif (ans>3)
{
cout<<"Incorrect Input!!\n\n\n";
cout<<"\n";
cout<<"\n";
return main();
}
}
while (check == 'y' || check == 'Y');
return 0;
}
#include<iostream>
usingnamespace std;
int f=1,s=6,ans, mainmenu, position;
bool firstclass [5]= {0};
bool secondclass [5]= {0};
int fspace = 5;
char check,con;
int main()
{
mainmenu:
cout <<"\t--Hello, welcome to AIRLINE RESERVATION--" << endl;
cout <<"--Please select the following:" << endl;
cout <<"--[1] For a First-class seat" << endl;
cout <<"--[2] For a Second-class seat" << endl;
cout <<"--[3] Quit program.\n\n" << endl;
do
{
cout<<"\tWhich class would you like?" << endl;
cin>>ans;
{
if (ans == 1)
goto firstClass;
}
{
if (ans == 2)
goto secondClass;
}
if (ans == 1)
{
firstClass:
cout << "\t--Welcome to first class--" << endl;
cout<<"\tPlease choose a seat"<< endl;
cin >> position;
f = position - 1;
if(f<=5)
{
if (firstclass [f] == 0)
{
firstclass [f] = 1;
cout << "Your seat number is: " << position << endl;
}
else
{
cout << "This seat is already booked" << endl;
}
}
elseif (f > fspace)
{
cout << "The seating arrangements for this class is 1-5, please pick again." << endl;
}
cout<<"\t--Would You Like To Continue? (y/n)--\n";
cin>>check;
{
if (check == 'n')
goto mainmenu;
}
}
elseif (ans == 1 && f>5)
{
cout<< "\tUnfortunately, the First class is now full." << endl;
cout<<"\tDo you want a seat in the Second classs? (y/n)\n";
cin>>con;
if ((con == 'Y' || con =='y') && s<=10)
{
secondClass:
cout<<"Your seat number is "<<s<<".\n";
s++;
cout<<"Do you want to continue(y/n)\n";
cin>>check;
}
elseif ((con == 'Y' || con =='y') && s>10)
{
cout<<"Sorry,all seats are taken!!\n\n";
cout<<"The next flight leaves in 3 hours;)!\n";
return 0;
}
elseif (con != 'y' || con != 'Y')
{
cout <<"The next flight leaves in 3 hours!\n";
return 0;
}
}
if (ans == 2 && s<=10)
{
cout<<"Your seat number is "<<s<<".\n\n";
s++;
cout<<"Do you want to continue? (y/n)\n";
cin>>check;
}
elseif (ans == 2 && s>10)
{
cout<<"\t\tSorry, but the second class is now full!\n";
cout<<"\tWould you like a seat in first class?(y/n)\n";
cin>>con;
if ((con == 'Y' || con =='y') && f<=5)
{
cout<<"Your seat number is "<<f<<".\n";
cout<<"Do you want to continue?(y/n)\n";
cin>>check;
}
elseif ((con == 'Y' || con =='y') && f>5)
{
cout<<"Soryy,all seats are taken!!\n\n";
cout<<"The next flight leaves in 3 hours!\n";
return 0;
}
elseif (con != 'y' || con != 'Y')
{
cout<<"The next flight leaves in 3 hours!\n";
return 0;
}
}
elseif (ans>3)
{
cout<<"Incorrect Input!!\n\n\n";
cout<<"\n";
cout<<"\n";
return main();
}
}
while (check == 'y' || check == 'Y');
return 0;
}
I have implemented your changes and made alot of other ones, the changed you made helped it stop it from booking seats 6 and above, but now it is blocking any seats being booked, here is my new code
#include<iostream>
usingnamespace std;
int f=1,s=6,ans, mainmenu;
bool firstclass [5]= {};
bool secondclass [10]= {};
int fspace = 5;
int sspace = 10;
char check,con, position;
int main()
{
mainmenu:
cout <<"\t--Hello, welcome to SHARMA AIRLINE RESERVATION--" << endl;
cout <<"--Please select the following:" << endl;
cout <<"--[1] For a First-class seat" << endl;
cout <<"--[2] For a Second-class seat" << endl;
cout <<"--[3] Quit program.\n\n" << endl;
do
{
cout<<"\tWhich class would you like?" << endl;
cin>>ans;
{
if (ans == 1)
goto firstClass;
}
{
if (ans == 2)
goto secondClass;
}
//if (ans == 1 && f<=5)
if (ans ==1)
{
firstClass:
cout << "\t--Welcome to first class--" << endl;
cout<<"\tPlease choose a seat from 1-5"<< endl;
cin >> position;
//for (f = 0; f <=fspace; f++)
f = position - 1;
//if (firstclass [f] == 0)
if (f<=5)
{
if (firstclass [f] == 0)
{
firstclass [f] = 1;
cout << "Your seat number is: " << position << endl;
}
else
{
cout << "This seat is already booked" << endl;
}
}
elseif (f > fspace)
{
cout << "The seating arrangements for this class is 1-5, please pick again." << endl;
}
cout<<"\t--Would You Like To Continue? (y/n)--\n";
cin>>check;
{
if (check == 'n')
goto mainmenu;
}
}
elseif (ans == 1 && f>5)
{
cout<< "\tUnfortunately, the First class is now full." << endl;
cout<<"\tDo you want a seat in the Second classs? (y/n)\n";
cin>>con;
if ((con == 'Y' || con =='y') && s<=10)
{
secondClass:
cout << "\t--Welcome to Second class--" << endl;
cout<<"\tPlease choose a seat from 6-10"<< endl;
cin >> position;
for (s = 0; s <=sspace; s++)
s = position - 1;
if (secondclass [s] == 0)
{
secondclass [s] = 1;
cout << "Your seat number is: " << position << endl;
}
else
{
cout << "This seat is already booked" << endl;
}
if (s == sspace)
{
cout << "The seating arrangements for this class is 6-10, please pick again." << endl;
}
cout<<"\t--Would You Like To Continue? (y/n)--\n";
cin>>check;
}
elseif ((con == 'Y' || con =='y') && s>10)
{
cout<<"Sorry,all seats are taken!!\n\n";
cout<<"The next flight leaves in 3 hours;)!\n";
return 0;
}
elseif (con != 'y' || con != 'Y')
{
cout <<"The next flight leaves in 3 hours!\n";
return 0;
}
}
elseif (ans == 2 && s>10)
{
cout<<"\t\tSorry, but the second class is now full!\n";
cout<<"\tWould you like a seat in first class?(y/n)\n";
cin>>con;
if ((con == 'Y' || con =='y') && f<=5)
{
goto firstClass;
}
if ((con == 'Y' || con =='y') && f>5)
{
cout<<"Soryy,all seats are taken!!\n\n";
cout<<"\tWould you like a seat in first class?(y/n)\n";
cin >> con;
if ((con == 'Y' || con =='y') && s>=6)
{
goto secondClass;
}
cout<<"The next flight leaves in 3 hours!\n";
return 0;
}
elseif (con != 'y' || con != 'Y')
{
cout<<"The next flight leaves in 3 hours!\n";
return 0;
}
}
elseif (ans>3)
{
cout<<"Incorrect Input!!\n\n\n";
cout<<"\n";
cout<<"\n";
return main();
}
}
while (check == 'y' || check == 'Y');
return 0;
}
So if I have number 9 for example, in ascii, to make an integer equal to it I put:
1 2
char c = 9;
int i = c - 48
Or number 4:
1 2
char c = 4;
int i = c - 48;
In each case, i will be 9 and 4 respectively.
You need to take away one more than this too, in your particular program, (thus -49), since you were taking one away in the first place. Does this all make sense?
oh my you completely redesgined my program. For the above program i am geting this error:
(13) : error C3861: 'MainMenuDisplay': identifier not found
(18) : error C3861: 'FirstClass': identifier not found
p(22) : error C3861: 'SecondClass': identifier not found
I think this program is just getting big and ugly where it does not need to. I would say, get your original program working, ignoring what code bluecoder has written (although he is right you should be using functions!). Then, we can take a look at how to make the program nicer, more compact and readable.
I agree mat, but i just dont understand this ascii thing, and this is for a project at college that im doing and i want the best grade possible, but learning at the same time, ive done a little bit on functions and understand it slightly, not using it was a very silly mistake of mine but looking at bluecoder and seen what he has done it looks so much neater and clearer to read.
If you guys can help me progress from this i will be forever in your debt!
edit: also im not taking bluecoder route because it makes it easier for me as a student, its not the case:P
Basically, use an int where you are dealing ONLY in whole numbers. Use a char ONLY when you are using characters. Don't use a char to represent numbers, if you can simply declare another int to use.
I would also avoid using goto. It's bad practice to use goto.
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
usingnamespace std;
int f=0,s=6,ans, mainmenu;
bool firstclass [5]= {};
bool secondclass [11]= {};
int fspace = 5;
int sspace = 11;
char check,con, position;
int main()
{
mainmenu:
cout <<"\t**WELCOME TO SHARMA AIRLINE RESERVATION**" << endl;
cout <<"\t----Please select from the following: ----" << endl;
cout <<"\t----For first classs press : 1----" << endl;
cout <<"\t----For second class press : 2----" << endl;
cout <<"\t----To quit the program press : 3----" << endl;
do
{
cout<<"\t----Which class would you like?----" << endl;
cin>>ans;
{
if (ans == 1)
goto firstClass;
}
{
if (ans == 2)
goto secondClass;
}
//if (ans == 1 && f<=5)
if (ans ==1)
{
firstClass:
cout << "\t--Welcome to first class--" << endl;
cout<<"\tPlease choose a seat from 1-5"<< endl;
cin >> position;
//for (f = 0; f <=fspace; f++)
f = position - 48;
//if (firstclass [f] == 0)
if (f<=5)
{
if (firstclass [f] == 0)
{
firstclass [f] = 1;
cout << "Your seat number is: " << position << endl;
}
else
{
cout << "This seat is already booked" << endl;
}
}
elseif (f > fspace)
{
cout << "The seating arrangements for this class is 1-5, please pick again." << endl;
}
cout<<"\t--Would You Like To Continue? (y/n)--\n";
cin>>check;
{
if (check == 'n')
goto mainmenu;
}
}
elseif (ans == 1 && f>5)
{
cout<< "\tUnfortunately, the First class is now full." << endl;
cout<<"\tDo you want a seat in the Second classs? (y/n)\n";
cin>>con;
if ((con == 'Y' || con =='y') && s<=10)
{
secondClass:
cout << "\t--Welcome to Second class--" << endl;
cout<<"\tPlease choose a seat from 6-10"<< endl;
cin >> position;
//for (s = 0; s <=sspace; s++)
s = position - 54;
if (s<10)
{
if (secondclass [s] == 0)
{
secondclass [s] = 1;
cout << "Your seat number is: " << position << endl;
}
else
{
cout << "This seat is already booked" << endl;
}
}
elseif (s != sspace)
{
cout << "The seating arrangements for this class is 6-5, please pick again." << endl;
}
cout<<"\t--Would You Like To Continue? (y/n)--\n";
cin>>check;
}
elseif ((con == 'Y' || con =='y') && s>10)
{
cout<<"Sorry,all seats are taken!!\n\n";
cout<<"The next flight leaves in 3 hours;)!\n";
}
elseif (con != 'y' || con != 'Y')
{
cout <<"The next flight leaves in 3 hours!\n";
}
}
elseif (ans == 2 && s>10)
{
cout<<"\t\tSorry, but the second class is now full!\n";
cout<<"\tWould you like a seat in first class?(y/n)\n";
cin>>con;
if ((con == 'Y' || con =='y') && f<=5)
{
goto firstClass;
}
if ((con == 'Y' || con =='y') && f>5)
{
cout<<"Soryy,all seats are taken!!\n\n";
cout<<"\tWould you like a seat in secondclass class?(y/n)\n";
cin >> con;
if ((con == 'Y' || con =='y') && s>=6)
{
goto secondClass;
}
cout<<"The next flight leaves in 3 hours!\n";
}
elseif (con != 'y' || con != 'Y')
{
cout<<"The next flight leaves in 3 hours!\n";
}
}
elseif (ans>3)
{
cout<<"Incorrect Input!!\n\n\n";
cout<<"\n";
cout<<"\n";
return main();
}
}
while (check == 'y' || check == 'Y');
return 0;
}