Consistant portable pausing of the console

Hello all,

I am trying to stop using the system("pause") command to pause the console, since I understand that it conjures demons, and will get me kicked out of a job interview.

Anytime I'm displaying a few sentences at once on the console, I like to pause the program so the user has time to read before moving on.

The main way that I have been trying to do this:

 
  cin.get();

or

 
  getchar();


Now a big difference between this and system("pause") is that it allows the user to input some characters in a place where I really just want them to press enter. If that's all there was to it, I could live with that. The problem is that this is messing with the other input functions of the program, such as:

1
2
  // input is a string
  getline(cin, input)


I've tried a few things to resolve this, like:

1
2
  cin.ignore();
  cin.clear();

or

1
2
  cin.ignore(INT_MAX, '\n');
  cin.clear();


But this only results in my program behaving even more odd, such as requiring me to press enter twice on the lines where I do want to take input.


What is a good, consistent, way of pausing the console without causing the input buffer to torment me?
Last edited on
1
2
// input is a string
  getline(cin, input)

what's wrong with this?

try this:
1
2
3
4
5
#include <limits> 

// ...

std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
Last edited on

try this:
1
2
3
4
5
#include <numeric> 

// ...

std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


I have tried that as well. That just allowed me to press enter a bunch of times without progressing in the application.

I've been digging through the megathread at the top of this forum a little more deeply, and I think I may have found what I was looking for. Unfortunately that thread is hard to read since it is filled with quite a bit of drama.

Putting:
1
2
cin.sync();
getline(cin, input);

Has cleared up the issues I was having in this particular case.

So if I understand correctly...

you can use cin.get(); or getchar(); to pause the application, but if you do, you should probably use cin.sync(); before taking any actual input from the user.
Last edited on
Using cin.ignore(...) is the correct way to do what you are asking. If it isn't working right for you it is because you are not reading input correctly.

Keeping your input synchronized is part of your job (as the programmer) when handling it. Unfortunately, the ubiquity of using cin >> foo typically throws a wrench in the proper understanding of user input.

Get all user input with getline( cin, s ) and parse it after that. This makes sure things are properly synchronized.

Also, cin.sync() is useless. Don't bother with it -- it won't help you.

Hope this helps.
Using cin.ignore(...) is the correct way to do what you are asking. If it isn't working right for you it is because you are not reading input correctly.

Keeping your input synchronized is part of your job (as the programmer) when handling it. Unfortunately, the ubiquity of using cin >> foo typically throws a wrench in the proper understanding of user input.

Get all user input with getline( cin, s ) and parse it after that. This makes sure things are properly synchronized.

Also, cin.sync() is useless. Don't bother with it -- it won't help you.

Hope this helps.


Ahhhh I see now. if you want to use cin.ignore() to pause, then you have to add the parameters, making the "pause" command:

1
2
3
4
5
using namespace std;

cin.ignore(INT_MAX, '/n');
// or
cin.ignore(numeric_limits<streamsize>::max(), '/n');


I was originally trying to pause using cin.get(); or getchar();, then using the various forms of cin.ignore(); to try to wipe the input buffer before the next cin.getline(cin, input);. I see now that just using one of the above listed commands does it in one step.

I never use cin >> whatever unless i'm doing some quick and dirty debugging or something. I always use getline(cin, input) for the reasons you just described.

I wouldn't say that cin.sync() is useless, because it did do what I was "looking for", but I now see why using this command is more clean and maintains better control of the input.

Sounds like I get it now? :-P
Yes.

What istream::sync() does is makes sure that the input stream buffer is properly synchronized with its attached source, as appropriate. It may do nothing. For what you are doing there is no need for you to explicitly 'sync' it.
Topic archived. No new replies allowed.