The problem is three-fold:
1) As already mentioned, "pause" is a fairly DOS/Windows-ish thing to do. On linux, you'd use the bash script:
read -n 1 -p "Press any key to continue . . ."
2) Using system() is a security hole. Use it only for homework.
3) The way it works is different than the way user-input normally works. Normally, input is "line-buffered", meaning that the user _must_ press ENTER to finish giving input. The pause command(s) are not line-buffered. Pressing just about any key will work.
Turning off line-buffering (and a few other things) is a fairly involved, platform-dependent task (but it isn't hard at all). However, for _standard_C++_ you can easily write your own pause:
1 2 3 4 5 6
|
void pause() {
cout << "Press ENTER to continue..." << flush;
cin.clear(); // use only if a human is involved
cin.flush(); // use only if a human is involved
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
| |
Thanks to _dirk for the suggestion for flushing the stream. This will play havoc if you are _not_ connected to a human.
If y'all want to know how to do it the non-line-buffered way, let me know. (And specify what platform you are using.)