Thread: Working with char

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    Working with char

    This simple section of my program isn't working as I expected.

    Code:
    printf("Make a move (A,B,L,R): ");
    move = getchar();
    printf("\n move = %c\n", move);
    It never actually wait for me to enter a character, why is this so?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    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:

    Code:
    printf("Make a move (A,B,L,R): ");
    move = getchar();
    printf("\n move = %c (ascii %d)\n", move, move);
    This will display the ASCII value of the character read in from the buffer.

    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    If it's 10, that's '\n'. You could try:

    Code:
    while (getchar() == '\n');
    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.
    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

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    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

    Code:
    while ( (getchar() == -1) && (getchar() == '\n') && (getchar() == ' ") ) {
        move = getchar();
    }
    In order to avoid this -1 thing, any new lines and spaces.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That means you have bigger problems. What are you typing right before this code (if anything)? Are you redirecting input from a file? Context.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    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.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Ok so I'm not getting an ascii of -1 anymore, but it keeps posting 32 (space) when I use a file input:

    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;
    }
    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.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're redirecting input from a file, that is:
    Code:
    myprog < input
    then you can't also get input from the user. -1 would correspond to EOF which would mean that your file is empty.
    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.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    -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:

    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
    }
    But that is probably not the case.

    Quote Originally Posted by Mentallic View Post
    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.
    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

  10. #10
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    So

    Code:
    move = getchar();
    is completely ignored? Maybe I should be using scanf instead then.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Mentallic View Post
    So

    Code:
    move = getchar();
    is completely ignored? Maybe I should be using scanf instead then.
    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
    Code:
    move = getchar();
    while (move == '\n' || move == ' ')
        move = getchar();
    (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.)

  12. #12
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Oh that makes perfect sense! Thankyou for your help.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mentallic View Post
    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

    Code:
    while ( (getchar() == -1) && (getchar() == '\n') && (getchar() == ' ") ) {
        move = getchar();
    }
    In order to avoid this -1 thing, any new lines and spaces.
    Code:
    while ( (getchar() != -1) && (getchar() != '\n') && (getchar() != ' ') ) {
        move = getchar();
    }
    But that still won't solve the problem... You need to take mk27's solution from message 3, right before calling getchar();
    Last edited by CommonTater; 09-06-2011 at 08:38 AM. Reason: Wow... I REALLY need that second cup of coffee this morning!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-18-2008, 02:59 AM
  2. Copying one char* to another not working?
    By Saggio in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2006, 06:03 AM
  3. map<char*, word> not working
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 03-13-2006, 02:02 AM
  4. working with char
    By kennny2004 in forum C++ Programming
    Replies: 20
    Last Post: 11-07-2005, 06:33 AM
  5. If(char != char) is not working... help?
    By Unregistered in forum Game Programming
    Replies: 4
    Last Post: 06-20-2002, 12:48 PM