}
int main()
{
int classchoice, desiredseatr;
string desiredseatc = "";
string reply = "";
cout<<"Which ticket type do you desire."<<endl<<"Our first two rows are first class, rows 3-7 are business class,"<<endl<<"and rows 8-13 are economy class. In which class would like to be seated?"<<endl<<"(Respond 1 for First Class, 2 for Business, and 3 for Economy).: ";
cin>>classchoice;
cout<<endl<<endl;
while ((classchoice == 0)||(classchoice >= 4))
{
cout<<"Error."<<" Please enter a class of 1, 2, or 3.: ";
cin>>classchoice;
cout<<endl;
}
seatchart(airplaneseats(), classchoice);
cout<<"Which seat would you like? "<<"\n"<<"Enter a row number: ";
cin>>desiredseatr;
while (classchoice == 1 && desiredseatr > 2)
{
cout<<"Error"<<"\n"<<"Please enter a row number less than 3: ";
cin>>desiredseatr;
}
while ((classchoice == 2 && desiredseatr < 3)||(classchoice == 2 && desiredseatr >7))
{
cout<<"Error"<<"\n"<<"Please enter a row number which is displayed: ";
cin>>desiredseatr;
}
while ((classchoice == 3 && desiredseatr < 8)||(classchoice == 2 && desiredseatr > 13))
{
cout<<"Error"<<"\n"<<"Please enter a row number which is displayed: ";
cin>>desiredseatr;
}
cout<<"Enter which letter you want your seat: ";
cin>>desiredseatc;
cout<<endl;
cout<<"Congratulations! Your ticket has been booked.";
cout<<endl<<endl;
cout<<"Would you like to book another seat ticket?(Y or N): ";
cin>>reply;
cout<<endl;
while((reply == "Y")||(reply == "y"))
{
cout<<"Which ticket type do you desire."<<endl<<"Our first two rows are first class, rows 3-7 are business class,"<<endl<<"and rows 8-13 are economy class. In which class would like to be seated?"<<endl<<"(Respond 1 for First Class, 2 for Business, and 3 for Economy).: ";
cin>>classchoice;
cout<<endl<<endl;
seatchart(airplaneseats(), classchoice);
cout<<"Which seat would you like? "<<"\n"<<"Enter a row: ";
cin>>desiredseatr;
while (classchoice == 1 && desiredseatr > 2)
{
cout<<"Error"<<"\n"<<"Please enter a row number less than 3: ";
cin>>desiredseatr;
}
while ((classchoice == 2 && desiredseatr < 3)||(classchoice == 2 && desiredseatr >7))
{
cout<<"Error"<<"\n"<<"Please enter a row number which is displayed: ";
cin>>desiredseatr;
}
while ((classchoice == 3 && desiredseatr < 8)||(classchoice == 2 && desiredseatr > 13))
{
cout<<"Error"<<"\n"<<"Please enter a row number which is displayed: ";
cin>>desiredseatr;
}
cout<<"Enter which letter you want your seat: ";
cin>>desiredseatc;
while ((desiredseatc == "G")||(desiredseatc == "g")||(desiredseatc == "H")||(desiredseatc == "h")||(desiredseatc == "I")||(desiredseatc == "i")||(desiredseatc == "J")||(desiredseatc == "j")||(desiredseatc == "K")||(desiredseatc == "k")||(desiredseatc == "L")||(desiredseatc == "l")||(desiredseatc == "M")||(desiredseatc == "m")||(desiredseatc == "N")||(desiredseatc == "n")||(desiredseatc == "O")||(desiredseatc == "o")||(desiredseatc == "P")||(desiredseatc == "p")||(desiredseatc == "Q")||(desiredseatc == "q")||(desiredseatc == "R")||(desiredseatc == "r")||(desiredseatc == "S")||(desiredseatc == "s")||(desiredseatc == "T")||(desiredseatc == "t")||(desiredseatc == "U")||(desiredseatc == "u")||(desiredseatc == "V")||(desiredseatc == "v")||(desiredseatc == "W")||(desiredseatc == "w")||(desiredseatc == "X")||(desiredseatc == "x")||(desiredseatc == "Y")||(desiredseatc == "y")||(desiredseatc == "Z")||(desiredseatc == "z"))
{
cout<<"Error"<<endl<<"Enter a letter that is displayed: ";
cin>>desiredseatc;
}
cout<<endl;
cout<<"Congratulations! Your ticket has been booked.";
cout<<endl<<endl;
cout<<"Would you like to book another seat ticket?(Y or N): ";
cin>>reply;
}
cout<<"Alright everything is set we hope you enjoy your flight.";
This is a program that is supposed to prompt the user to input the class in which he would like to be seated, and when the user inputs the class the program should show a seating chart of the class. So far I have the seating chart, and display right, but I still have some problems.
My program has the following bugs:
1. If the user inputs anything other than a number when prompted to enter the class in which they would like to be seated, the program terminates as if it had completed successfully.
2. If the user inputs anything other than a number when prompted to input a row number the program completes as if it completed successfully.
3. If the user inputs a number when prompted to enter the column letter he wants, the program goes on to the next step as if the prompt had been answered correctly.
For 1. and 2.:
When the wrong input is given, cin sets its failbit flag to true, and while cin.fail()1 would return true, the object will not attempt to read any more information. You can fix this by clearing the flags, e.g.
1 2 3 4 5 6 7
int choice(0);
cout << "Please enter your choice: ";
cin >> choice;
while(cin.fail()){
cout << "Error, not an integer. Please enter your choice again: ";
cin >> choice;
}
For 3:
Even if you enter a number, the program will not know that. Instead, it treats your input as if it were a C-string, so for example if you entered 92, the program would interpret it as "92". Considering all you want is a single letter, you can probably just do this:
1 2 3 4 5 6
char desiredcolumn('\0');
cin >> desiredcolumn;
toupper(desiredcolumn); //Forget checking for both upper- and lowercase. Just use one case.
while(desiredcolumn < 'G' || desiredcolumn > 'Z'){ //Remember chars can be treated as ints
//Blah blah blah...
}