Get rid of trailing newline for scanf

Discussion in 'C Programming' started by Till Crueger, Jan 16, 2005.

  1. Till Crueger

    Till Crueger Guest

    Hi,
    I have a little problem with the following code:

    #include <stdio.h>

    int main(void) {
    char input='\0';
    while(input!='q') {
    printf("Menu\n");
    fflush(stdout);
    scanf("%c",&input);
    /* do something */
    }
    }

    After I get an input there is still a trailing newline. However I always
    thought the next scanf would consume this newline. I checked with the FAQ,
    but all I found was section 12.18 which covers this issue a bit. The
    solution it gives is to only use scanf(), which in this case I do. Any
    other hints on what to do about this?
    Thanks,
    Till

    --
    Please add "Salt and Peper" to the subject line to bypass my spam filter
    Till Crueger, Jan 16, 2005
    #1
    1. Advertising

  2. Till Crueger

    S.Tobias Guest

    Till Crueger <> wrote:
    > Hi,
    > I have a little problem with the following code:


    > #include <stdio.h>


    > int main(void) {
    > char input='\0';
    > while(input!='q') {
    > printf("Menu\n");
    > fflush(stdout);
    > scanf("%c",&input);

    Try:
    scanf(" %c", &input);
    (leading space will eat whitespace in the input)
    > /* do something */
    > }
    > }


    > After I get an input there is still a trailing newline. However I always
    > thought the next scanf would consume this newline.


    scanf is a very determinate function, it won't do anything unless
    you tell it to.

    > I checked with the FAQ,
    > but all I found was section 12.18 which covers this issue a bit. The
    > solution it gives is to only use scanf(), which in this case I do. Any
    > other hints on what to do about this?


    scanf is perhaps not good for every task, especially not for user input
    (scanf is for _formatted_ input; user must be very orderly to feed it
    with the right data). To see this try:
    Menu
    abcd<Enter>
    Menu
    Menu
    Menu
    Menu
    I think in general fgets() is better. But for simple cases like above
    (where the code is not a "production" code), scanf() is all right
    too, and maybe easier to use (if you know what you're doing).

    As a learning exercise, try:
    scanf("%c ", &input); //space is after conversion specifier
    printf("entered: %c\n", (int)input);
    and explain the funny effect. Good luck!

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, Jan 16, 2005
    #2
    1. Advertising

  3. Till Crueger

    S.Tobias Guest

    S.Tobias <> wrote:
    > Till Crueger <> wrote:


    > > After I get an input there is still a trailing newline. However I always
    > > thought the next scanf would consume this newline.


    > scanf is a very determinate function, it won't do anything unless
    > you tell it to.


    Argh, just remembered something, which is a must-know.
    Whitespace in the input string is eaten at each conversion
    specification *except* at %c, %n and %[.

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, Jan 16, 2005
    #3
  4. On Sun, 16 Jan 2005 14:04:55 +0100, Till Crueger <> wrote:

    >Hi,
    >I have a little problem with the following code:
    >
    >#include <stdio.h>
    >
    >int main(void) {
    > char input='\0';
    > while(input!='q') {
    > printf("Menu\n");
    > fflush(stdout);
    > scanf("%c",&input);
    > /* do something */
    > }
    >}
    >
    >After I get an input there is still a trailing newline. However I always
    >thought the next scanf would consume this newline. I checked with the FAQ,
    >but all I found was section 12.18 which covers this issue a bit. The
    >solution it gives is to only use scanf(), which in this case I do. Any
    >other hints on what to do about this?


    There's another way of parsing input:
    char buffer[80];
    int length = 70;
    fgets(buffer, length, stdin);
    sscanf(buffer, "...");

    This will require you to parse input on a line-by-line bases, but should
    otherwise function identically.

    (I'm really suprised this doesn't appear in the FAQ - I've seen this issue
    arise enough times that it should. Either that, or it's not immediatly
    visible.)
    Raymond Martineau, Jan 16, 2005
    #4
  5. Till Crueger wrote:
    > Hi,
    > I have a little problem with the following code:
    >
    > #include <stdio.h>
    >
    > int main(void) {

    Ok.
    > char input='\0';
    > while(input!='q') {
    > printf("Menu\n");
    > fflush(stdout);
    > scanf("%c",&input);
    > /* do something */
    > }
    > }

    [...]
    Forgot something? Your `main()` function isn't returning an integer.
    Secondly, using `fgets` and `sscanf` is a better approach, as shown
    earlier by Raymond.

    Regards,
    Jonathan.

    --
    "If unsigned integers look like two's complement (signed) integers,
    it's because two's complement integers look like unsigned integers."
    -Peter Nilsson
    Jonathan Burd, Jan 17, 2005
    #5
    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. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,377
    those who know me have no need of my name
    Apr 3, 2006
  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    672
    Richard Bos
    May 2, 2006
  3. ucfcpegirl06

    Getting Rid of Trailing white spaces

    ucfcpegirl06, Jul 22, 2005, in forum: C++
    Replies:
    4
    Views:
    430
    ucfcpegirl06
    Jul 23, 2005
  4. billbell52
    Replies:
    2
    Views:
    111
    Xavier Noria
    Jul 11, 2007
  5. Rafael Oliveira

    avoid newline scanf

    Rafael Oliveira, Mar 24, 2012, in forum: C Programming
    Replies:
    6
    Views:
    738
    Don Y
    Mar 24, 2012
Loading...

Share This Page