System ("pause");

Nov 19, 2011 at 3:49am
Any alternatives?
Nov 19, 2011 at 8:06am
std::cin.get()
Nov 19, 2011 at 10:01am
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 Nov 19, 2011 at 10:04am
Nov 19, 2011 at 2:14pm
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 Nov 19, 2011 at 2:19pm
Nov 19, 2011 at 7:11pm
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!
Nov 19, 2011 at 11:16pm
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 Nov 19, 2011 at 11:16pm
Nov 20, 2011 at 3:52am
I think the entire debate would be over if they just had a system("you_know_what_I_mean"); command! -:)
Nov 21, 2011 at 12:48am
Just initialize a variable at the beginning called pause and you could do something like:

cout << "Press any key to continue...";
cin >> pause;
Nov 21, 2011 at 2:13pm
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 Nov 21, 2011 at 2:30pm
Topic archived. No new replies allowed.