cin.ignore only working at a certain point :S

Hiya,
New to C++ (Thats why this is in the beginners section! =) ) and I'm having a bit of trouble with cin.ignore(), and asking my program to prompt before continuing. Seems to be a common issue but I cant seem to resolve it. It will work at a certain point in my code but not a couple of lines later. I want my program to prompt the user to 'Press enter to continue' and various points of a quiz.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    cout << "\n\nWelcome to a basic quiz\n" << endl;
    cout << "Lets start with the sports questions. Press ENTER to begin.\n.";
    cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); //THIS ONE WORKS
    sporttest();
    cout << "\nNext up is our food quiz. Press ENTER to begin.\n";
    cin.ignore(std::numeric_limits<streamsize>::max(),'\n');//THIS ONE DOESN'T
    food();
    name();
    entertoclose();

  return 0;
  }
  


The functions are all forward declared and work just fine, and the cin.ignore line works the first time, but not the second, even if I delete the first one the second will just not prompt 'Enter to continue'.

Confused!!

Thanks in advance for any advice!
Does sporttest uses std::cin too?
If so there is possibly endline symbol left in input buffer.
the second will just not prompt 'Enter to continue'.

If you don't see the output, maybe you need to use endl rather than '\n', in order to flush the output buffer.
Failing that, are you sure that the function sporttest() has actually returned, or maybe it is waiting for something?
MiiNiPaa, There is use of cin in sporttest, so perhaps that's where the trouble lies?

Chervil, sportest and all other functions run as I want them too, its just I would rather the user could 'press enter to continue' before the functionkicks into action, so the amount of text they get barraged with is controlled if you get what I mean. I'll replace my \n with endl and see if that helps to flush the buffer!
Hmm, I think I may have misunderstood. My impression was that the text "Press ENTER to begin" was not being displayed. Maybe it is displayed, but the program then proceeds to execute the food() function?

Anyway, if you have code like this:
cin >> x;
then the user must press enter after supplying the value for x. Afterwards, the newline remains in the input buffer. The next cin.ignore() will find that newline, and hence not wait. A possible answer is to use cin.ignore twice in succession, the first time to clear the contents of the buffer, the second in order to wait for the user.
Yes. for example if you have code
1
2
int x;
std::cin >> x;

and you enter "13"
there will be actually "13\n" in input buffer ('\n' because you pressed enter)
After cin >> x the "\n" will be left in input buffer. So ignore() will never request additional input because if finds everything it needs in buffer.

Solution: clear your stream at the end of sporttest().
Well the problem was clearing the buffer at the end of sporttest(), so thanks for the advice! Everything works fine now, although it wasn't endl; that fixed it, I did a bit of a google and used:

while(cin.get() != '\n');

and that cleared everything up. Thanks again!

Pierce
Topic archived. No new replies allowed.