A problem with reading in a character.

Discussion in 'C Programming' started by Gus Tabares, Nov 5, 2003.

  1. Gus Tabares

    Gus Tabares Guest

    Hello,

    I'm having trouble reading in a character. Here is a snippet of code:

    int num;
    char character;

    printf("Enter a number: ");
    scanf("%d", &num);
    printf("You entered %d.\n", num);

    printf("Enter a character: ");
    scanf("%c", &character);
    printf("You entered %c.\n", character);

    When I run this, I get:

    Enter a number: 3
    You entered 3.
    Enter a character: You entered
    ..

    I'm not sure why I'm unable to enter in a character before the last
    printf is executed. I'm also tried this with getchar(), with the same
    result.

    Any help is appreciated.
     
    Gus Tabares, Nov 5, 2003
    #1
    1. Advertising

  2. "Gus Tabares" <> schrieb im Newsbeitrag
    news:...
    > Hello,
    >
    > I'm having trouble reading in a character. Here is a snippet of code:
    >
    > int num;
    > char character;
    >
    > printf("Enter a number: ");
    > scanf("%d", &num);
    > printf("You entered %d.\n", num);
    >
    > printf("Enter a character: ");
    > scanf("%c", &character);
    > printf("You entered %c.\n", character);
    >
    > When I run this, I get:
    >
    > Enter a number: 3
    > You entered 3.
    > Enter a character: You entered
    > .

    ^^^^

    Hint:
    You have no trouble reading a character, you just read the wrong character
    :)
    Why is the last dot a line below the prompt?
    Can it be, that the printed character is '\n' ?
    What exactly was your first input, and where did your first scanf() stop
    consuming input?
    HTH
    Robert
     
    Robert Stankowic, Nov 5, 2003
    #2
    1. Advertising

  3. (Gus Tabares) writes:
    > Hello,
    >
    > I'm having trouble reading in a character. Here is a snippet of code:
    >
    > int num;
    > char character;
    >
    > printf("Enter a number: ");
    > scanf("%d", &num);
    > printf("You entered %d.\n", num);
    >
    > printf("Enter a character: ");
    > scanf("%c", &character);
    > printf("You entered %c.\n", character);
    >
    > When I run this, I get:
    >
    > Enter a number: 3
    > You entered 3.
    > Enter a character: You entered
    > .


    See the C FAQ at <http://www.eskimo.com/~scs/C-faq/top.html>,
    particularly question 12.18. I recommend browsing around the rest of
    the FAQ while you're there.

    (BTW, 12.18 refers to the gets() function without condemning it, but
    see also 12.23.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
     
    Keith Thompson, Nov 5, 2003
    #3
  4. Gus Tabares

    Anupam Guest

    (Gus Tabares) wrote in message news:<>...
    [snip]
    > printf("Enter a number: ");
    > scanf("%d", &num);
    > printf("You entered %d.\n", num);
    >
    > printf("Enter a character: ");
    > scanf("%c", &character);
    > printf("You entered %c.\n", character);
    >
    > When I run this, I get:
    >
    > Enter a number: 3

    Ok what you are really doing here is entering 2 characters, not one.
    These are '3' and '\n'. Whenever scanf gets a '\n' it will stop
    scanning its input. However it leaves '\n' inside the input queue.
    So when it started reading from the input queue it read 3 ... then
    said .. ok this is valid... lets see what the next character is ...
    its '\n'... That means that I must stop scanning. The '3' which it
    read was duly converted to an integer and stored in num.

    > You entered 3.


    Now it prints out that num.

    Next line is the crucial one. Remember that it removed '3' from the
    input but '\n' still remains.

    So when the second scanf looks for an input it finds '\n' and stops
    immediatly and the '\n' is read into character.

    That is why when you print out the next line, there is a newline
    between the "You entered" and the "." .

    > Enter a character: You entered
    > .
     
    Anupam, Nov 5, 2003
    #4
  5. "Anupam" <> wrote in message
    news:...
    > (Gus Tabares) wrote in message

    news:<>...
    > [snip]
    > > printf("Enter a number: ");
    > > scanf("%d", &num);
    > > printf("You entered %d.\n", num);
    > >
    > > printf("Enter a character: ");
    > > scanf("%c", &character);
    > > printf("You entered %c.\n", character);
    > >
    > > When I run this, I get:
    > >
    > > Enter a number: 3

    > Ok what you are really doing here is entering 2 characters, not one.
    > These are '3' and '\n'. Whenever scanf gets a '\n' it will stop
    > scanning its input. However it leaves '\n' inside the input queue.
    > So when it started reading from the input queue it read 3 ... then
    > said .. ok this is valid... lets see what the next character is ...
    > its '\n'... That means that I must stop scanning. The '3' which it
    > read was duly converted to an integer and stored in num.
    >
    > > You entered 3.

    >
    > Now it prints out that num.
    >
    > Next line is the crucial one. Remember that it removed '3' from the
    > input but '\n' still remains.
    >
    > So when the second scanf looks for an input it finds '\n' and stops
    > immediatly and the '\n' is read into character.
    >
    > That is why when you print out the next line, there is a newline
    > between the "You entered" and the "." .
    >
    > > Enter a character: You entered
    > > .


    I am aware that there are many solutions to this (one I can think of is
    including a getch() in between the 2 scanfs). However, I am not satisfied
    with this method and the extra getch() expects some comments to make the
    code more readable.

    My question is:

    Is there any library function that can flush the standard input at a user
    discretion. I read somewhere that fflush(stdin) is an undefined behavior. am
    I correct? what is the best solution for this?

    Thanks and Regards,
    Praveen Kumar
     
    sahukar praveen, Nov 6, 2003
    #5
  6. Gus Tabares

    Anupam Guest

    "sahukar praveen" <> wrote in message news:<>...

    [snip]

    > I am aware that there are many solutions to this (one I can think of is
    > including a getch() in between the 2 scanfs). However, I am not satisfied
    > with this method and the extra getch() expects some comments to make the
    > code more readable.


    The best way would be to write your own function which keeps reading
    characters in a loop using _getchar()_ until you get an EOF. However
    be sure to check that it was indeed the end of the stream using feof()
    .... it might have been a read error. You would be ill-advised to use
    getch(). It is not standardised.

    >
    > My question is:
    >
    > Is there any library function that can flush the standard input at a user
    > discretion. I read somewhere that fflush(stdin) is an undefined behavior. am
    > I correct? what is the best solution for this?


    Correct, there is no way as per the standard ( as per my knowledge
    of the standard, which may not be perfect ) to remove the characters
    waiting at an input queue without reading them and without invoking
    UB. The only real solution is the one you mention which is to eat up
    the intervening characters by reading them without using them . fflush
    is not defined on input streams.

    >
    > Thanks and Regards,
    > Praveen Kumar
     
    Anupam, Nov 6, 2003
    #6
  7. Gus Tabares

    CBFalconer Guest

    sahukar praveen wrote:
    >

    .... snip ...
    >
    > I am aware that there are many solutions to this (one I can think
    > of is including a getch() in between the 2 scanfs). However, I am
    > not satisfied with this method and the extra getch() expects some
    > comments to make the code more readable.
    >
    > My question is:
    >
    > Is there any library function that can flush the standard input
    > at a user discretion. I read somewhere that fflush(stdin) is an
    > undefined behavior. am I correct? what is the best solution for
    > this?


    scanf is not generally suitable for interactive input. You have
    two basic choices. Input a complete line and parse it with
    sscanf, or use getc and ungetc and your own conversion routines
    from stream input. The first has the problem of reading the
    complete line, of unknown length, and fgets is not completely
    suitable. The second can be highly efficient, but requires you
    write all the conversion routines correctly. My ggets can help
    with the first, as I pointed out elsewhere.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Nov 6, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    904
    Roedy Green
    Oct 27, 2005
  2. KwikRick
    Replies:
    1
    Views:
    359
    Christos TZOTZIOY Georgiou
    Aug 22, 2003
  3. dbuser
    Replies:
    8
    Views:
    516
    Thomas J. Gritzan
    Oct 10, 2005
  4. utab
    Replies:
    1
    Views:
    289
  5. wxdeveloper
    Replies:
    2
    Views:
    140
    wxdeveloper
    Aug 4, 2009
Loading...

Share This Page