System ("pause");

Any alternatives?
std::cin.get()
I'm against using stream hackery just because someone says system("PAUSE"); is nasty, nasty code (and feels smugly superior doing so).

Use that instruction in homework and when learning the language.
As you become a more experienced programmer the need for system("PAUSE"); will be gone by itself.
Last edited on
I'm against using stream hackery just because someone says system("PAUSE"); is nasty, nasty code (and feels smugly superior doing so).


So wait, we've given this chap a better, shorter, faster, safer and standard-compliant alternative, and you disagree with it because someone else said system("PAUSE") is nasty?
Last edited on
LOL! Apparently, Moschops. Go figure.

Anyway, happy reading: http://www.cplusplus.com/forum/beginner/1988/

The truly awesome way of doing it is encapsulating the pausing code inside the destructor of a class. This is therefore guaranteed to run even in the event of a runtime error!
and you disagree with it because someone else said system("PAUSE") is nasty?


Ooops! I swear this keyboard makes me write stuff...
Last edited on
I think the entire debate would be over if they just had a system("you_know_what_I_mean"); command! -:)
Just initialize a variable at the beginning called pause and you could do something like:

cout << "Press any key to continue...";
cin >> pause;
GCC and by extension MingW allows you to declare a function as a destructor to the program as a whole. This is by far my favorite method as it pauses your program at the end without you having to enter anything AND does not prevent you from calling the same function at anytime in your code. Here is my method:
1
2
3
4
5
6
7
void pause() __attribute__((destructor)); /* Function Declaration, You Cannot Declare Attributes Without This */

void pause()
{
   std::cin.sync(); // Flush The Input Buffer Just In Case
   std::cin.ignore(); // There's No Need To Actually Store The Users Input
}


EDIT: I've been digging around MSDN for a while now to find a way to do this with CL (That is M$'s compiler), but haven't found anything yet because it's not my main compiler. If anyone knows of a way to do this PLEASE let me know.

EDIT 2: @ OP: You can also look into "atexit()": http://www.cplusplus.com/reference/clibrary/cstdlib/atexit/
Last edited on
Topic archived. No new replies allowed.