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