Re: scanf dilemma for beginner

Discussion in 'C Programming' started by Greg P., Aug 29, 2003.

  1. Greg P.

    Greg P. Guest

    I modified your code a little bit. You needed some buffer flushes to sync
    your I/O with the OS. I also changed your if statements into a switch:
    ------------------------
    #include <stdio.h>

    int main(void)
    {

    int num = 0;
    int ch = 0;
    int i;

    printf("Enter decimal number (or zero for list): ");
    fflush(stdout);
    fflush(stdin);
    scanf("%d",&num);

    if (num == 0)
    {
    for (i = 1; i <= 100; ++i)
    printf("\nDecimal: %3d\tOctal: %3o\tHex: %3x", i , i, i);
    return(1);
    }

    printf("\nWould you like (h)ex or (o)ctal? ");
    fflush(stdout);
    fflush(stdin);
    ch = getchar();

    switch(ch)
    {
    case 'h':
    case 'H':
    printf("\n\n%d in Hexadecimal is: %x", num, num);
    break;

    case 'o':
    case 'O':
    printf("\n\n%d in Octal is: %o", num, num);
    break;

    default:
    printf("\nSorry, but there was some sort of error.
    Cheers!\n\n");
    break;
    }
    return 0;
    }
    Greg P., Aug 29, 2003
    #1
    1. Advertising

  2. Greg P.

    Mac Guest

    On Fri, 29 Aug 2003 01:32:23 +0000, Greg P. wrote:

    > I modified your code a little bit. You needed some buffer flushes to sync
    > your I/O with the OS. I also changed your if statements into a switch:
    > ------------------------
    > #include <stdio.h>
    >
    > int main(void)
    > {
    >
    > int num = 0;
    > int ch = 0;
    > int i;
    >
    > printf("Enter decimal number (or zero for list): ");
    > fflush(stdout);
    > fflush(stdin);


    Don't flush input streams. This is UB.

    [snip]

    Mac
    --
    Mac, Aug 29, 2003
    #2
    1. Advertising

  3. Greg P.

    Greg P. Guest

    "Mac" <> wrote in message
    news:p...
    | Don't flush input streams. This is UB.

    I thought the same thing before I typed it, but without them the program
    does not work properly (in this case).
    Greg P., Aug 29, 2003
    #3
  4. Greg P.

    Jirka Klaue Guest

    Greg P. wrote:

    > "Mac" <> wrote in message
    > news:p...
    > | Don't flush input streams. This is UB.
    >
    > I thought the same thing before I typed it, but without them the program
    > does not work properly (in this case).


    No, it doesn't. The reason is that scanf leaves the '\n' in the input stream.
    It can be removed with a simple getchar() or, if you want to be sure with

    while ((c = getchar()) != '\n' && c != EOF)
    ;

    Jirka
    Jirka Klaue, Aug 29, 2003
    #4
  5. Greg P.

    Jirka Klaue Guest

    Greg P. wrote:
    > "Jirka Klaue" <-berlin.de> wrote:
    > | No, it doesn't. The reason is that scanf leaves the '\n' in the input
    > stream.
    > | It can be removed with a simple getchar() or, if you want to be sure with
    > |
    > | while ((c = getchar()) != '\n' && c != EOF)
    > | ;
    >
    > I was thinking: since flushing input is obviously not a good idea, would a
    > scanf() function suffice:
    >
    > scanf("\n%c", &char);

    ^^^^
    This is not a good idea. ;-)

    > I mean, do all implementations (or at least most) leave the newline
    > character for the next input routine? If that's so could we then just add
    > '\n' to the scanf format string as above?


    scanf(" %c", &c); discards leading whitespace.

    But ...
    .... what if the user entered 42xxx before?

    Jirka
    Jirka Klaue, Aug 29, 2003
    #5
  6. Greg P.

    Greg P. Guest

    "Jirka Klaue" <-berlin.de> wrote in message
    news:bin1ui$gbr$-Berlin.DE...
    | But ...
    | ... what if the user entered 42xxx before?

    Ah! Very perseverant. I did not think of that <g>
    Greg P., Aug 29, 2003
    #6
  7. On Fri, 29 Aug 2003, Greg P. wrote:
    >
    > "Jirka Klaue" <-berlin.de> wrote...
    > |
    > | No, it doesn't. The reason is that scanf leaves the '\n' in the input
    > | stream. It can be removed with a simple getchar() or, if you want to
    > | be sure, with
    > |
    > | while ((c = getchar()) != '\n' && c != EOF)
    > | ;
    >
    > I was thinking: since flushing input is obviously not a good idea, would
    > a scanf() function suffice:
    >
    > scanf("\n%c", &char);


    The scanf() format corresponding to Jirka's algorithm is

    scanf("%*[^\n]\n");

    This is definitely the way to do it, if you're looking for a one-line
    approach and don't mind the obscurity.

    > I mean, do all implementations (or at least most) leave the newline
    > character for the next input routine?


    Yes, of course. All conforming implementations, that is.

    > If that's so could we then just add
    > '\n' to the scanf format string as above?


    You could, but what if the user *didn't* enter '\n'? Then the scanf()
    fails, and you're still left with garbage in the input buffer.

    -Arthur
    Arthur J. O'Dwyer, Aug 29, 2003
    #7
  8. Greg P.

    Kevin Easton Guest

    Arthur J. O'Dwyer <> wrote:
    >
    > On Fri, 29 Aug 2003, Greg P. wrote:
    >>
    >> "Jirka Klaue" <-berlin.de> wrote...
    >> |
    >> | No, it doesn't. The reason is that scanf leaves the '\n' in the input
    >> | stream. It can be removed with a simple getchar() or, if you want to
    >> | be sure, with
    >> |
    >> | while ((c = getchar()) != '\n' && c != EOF)
    >> | ;
    >>
    >> I was thinking: since flushing input is obviously not a good idea, would
    >> a scanf() function suffice:
    >>
    >> scanf("\n%c", &char);

    >
    > The scanf() format corresponding to Jirka's algorithm is
    >
    > scanf("%*[^\n]\n");
    >
    > This is definitely the way to do it, if you're looking for a one-line
    > approach and don't mind the obscurity.


    I don't think that works - that last \n is treated the same as any other
    whitespace (skip any amount of whitespace) - you want %*c there, to read
    and discard the single newline character.

    - Kevin.
    Kevin Easton, Aug 30, 2003
    #8
    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. Chris
    Replies:
    0
    Views:
    407
    Chris
    Nov 10, 2003
  2. Ian Roddis

    Re: scanf dilemma for beginner

    Ian Roddis, Aug 29, 2003, in forum: C Programming
    Replies:
    0
    Views:
    434
    Ian Roddis
    Aug 29, 2003
  3. chris

    Re: scanf dilemma for beginner

    chris, Aug 29, 2003, in forum: C Programming
    Replies:
    11
    Views:
    640
    The Real OS/2 Guy
    Sep 1, 2003
  4. =?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,369
    those who know me have no need of my name
    Apr 3, 2006
  5. =?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:
    671
    Richard Bos
    May 2, 2006
Loading...

Share This Page