C Have to Press Enter to Continue
-
11-30-2003 #1
Registered User
Press enter to continue... beginner code
i'm doing a printf, than i want to stop and ask the user to press enter to continue (that would return him/her to a main menu.... now i got it to stop and wait for input, but when pressing enter it only gives me a new line... here's the code:
Code:
void pause(void){ printf("Press ENTER to continue.... \n"); while(getc() != EOF) /* nothing happens */; }
-
11-30-2003 #2
& the hat of GPL slaying
EOF is used for file manipulation, rarely for use with standard input. Try using getchar() and comparing that to \n
-
11-30-2003 #3
Code Goddess
EOF and '\n' are not the same. Try something more like this:
Code:
void pause ( void ) { printf ( "Press enter to continue..." ); fflush ( stdout ); getchar(); }
My best code is written with the delete key.
-
11-30-2003 #4
Registered User
Great it works like so:
Code:
printf("Press enter to continue: "); while(getchar() != '\n');
more reading for me.
Thanks guys!
-
11-30-2003 #5
Casual Visitor
probably the semi on the while... sort of like if(something);
try
Code:
while(getchar() != '\n'){}
I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P
-
11-30-2003 #6
& the hat of GPL slaying
probably the semi on the while... sort of like if(something);
A parse error at the end of the file would seem to indicate a extra or missing
somewhere.Note: the javascript that forces you to put the bracket in code tags is really annoying.
-
12-01-2003 #7
Registered User
yeah, i found that one too... in another function... it's so weird whe it does not give you any clue as to where to look for the error
-
12-01-2003 #8
Casual Visitor
Originally posted by rox
yeah, i found that one too... in another function... it's so weird whe it does not give you any clue as to where to look for the errorI haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P
-
12-01-2003 #9
End Of Line
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
-
12-01-2003 #10
Registered User
fflush(stdout);
quoteI was wondering why you can do this for stdout but not stdin I have read the faqs and it is a little too technical for me. Maybe because I don't understand what a stream is really well. Could someone could give me a concrete down to earth answer? Thanx in advance
-
12-01-2003 #11
and the hat of int overfl
> I was wondering why you can do this for stdout but not stdin
The standard says fflush() is applicable to
- output streams (stdout is an output stream)
- update streams where the last operation was a write.Since stdin is never either of those cases, you can never use fflush on stdin
On stdout, it means that whatever characters have been written so far will be pushed out of the buffering in the standard library and onto the operating system.
stdout is flushed automatically if
- you print a newline character
- the internal buffer becomes full.The typical use is making sure a prompt appears on the stdout, when that prompt doesn't contain a newline
printf( "Enter name > " );
fflush( stdout );In some environments, if you omit the fflush(), you may not see the prompt at the moment you expect to see it.
-
12-01-2003 #12
Code Goddess
>I was wondering why you can do this for stdout but not stdin
Because requiring a single function to perform two completely different actions is illogical. Think about it: flushing an output stream means to send buffered data to its final location. Flushing an input stream means to discard the remaining buffered data. These are inherently incompatible operations, so using fflush for both makes little sense.Of course, the simpler "why" is that the standard says so, and smart programmers follow the standard.
My best code is written with the delete key.
-
12-01-2003 #13
Been here, done that.
Originally posted by Salem
Since stdin is never either of those cases, you can never use fflush on stdin
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
-
12-01-2003 #14
Code Goddess
>As you well know a few compilers have extended the standard
Unless we say otherwise, we're talking about standard C when we talk about things you can or cannot do and things that are or are not possible.>Don't say "never" Salem.
This is true, the standard committee may decide to add that functionality. It isn't likely, but not outside the bounds of reason.My best code is written with the delete key.
-
12-02-2003 #15
Been here, done that.
So you're saying,
Never use getch().
Never use graphics packages.
Never use conio.h nor ncurses.h.
Never use anything unless it conforms 100% to the standard (which one? C89, C99?)Come on, you know better.
In some cases to accomplish a specific task and you have the compiler available, you must use extensions. And this board should help even with those extensions. But, where possible, describe the "way of the standard."
I believe the statement "Since stdin is never either of those cases, you can never use fflush on stdin" should be modified to "Since stdin is never either of those cases, you should not rely on fflush on stdin, it does not conform to the standards."
Just my 2¢
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
trouettedentoory64.blogspot.com
Source: https://cboard.cprogramming.com/c-programming/47799-press-enter-continue-beginner-code.html