0

Could you please help me determining what is wrong in my code again? Every time you select a case, for example you selected "1" which is "NBA Player", and you are asked who is your favorite player, the program ends as soon as you type your answer. I think the problem is in my getline statement but I can't really identify it.

#include<iostream> #include<conio.h> #include<string> using namespace std; int main() { int choice; string nbaPlayer; string tele; string food; string subject; string x; cout << "This program determines your favorites.\n\n"; cout << "Please select the number of your corresponding choice."; cout << "\n1. NBA Player"; cout << "\n2. Teleserye"; cout << "\n3. Food"; cout << "\n4. Subject"; cout << "\n5. Exit"; cin >> choice; switch (choice) { case 1: cout << "You have chosen NBA Player.\n"; cout << "Please enter your favorite NBA Player. \n"; getline(cin, nbaPlayer); cout << "Your favorite NBA player is " << nbaPlayer; break; case 2: cout << "You have chosen Teleserye.\n"; cout << "Please enter your favorite teleserye. \n"; getline(cin, tele); cout << "Your favorite teleserye is " << tele; break; case 3: cout << "You have chosen food.\n"; cout << "Please enter your favorite food. \n"; getline(cin, food); cout << "Your favorite food is " << food; break; case 4: cout << "You have chosen subject.\n"; cout << "Please enter your favorite subject. \n"; getline(cin, subject); cout << "Your favorite subject is " << subject; break; case 5: cout << "You chose to exit.\n"; break; default: cout <<"\nInvalid input"; } getch(); } 
1
  • @JoachimPileborg what I mean is that the getline statement doesn't work Commented Jul 21, 2013 at 9:22

1 Answer 1

2

Of course it ends, after the switch statement there is nothing to continue the program.

You probably want a loop around the output and switch:

bool go_on = true; while (go_on) { // Output menu... // Get choice switch (choice) { // All other cases... case 5: go_on = false; // Tell loop to end break; } } 

Oh, it seems that your problem is that you get an empty line... This is because after you get the choice, the stream leaves the newline in the input buffer, so when you do std::getline it reads that newline and not the input you want.

You can remove the trailing newline like this:

std::cin >> choice; // Skip trailing text, up to and including the newline std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') 
Sign up to request clarification or add additional context in comments.

3 Comments

I suspect this is what the getch() at the end was intended for, but it's in the wrong place.
@Barmar It seems to very common to use e.g. getch at the end to stop the program from exiting until a key is pressed, especially for beginners. And getch can't be used for this as it bypasses the stream input and read directly from the console.
Ahh, that makes sense. I knew it bypassed the stream, but I thought that was just another beginner error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.