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.
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?