>Noobs *rolleyes*
Um, I fail to see what the problem is that warrants this kind of response.
>cin.ignore(256, '\n');
>There's also getch.
Let's not keep it so simple that we produce bad code, mmkay? Here are the issues:
1) The program is running interactively, so the user needs to see any output.
Obviously, if the program terminates and closes the window before the user has time to read any output, that's an issue.
2) The program is running in a separate process with its own console window.
If the program owns the window and the process terminates, the window will be destroyed, thus losing any of the output before the user can read it.
3) The program is performing unclean input that might leave junk in the stream.
This happens when you mix formatted and unformatted input, cin's >> operator and cin.get, for example.
4) The program uses a blocking single character read to stop execution.
If there are any characters in the stream, this read will succeed immediately and there won't be any blocking.
The solution is to discard leftover characters in the stream if they exist so that the blocking read blocks, but only if the program is running interactively in a separate process. Here's a largely portable solution that covers all of the bases, but it's kind of voodooish:
Code:
#include <cstring>
#include <iostream>
#include <ios>
#include <istream>
#include <limits>
namespace jsw {
template <typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& ignore (
std::basic_istream<CharT, Traits>& in, CharT delim )
{
if ( in.rdbuf()->sungetc() != Traits::eof() && in.get() != delim )
in.ignore ( std::numeric_limits<std::streamsize>::max(), delim );
return in;
}
}
int main ( int argc, char *argv[] )
{
bool interactive;
bool owns_console;
for ( int i = 1; i < argc; i++ ) {
if ( std::strlen ( argv[i] ) == 2 ) {
if ( argv[i][0] == '-' ) {
if ( argv[i][1] == 'i' )
interactive = true;
else if ( argv[i][1] == 'o' )
owns_console = true;
}
}
}
// Your program goes here
if ( interactive && owns_console ) {
std::cin.clear();
jsw::ignore ( std::cin, '\n' );
std::cout<<"Press Enter to continue . . .";
std::cin.get();
}
}
The problem is that while you can portably control the stream contents (sort of), you can't portably tell with code how the program is being run. So you can use parameters to tell that. When you have problems keeping the output window open, just add -i and -o to the run string:
As a side note, anything besides the following two options is probably a poor choice when it comes to clearing an unfinished line from the input stream:
Code:
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
Code:
char ch;
while ( std::cin.get ( ch ) && ch != '\n' )
;
That means getch is a poor choice (Yarin), and random magic numbers for the ignore amount are a poor choice (sh3rpa), and system("PAUSE") is an extremely poor choice for various reasons (SVXX). All in all, this is a deceptively simple operation, but it's very easy to step into the realm of non-portable behavior.