Before my program quits, I would like a user to press enter before the program ends. I want to do something similiar to simple:
getchar();
but with using scanf();
Is there anyway of doing it like that ?
Regards,
apacz.
Before my program quits, I would like a user to press enter before the program ends. I want to do something similiar to simple:
getchar();
but with using scanf();
Is there anyway of doing it like that ?
Regards,
apacz.
>Is there anyway of doing it like that ?
I suppose:
But why do you what to use scanf? It's for formatted input, and this task implies unformatted input. getchar is better suited in principle and in practice.Code:scanf ( "%*c" );
My best code is written with the delete key.
because I have sucha code:
and it doesn't work neither if there is '\n' in scanf or not.Code:#include <stdio.h> int main() { int s; scanf("%d\n", &s); getchar(); return 0; }
Which is why we usually suggest fgets() + sscanf() rather than trying to cope with the randomness which simple scanf() leaves behind.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
>and it doesn't work neither if there is '\n' in scanf or not
Using scanf to read a single character won't save you from that either because you're simulating the functionality of getchar. The effect is identical. Now, the proper solution is to use fgets for reading a line and then parsing the line in memory. This avoids the "whitespace stuck in the stream" problem:
Alternatively, you can "flush" the stream with an ugly hack:Code:#include <stdio.h> #include <stdlib.h> int main ( void ) { char buf[BUFSIZ]; int x; printf ( "Enter a whole number: " ); fflush ( stdout ); if ( fgets ( buf, sizeof buf, stdin ) != NULL ) { if ( sscanf ( buf, "%d", &x ) != 1 ) { fprintf ( stderr, "Invalid input\n" ); return EXIT_FAILURE; } printf ( "You entered: %d\n", x ); } getchar(); return EXIT_SUCCESS; }
I don't recommend that.Code:int ch; while ( ( ch = getchar() ) != EOF && ch != '\n' ) ;
My best code is written with the delete key.
Thank you very much .
ya..there is a way..Originally Posted by apacz
just decalre a variable of type char and use scanf to read it..i think it will work..u can also do with int
You don't need a variable. There's a format specifier modifer that allows the discarding of what you read. However, your solution wouldn't work any better than just a single getchar call. More than one character in the stream would have the same problem. There are format specifiers that allow you to read a everything up to a newline, so you'd need that.
It doesn't make it a good solution. But you could do it.
Quzah.
Hope is the first step on the road to disappointment.
Instead of using getchar(), there is another way to make the program stop before quitting and wait for the user to type anykey.
The answer is... using : system("pause"); at the end of your code.
Just try!
No, don't EVER use system() anything if you can help it. . .
>The answer is... using : system("pause"); at the end of your code.
This isn't recommended because it's neither portable nor secure.
My best code is written with the delete key.
Yeah... Windows specific functionOriginally Posted by Prelude
"The Internet treats censorship as damage and routes around it." - John Gilmore
For more information on system("pause") and the alternatives, read these FAQs:
- FAQ > How do I... (Level 1) > Stop my Windows Console from disappearing everytime I run my program?
- FAQ > How do I... (Level 1) > How do I get my program to wait for a keypress?
BTW, using getchar() is greatly preferable to using system("pause"), although you may have to put it into a loop (as the second FAQ link does) if there's something in the input buffer.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.