best way of "pausing" programs

i now use:
1
2
cin.sync();
cin.get();


i know i could also add a cin.clear() but i dont see the need to do that if i just want to pause

what do you guys think?? am i doing it right or is there a better way of doing this?
¿why do you want to pause it?
when i show something on the screen for example and i want to give the user the chance to read it
Last edited on
The "right" way is to not pause the program. If you are using windows and thinks it's a "problem" just use system("pause");.
Last edited on
i dont think im ever gonna use that...
Keep the console open long enough to see your program's output http://cplusplus.com/articles/iw6AC542/
A wrapper so you don't modify your code http://cplusplus.com/forum/beginner/1988/3/#msg14102 (windows)

I know that you can config xterm so it doesn't close on termination.
long codes for small thing ;)
but thanks
¿Long? It's 1 line...
cin.clear(); cin.sync(); cin.ignore(unsigned(-1));
Good for very simple console programs, but not the best or most proper solution.
You forgot to put a terminating character in your 'ignore' method call.

cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
Last edited on
Oops, I guess my compiler is not standard conforming - instead of it defaulting to EOF it defaults to '\n' o_o
i now use:
cin.sync();

The effects of cin.sync() are implementation-defined, it is not guaranteed to do anything (it's only a step above the infamous fflush(stdin), which is UB). The call to ignore(), as shown by Duoas is what you need if your goal is to consume all entered, but not processed, input characters.
Who cares, seriously? The only reason anyone is going to need to pause the console to read text is if they're just doing homework or learning. So why not just use system( "pause" ) in that case? Now if you were writing a more advanced program where cross platform capabilities and efficiency are important then you shouldn't use this but, let's face it, no one writing a program like that is going to be asking this question. :D
It's all to easy to fall into habits so better get into good ones early on ;)
@codingshark

yeah, its true. i only want to use it for my school tasks but i just was interested in what ways you can do it the best. even if its just some homework i still like doing it the better way(s)...
i'm still a beginner programmer but im learning pretty fast and programming in school bores me cause its sooo slooooow going
From the simple point of view, the only difference between

    system( "pause" );

and

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

is only a little typing.

The difference, then, is in the merits of doing it one way or another. Since the system() method is a positive security hole, among other things, it is better just to learn it the right way.
Who cares, seriously?

Some people like doing things the right way that's all. We could all call you codingshirk too. You'd still know what we mean and this is just a forum so "who cares"? It would still be wrong though...
For instance, it is wrong to put a space in my username. The space is there to satisfy the 3-char username minimum limit. You could call me L B, but it's still wrong, even though it looks right.

Things aren't always what they look like. system("pause") looks right, but it's wrong ;)

Maybe you just see no evil? I think cnoeval agrees.
Last edited on
Topic archived. No new replies allowed.