Universal sys pause

so some people use
1
2
3
 cin.get();
 cin.ignore(255, 'n');
 cin.clear();


for a system pause, shouldn't it be

1
2
3
cin.get();
cin.ignore(0);
cin.clear();


or

1
2
3
cin.get();
cin.ignore(0, 'n');
cin.clear();
The defaults for ignore are (1,'\n') so just cin.ignore(); would suffice for system. If you set the max chars argument to 0 the function will exit immediately (not really the behavior you want)
ah yea i never though about just cin.ignore(); by itself.
I think all of them are wrong. clear() should be called first to clear any error state otherwise the other functions will just return right away in case of an error. The second function that should be called is ignore(). This is to ignore the rest of the line. Use a large number like INT_MAX to be able to handle long lines. Last we call get() to wait for any input.
1
2
3
cin.clear();
cin.ignore(INT_MAX, '\n');
cin.get();


This will only work properly if there is a newline char already in the input stream. If the last read was done by using std::getline or if you have not used any input at all you will have to press enter twice. In that case I think it's better to just call
1
2
cin.clear();
cin.get();


I can't think of a way that works for both cases.
Last edited on
Include <limits> for this to work.
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
You shouldn't worry about a new line character already being in the stream, this just means that the user pressed enter during your (excessively long?) output and didn't want to read it.
Last edited on
Topic archived. No new replies allowed.