You are not logged in.
Pages: 1
Well, I'm making a big game. I use getchar(); to wait for ENTER from user.
Like this:
getchar();
It looks pretty valid, though.
But, this really annoys me, when, getchar() doesn't pause the program and wait for user input. It just, passes...
Maybe, all the input (fgets and scanf stuff) before the getchar(); must have interfered with the getchar();, i don't really know.
Please help...
Also, if you have a better code (other than getchar()), feel free to post it.
milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/
Offline
#include <stdio.h>
int main() {
puts("Testing getchar()");
if(getchar() == 'y') puts("WIN");
else puts("FAIL");
return(0);
}
getchar() works fine here. You put so little info about what you are doing in your post there was nothing else I or anybody else could do to help you out...
You're just jealous because the voices only talk to me.
Offline
The one thing I would wonder is if your buffer isn't always empty, so there is something for getchar() to grab before the user inputs something else.
Offline
I have two answers to your question:
Use "fflush(stdin);" before you call "getchar"
Also, if you have a better code (other than getchar()), feel free to post it.
There are many different ways to get character input. Use whatever language and library works for you.
Is there a special reason you're using C?
Last edited by drcouzelis (2013-04-12 00:23:45)
Offline
I will bet you ONE HUNDRED DOLLARS you only see this problem when the call to getchar() is preceded by a scanf().
Don't use scanf for interactive programs. There are two main reasons for this:
1) scanf can't recover from malformed input. You have to get the format string right, every time, or else it just throws away whatever input it couldn't match and returns a value indicating failure. This might be fine if you're parsing a fixed-format file when poor formatting is unrecoverable anyway, but it's the exact opposite of what you want to do with user input. Use fgets() and sscanf(), fgets() and strtok(), or write your own user input routines using getchar() and putchar().
1.5) Even properly used, scanf inevitably discards input (whitespace) that can sometimes be important.
2) scanf has a nasty habit of leaving newlines in the input stream. This is fine if you never use anything but scanf, since scanf will usually skip over any whitespace characters in its eagerness to find whatever it's expecting next. But if you mix scanf with fgets/getchar, it quickly becomes a total mess trying to figure out what might or might not be left hanging out in the input stream. Especially if you do any looping -- it's quite common for the input stream to be different on the first iteration, which results in a potentially weird bug and even weirder attempts to fix it.
tl;dr -- scanf is for formatted input. User input is not formatted. //
Last edited by Trent (2013-04-12 01:12:19)
Offline
Milo64, I was wrong. There was enough info for other people to help you out...
You're just jealous because the voices only talk to me.
Offline
I have two answers to your question:
Use "fflush(stdin);" before you call "getchar"
milo64 wrote:Also, if you have a better code (other than getchar()), feel free to post it.
There are many different ways to get character input. Use whatever language and library works for you.
Is there a special reason you're using C?
I use C because it's my main programming language.
milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/
Offline
I will bet you ONE HUNDRED DOLLARS you only see this problem when the call to getchar() is preceded by a scanf().
Don't use scanf for interactive programs. There are two main reasons for this:
1) scanf can't recover from malformed input. You have to get the format string right, every time, or else it just throws away whatever input it couldn't match and returns a value indicating failure. This might be fine if you're parsing a fixed-format file when poor formatting is unrecoverable anyway, but it's the exact opposite of what you want to do with user input. Use fgets() and sscanf(), fgets() and strtok(), or write your own user input routines using getchar() and putchar().
1.5) Even properly used, scanf inevitably discards input (whitespace) that can sometimes be important.
2) scanf has a nasty habit of leaving newlines in the input stream. This is fine if you never use anything but scanf, since scanf will usually skip over any whitespace characters in its eagerness to find whatever it's expecting next. But if you mix scanf with fgets/getchar, it quickly becomes a total mess trying to figure out what might or might not be left hanging out in the input stream. Especially if you do any looping -- it's quite common for the input stream to be different on the first iteration, which results in a potentially weird bug and even weirder attempts to fix it.
tl;dr -- scanf is for formatted input. User input is not formatted. //
I forgotted to say that, I use scanf to get an integer.
milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/
Offline
at the simplest, fgets+sscanf should avoid this problem. There are also good options with (n)curses, depending how much interaction you expect.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Anyway, i use getchar() 2 times, it works, this now solved. thanks.
milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/
Offline
Okay, but when it breaks again because you decided to shotgun-debug instead of doing things right the first time, don't say I didn't warn you...
Offline
Pages: 1