A common way of pausing the program is to make use of the standard function getchar().
scanf() behaviour will mess up your ability to pause the program with getchar() sometimes. Read about it here .
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
But if you still want to use some weird input methods, like cin, then you have to put fflush (stdin); before getchar.
Best way is to avoid any `weird input methods' and do not start even
usingfflush( stdin ) at all. Here is why not to use it.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
>Yes that's right, effect of fflush is undefined for stdin and it of course must not be used.
Then why did you tell the OP to use it?
>But if they already do things insecurely, then they must use other uncertain things to compensate it
There are better ways to discard pending characters than to introduce undefined behavior. The easiest is a simple loop:
void jsw_flush ( void )
{
int ch;
do
ch = getchar();
while ( ch != EOF && ch != '\n' );
clearerr ( stdin );
}
If you can't suggest one of the better options, you're better off sticking to the original advice of always using fgets.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>I usually use FILENAME_MAX as size
I prefer BUFSIZ, personally.
>then the next input would consume it, and would most likely cause a wrong input.
That's a quality of implementation issue. You write your code so that it can intelligently handle excessively long lines.
>The standard doesn't guarantee either, that the input
>stream would be flushed when the program exits
I'm not sure where you're going this.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
You can also use system("PAUSE"),
NO!!! Here's why
DevC++ puts this in for you automatically,
Only if you don't correct the template, which IMAO everyone should do.i'm not sure about linux at all since i don't need to use it on Linux :) And here's one major reason why you don't use it!
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I repeat.. " It's not good resourcefully if you use this method"..
I repeat... what Mr. WaltP said: "NO!!! Here's why "
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
>NO!!! Here's why
No. Most people who do this are just beginners. It is often important for them to get it working with minimal effort so they can concentrate on learning to program.
When the time is right they can learn about it's pitfalls. Even using cin.get() etc is plagued with gotchas, because clearing the input stream is not that straight-forward.
Of course if you ain't a newbie, and you've planning to release your code into the public domain, you should strive to adopt good habits which ensure maximum portability on different platforms and various compilers.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
>NO!!! Here's why
No. Most people who do this are just beginners. It is often important for them to get it working with minimal effort so they can concentrate on learning to program.
I respectfully disagree. It's that understanding sympathy for `working with minimal effort' that gets us in trouble as beginners. In the name of `just for beginners' we get burried into a pile of worthless practices hard to break later on. If you are committed to learn to program in C, work hard and learn it right from the beginning.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
>In the name of `just for beginners' we get burried into a pile of worthless practices hard to break later on.
We are not talking about you. You are specially challenged, learning new habits will be difficult for you. Everyone else learns good habits in time.
Learning to pause the console window is not vitally important for newbies to grasp. Re-read my original post for an explanation.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
>In the name of `just for beginners' we get burried into a pile of worthless practices hard to break later on.
We are not talking about you. You are specially challenged, learning new habits will be difficult for you. Everyone else learns good habits in time.
I always give you the benefit of the doubt. However uncalled comments like this does remove all doubts about your character.Learning to pause the console window is not vitally important for newbies to grasp. Re-read my original post for an explanation.
I don't need to re-read your original post. I already gave the OP the answer of how he could pause the program without the need to use any none portable nor compiler dependent function.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218