This simple section of my program isn't working as I expected.
It never actually wait for me to enter a character, why is this so?Code:printf("Make a move (A,B,L,R): "); move = getchar(); printf("\n move = %c\n", move);
This simple section of my program isn't working as I expected.
It never actually wait for me to enter a character, why is this so?Code:printf("Make a move (A,B,L,R): "); move = getchar(); printf("\n move = %c\n", move);
getchar gets a character from the keyboard buffer. If you've already got input in the keyboard buffer waiting for it, then it's not going to hang around waiting for more.
Most likely a newline. You could find out this way:
This will display the ASCII value of the character read in from the buffer.Code:printf("Make a move (A,B,L,R): "); move = getchar(); printf("\n move = %c (ascii %d)\n", move, move);
Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion
If it's 10, that's '\n'. You could try:
Before you prompt for the move. In any case, it is important you understand what the problem is here or it will only plague you further.Code:while (getchar() == '\n');
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Sorry but keyboard buffer? I'm not sure what that is.
The ASCII being returned is actually -1, which isn't appearing on that chart. Ok so I should do something like
In order to avoid this -1 thing, any new lines and spaces.Code:while ( (getchar() == -1) && (getchar() == '\n') && (getchar() == ' ") ) { move = getchar(); }
That means you have bigger problems. What are you typing right before this code (if anything)? Are you redirecting input from a file? Context.
Yes it's input from a file, but it could also equivalently be input from the user. If there are any other questions you need answering, just ask. I'd post the code too but it's about 300 lines long so I doubt you want to see all that.
Ok so I'm not getting an ascii of -1 anymore, but it keeps posting 32 (space) when I use a file input:
So how could I get it to actually read the input from the user? Oh by the way, when I enter the file in manually through the prompt, it actually stops to wait for my input but it still says that the char move variable is different to what I entered.Code:char makeMove() { char move = ' '; printf("Make a move (A,B,L,R): "); while ( (getchar() == -1) && (getchar() == '\n') && (getchar() == ' ') ) { move = getchar(); } printf("\n move = %c (ascii %d)\n", move, move); return move; }
If you're redirecting input from a file, that is:
then you can't also get input from the user. -1 would correspond to EOF which would mean that your file is empty.Code:myprog < input
Also your while statement is a very interesting thing, and I doubt that it's quite what you want. For instance, if the first getchar is anything at all, then move is never changed (because you never assigned the getchar result to move), so it is still the space you set it to be at the very top.
-1 indicates an error or EOF (end of file). Normally that will not happen with stdin, unless you have redirected a file into it. If it is not EOF, errno is set:
But that is probably not the case.Code:#include <errno.h> errno = 0; // clear any previous error move = getchar(); if (move == -1 && errno) { // genuine error perror("getchar() failed"); // will add a string explaining the error }
The prompt will still appear unless you have also redirected stdout. However, it is not really waiting for you or making use of keyboard input if you have piped a file into stdin -- that's an illusion.
Maybe you should explain exactly how you are piping the file in.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
So
is completely ignored? Maybe I should be using scanf instead then.Code:move = getchar();
It's completely ignored only because you surrounded it with a while loop that cannot possibly actually run (since the loop condition must needs be false). Perhaps you meant something like
(Note the assigning of move so that the character is actually read, plus the change in the while loop to make it at least feasibly a loop.)Code:move = getchar(); while (move == '\n' || move == ' ') move = getchar();
Oh that makes perfect sense! Thankyou for your help.
Last edited by CommonTater; 09-06-2011 at 08:38 AM. Reason: Wow... I REALLY need that second cup of coffee this morning!