Flight Reservation Program

For a computer science class that I am taking, I am instructed to add on to the code that my instructor has written so that the user can make reservations using the makeReservation function and use getInfo and showInfo to put information into SEAT structs for each user and for each seat. I am slowly making my way through it and I can enter in information for one user(i have yet to set up an array for multiple amounts of users) and it will show his or her information but after it displays the struct it goes back to the menu and begins going into an infinite loop wanting the user to enter in whether or not they want to find it automatically or manually.

Any help is very appretiated.

My full code is here at http://www.chiefgamer.com/tman/airplanev2.cpp
I think the problem is either in the menu choice function or main, maybe in the makeReservation function but I cannot find it
It's not the actual menu, the
1
2
3
4
  do {
    cout << "Would you like to select a seat (a)utomatically or (m)anually: ";
    cin >> method;
  } while (tolower(method)!='a' && tolower(method)!='m');

is fine.

Oh, and you slacked a little in the end. Where it is
1
2
3
4
5
6
		do{
			
			cout<<"What type of meal would you like?((r)egular, (v)egatarian, or (o)ther) ";
			cin>>s.mealType;
			cout<<"\n";
		}while(s.mealType!='r' && s.mealType!='v' && s.mealType!= 'o');

you should have
1
2
3
4
5
6
		do{
			
			cout<<"What type of meal would you like?((r)egular, (v)egatarian, or (o)ther) ";
			cin>>s.mealType;
			cout<<"\n";
		}while(tolower(s.mealType)!='r' && tolower(s.mealType)!='v' && tolower(s.mealType)!= 'o');


Why is it necessary in showInfo to define the three strings?

I'm sorry, but I could not find flaw in your code, at least anything obvious.
But I have insight: this happens to me all the time when my cin operation fails inside a loop that requires input to get out of. You could try using a cin.clear() in your suspect trouble spot, and before that a !cin to determine if it failed somehow.

Sorry, I can help no more.
Thanks for the help. The cin.clear() method did not excactly work. And also when I am asked by the getInfo function if the information is correct, if I enter yes it goes back to the menu which somehow triggers an infinite loop. I thought maybe it was in the do while loop in main. Also if I enter no, when I am entering my name, it only accepts the first name and puts an 'o' in the place of the first name. i updated the existing code with little things that I have changed.

again any help is greatly appretiated.
With your new information, I have a theory...

When you type yes at the cin>>answer spot, cin buffers ALL of the characters, then extracts the first one to assign to answer, Y. Since this is not n, function getInfo terminates, with the characters es\n still on the stream. Function makeReservation terminates, then control returns to toplevel main, and menuchoice is called. The cin>>choice line in menuchoice then reads the es\n, truncates the \n, and tries to apply this to choice. It fails, sets its error flag to ios::fail, and returns without assigning anything. Because flag ios::fail is set, all subsequent calls will also fail, and since the user cannot interact, an infinite loop is caused.

This has a simple fix. Change
1
2
3
4
  do {		  
    cout << "Choice: ";
    cin >> choice;
  } while (choice < 1 || choice > 4);

to
1
2
3
4
5
6
  do {		  
    cout << "Choice: ";
    cin >> choice;
    if(!cin)
        cin.clear();
  } while (choice < 1 || choice > 4);


Hope that helps!
Topic archived. No new replies allowed.