Thread: Problems reading in character

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    83

    Problems reading in character

    Hi,

    I just took up c programming and like it so far. Now I got a problem reading in a character in a program. The program looks like this:


    insert
    Code:
    #include <stdio.h>
    
    main()
    {
        char c1, c2;
        printf("Letter 1: ");
        c1 = getchar();
        
        printf("Letter 2: \n");
        c2 = getchar();
        
        printf("c1 %c, c2 %c", c1, c2);
        if (c1 < c2)
            printf("Letter 1 is before Letter 2.\n");
        else
            printf("Letter 1 is after Letter 2\n");
    }
    The program reads in c1, but ignores c2. Anyone knows why?
    I have the same problem using scanf (see below).


    insert
    Code:
    #include <stdio.h>
    
    main()
    {
        char c1, c2;
        
        printf("Letter 1: ");
        scanf("%c", &c1);
        
        printf("Letter 2: ");
        scanf("%c", &c2);
        
        printf("\nc1 %c, c2 %c", c1, c2);
        if (c1 < c2)
            printf("\nLetter 1 is before Letter 2.\n");
        else
            printf("\nLetter 1 is after Letter 2\n");
    }
    Any help is appreciated. Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,514
    Quote Originally Posted by jjohan
    The program reads in c1, but ignores c2. Anyone knows why?
    When you entered Letter 1, the newline from the enter was read and stored in c2. There are few ways to deal with this, one solution being to read that newline and discard it before reading Letter 2.

    By the way, main() should be int main(void). You might also want to explicitly return 0; from the main function, though this is optional as a special case for main if you're compiling with respect to C99 or later.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Using scanf with the %c format specifier is equivalent to using getchar*. If you think about what you type, you type a letter (e.g. 'x'), then you hit the enter key. The enter key sends a newline character to the program, so your first getchar gets the 'x', the second one gets the newline ('\n'). Take a look at this link for more info and one way to clear/flush the input buffer up to the newline -- note, fflush(stdin) is NOT the way.

    And I would stick with the getchar version, it's a bit clearer, IMO. Note, however that getchar returns an int (see here) -- this is so it can return all possible chars, as well as special values like EOF. Thus you should declare c1 and c2 to be of type int. And if you really wanted to be thorough, you should check both c1 and c2 for EOF before you compare.

    * I'm not feeling 100% sure that the two are completely equivalent, so as a caveat, there may be minor differences. But as far as your issue is concerned, they're the same.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    13
    I tried a
    #include <windows.h>
    and
    Sleep(2000); // wait 2 seconds

    between c1 and c2 input

    and found it was unlikely that a character would go in as c1 and "enter" command would go into c2 but I could be wrong. please explain if you can correct me.


    Then I found a line from this page very useful that fixed the problem
    FAQ > Flush the input buffer - Cprogramming.com

    FAQ > Flush the input buffer

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Sleeping won't help. The enter character is in the input buffer once you press enter, and it waits there until it is removed by a function like getchar, scanf, fgets, etc.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by 4eric View Post
    ...and found it was unlikely that a character would go in as c1 and "enter" command would go into c2 but I could be wrong. please explain if you can correct me.
    Run this, and follow the instructions:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int c1, c2;
    
        printf("Enter a single letter: ");
        fflush(stdout);
    
        c1 = getchar();
        c2 = getchar();
    
        printf("You entered: %c\n",c1);
    
        if(c2 == '\n')
            printf("c2 contains a newline\n");
    
        return 0;
    }

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    3,749
    Quote Originally Posted by 4eric View Post
    and found it was unlikely that a character would go in as c1 and "enter" command would go into c2 but I could be wrong. please explain if you can correct me.
    The input is line buffered by default is nearly all C programming systems by default.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Sep 2014
    Posts
    13
    thank you very much read the posting

    Sleeping won't help. The enter character is in the input buffer once you press enter, and it waits there until it is removed by a function like getchar, scanf, fgets, etc.

    I am out of this web forum
    Goodbye

  9. #9
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    Thank you all for all help. Appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading X character from a file
    By fallout01 in forum C Programming
    Replies: 3
    Last Post: 02-04-2008, 10:58 PM
  2. reading in one character at a time
    By isotope1 in forum C++ Programming
    Replies: 2
    Last Post: 01-26-2006, 12:55 PM
  3. reading a character
    By Calavera in forum C Programming
    Replies: 6
    Last Post: 11-23-2004, 12:25 PM
  4. Problems with character input
    By OmnipotentCow in forum C Programming
    Replies: 19
    Last Post: 06-20-2003, 03:39 PM
  5. about reading character from a file
    By Abdi in forum C Programming
    Replies: 3
    Last Post: 05-03-2002, 11:25 AM