Prompting the user to press Enter

I know the cin.get(); is supposed to prompt the user to press enter... is it not? I can't figure out why this is not working... take a look

{string command;
while ( 0==0 ){
cout<< "Please specify a command:\n";
cin>> command;
if ( command == "End"){
cout<<"Process Ending.";
cin.get();
return 1;
}

I thought this would display "Process Ending." and then allow the user to press enter before "return 1;"... but it does not allow me too... however if I do this:

{string command;
while ( 0==0 ){
cout<< "Please specify a command:\n";
cin>> command;
if ( command == "End"){
cin.get();
cout<<"Process Ending.";
cin.get();
return 1;
}

It DOES allow me to press enter before "return 1;"... my question is why does it do this? Is there a more reliable way to prompt the user to press enter? Why does it only work if I put cin.get(); above AND below cout<<"Process Ending.";? Shouldn't it work if I just put it below? Thank you in advance!
cin.get() doesn't prompt the user. It doesn't have any output. I think you are thinking of system("pause").

The iostream equivalent of system("pause") is:
1
2
cout << "Press any key to continue...";
cin.get();


cin handles ONLY inputs so you will never see something appear on the screen as a result of a cin (unless you are actually mashing keys). cout handles all outputs.
Last edited on
You need something like
1
2
3
cout << "Press enter to continue . . . ";
cin.sync();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

at the end of your program.

Make sure to put #include <limits> in your program as well.

Oh, and I wouldn't recommend using system("pause").
Here's why: http://www.cplusplus.com/forum/articles/11153/
Last edited on
I'm with ld main here, cin.ignore(); is the better choice; system isn't something you want in production code.
When you input the word you press Enter. You read the word, but the buffer still contains the '\n' and that's what cin.get() takes.
That's the reason you need to do it twice.

Solution: Don't do it. ¿why should the user need to press Enter?
Topic archived. No new replies allowed.