Help with scanf

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sivarn

    #1

    Help with scanf

    I'm writing a program for that takes dates as input using scanf. I
    want to verify that the user is inputting a full 4 digits for the
    year. How do I do this? I know that the return value on printf is the
    number of printed characters; so if I could somehow get my year
    variable to store the leading zeros, I could just run a check:

    int dummy = 0;

    dummy = printf("%d", year);

    if (dummy != 4)
    {
    ...do something...
    }
  • Leor Zolman

    #2
    Re: Help with scanf

    On 31 Mar 2004 19:54:06 -0800, matula@tamu.edu (Sivarn) wrote:
    [color=blue]
    >I'm writing a program for that takes dates as input using scanf. I
    >want to verify that the user is inputting a full 4 digits for the
    >year. How do I do this? I know that the return value on printf is the
    >number of printed characters; so if I could somehow get my year
    >variable to store the leading zeros, I could just run a check:
    >
    >int dummy = 0;
    >
    >dummy = printf("%d", year);
    >
    >if (dummy != 4)
    >{
    > ...do something...
    >}[/color]

    Wait a minute...you started by asking about scanf, and you say you want to
    verify the input? Then I'm sure you'd rather not have to print the value
    just in order to be able to validate the user input, right?

    You certainly don't have to. Let's go ahead and use scanf, because that's
    how you say you're doing it (but scanf is essentially useless if you need
    serious validation of input. More on that later...):

    int count;
    int year;
    count = scanf("%d", &year);

    Note that you must provide the & on year, since scanf has to know /where/
    to put the value. count tells you how many "items" were scanned; it could
    be 0 if the user didn't type a number. So let's test that:

    if (count != 1)
    {
    printf("Bad input. You lose.\n");
    exit(1);
    }

    (Of course you might choose to do something other than exit here.)

    So, how to test for 4 digits? Well, the easiest way I can think of is to
    just test for a valid (or invalid) range:

    if (year < 1900 || year > 2030) /* or whatever */
    {
    printf("Bad year. You're outa here.\n");
    exit (1);
    }

    If you /really/ want "any 4 digit number", just test that it is between
    1000 and 9999.

    So that takes care of that. Now we can improve the input. sscanf is a royal
    pain to work with, because if the user just keeps pressing enter, it'll
    merrily echo blank lines till doomsday (it ignores leading whitespace,
    including newlines). And if the user presses, say, a letter when scanf is
    looking for a number, that letter persists on the input stream the /next/
    time you try to read a number, further wreaking havoc.

    Better to force a single line of input, and then process it. I like to use
    fgets to read a line into a buffer, and then sscanf to do the conversion.
    sscanf is a lot like scanf, but it reads input from a string buffer instead
    of from the standard input. Putting all of that together with some
    re-prompting logic, here's a program for you:

    #include <stdio.h>

    int main()
    {
    int year;
    char buffer[100];

    while (1)
    {
    printf("Please enter a year: ");
    fgets(buffer, 100, stdin);
    if (sscanf(buffer, "%d", &year) != 1)
    printf("that wasn't a number! Try again....\n");
    else if (year < 1900 || year > 2030)
    printf("Bad year! Must be between 1900 and 2030."
    " Try again...\n");
    else
    break;
    }

    printf("Success ! your year is %d\n", year);

    return 0;
    }

    That is something you can now expand to support as much processing on that
    line of input as you please, without running into a slew of problems you'd
    get from using just scanf.

    Good luck,
    -leor



    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

    Comment

    • Leor Zolman

      #3
      Re: Help with scanf

      On Thu, 01 Apr 2004 04:39:57 GMT, Leor Zolman <leor@bdsoft.co m> wrote:
      [color=blue]
      >So that takes care of that. Now we can improve the input. sscanf is a royal
      >pain to work with[/color]

      That should have read: /scanf/ is a royal pain...oops.
      -leor

      --
      Leor Zolman --- BD Software --- www.bdsoft.com
      On-Site Training in C/C++, Java, Perl and Unix
      C++ users: Download BD Software's free STL Error Message Decryptor at:
      An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

      Comment

      • Simon Biber

        #4
        Re: Help with scanf

        "Leor Zolman" <leor@bdsoft.co m> wrote:[color=blue]
        > if (count != 1)
        > {
        > printf("Bad input. You lose.\n");
        > exit(1);
        > }[/color]

        1 is not a portable exit code (termination status); it might have unexpected
        effects. If you want to indicate failure, do so explicitly with the macro
        EXIT_FAILURE. The only portable exit codes are 0, EXIT_SUCCESS and
        EXIT_FAILURE. The value of EXIT_SUCCESS may or may not be zero, but has the
        same effect.
        exit(EXIT_FAILU RE);
        EXIT_FAILURE is defined in <stdlib.h> which you should already have included
        for the exit function itself. It might expand to 1 or to any other value
        other than zero.

        Also, please don't post tabs to newsgroups. Indenting should be done with
        spaces.

        [...]
        [color=blue]
        > #include <stdio.h>
        >
        > int main()
        > {
        > int year;
        > char buffer[100];
        >
        > while (1)
        > {
        > printf("Please enter a year: ");[/color]

        Here you need to flush the stdout buffer to ensure that the prompt is
        displayed to the screen before you wait for user input.
        fflush(stdout);
        [color=blue]
        > fgets(buffer, 100, stdin);[/color]

        I'd avoid magic numbers and check for success/failure
        if(fgets(buffer , sizeof buffer, stdin) == NULL)
        {
        printf("There was a problem reading your input.\n");
        exit(EXIT_FAILU RE);
        }

        At this point if the user has entered a number outside the range of int, the
        behaviour of sscanf will be undefined. This is a good point to validate the
        input before continuing.
        if(strlen(buffe r) != 5 || strspn(buffer, "0123456789 \n") != 5)
        {
        printf("That wasn't a four digit number! Try again...\n");
        }
        else
        [color=blue]
        > if (sscanf(buffer, "%d", &year) != 1)
        > printf("that wasn't a number! Try again....\n");
        > else if (year < 1900 || year > 2030)
        > printf("Bad year! Must be between 1900 and 2030."
        > " Try again...\n");
        > else
        > break;
        > }
        >
        > printf("Success ! your year is %d\n", year);
        >
        > return 0;
        > }[/color]

        --
        Simon.


        Comment

        • Simon Biber

          #5
          Re: Help with scanf

          "Simon Biber" <news@ralminNOS PAM.cc> wrote:[color=blue]
          > "Leor Zolman" <leor@bdsoft.co m> wrote:[color=green]
          > > #include <stdio.h>[/color][/color]

          I forgot to add that my changes to the code below require
          #include <stdlib.h> /* for exit and EXIT_FAILURE */
          #include <string.h> /* for strlen and strspn */
          [color=blue][color=green]
          > > int main()
          > > {
          > > int year;
          > > char buffer[100];
          > >
          > > while (1)
          > > {
          > > printf("Please enter a year: ");[/color]
          >
          > Here you need to flush the stdout buffer to ensure that the prompt is
          > displayed to the screen before you wait for user input.
          > fflush(stdout);
          >[color=green]
          > > fgets(buffer, 100, stdin);[/color]
          >
          > I'd avoid magic numbers and check for success/failure
          > if(fgets(buffer , sizeof buffer, stdin) == NULL)
          > {
          > printf("There was a problem reading your input.\n");
          > exit(EXIT_FAILU RE);
          > }
          >
          > At this point if the user has entered a number outside the range of int,[/color]
          the[color=blue]
          > behaviour of sscanf will be undefined. This is a good point to validate[/color]
          the[color=blue]
          > input before continuing.
          > if(strlen(buffe r) != 5 || strspn(buffer, "0123456789 \n") != 5)
          > {
          > printf("That wasn't a four digit number! Try again...\n");
          > }
          > else
          >[color=green]
          > > if (sscanf(buffer, "%d", &year) != 1)
          > > printf("that wasn't a number! Try again....\n");
          > > else if (year < 1900 || year > 2030)
          > > printf("Bad year! Must be between 1900 and 2030."
          > > " Try again...\n");
          > > else
          > > break;
          > > }
          > >
          > > printf("Success ! your year is %d\n", year);
          > >
          > > return 0;
          > > }[/color][/color]

          --
          Simon.


          Comment

          • Arthur J. O'Dwyer

            #6
            Re: Help with scanf


            On Thu, 1 Apr 2004, Leor Zolman wrote:[color=blue]
            >
            > On 31 Mar 2004 19:54:06 -0800, matula@tamu.edu (Sivarn) wrote:[color=green]
            > >I'm writing a program for that takes dates as input using scanf. I
            > >want to verify that the user is inputting a full 4 digits for the
            > >year. How do I do this?[/color][/color]
            [color=blue]
            > Let's go ahead and use scanf, because that's how you say you're
            > doing it (but scanf is essentially useless if you need
            > serious validation of input. More on that later...):[/color]

            Now, just a minute there! I'll grant you that scanf is less than
            completely useful for *complex* validation, but *serious* validation
            can indeed be performed, if you're willing to put up with the syntax.
            I like scanf. :-)

            #include <stdio.h>
            #include <stdlib.h>
            #include <ctype.h>

            int main(void)
            {
            char buffer[5] = {0};
            char *bp;
            int k;
            int year;

            puts("Enter a 4-digit year now.");
            if (1 != scanf(" %4[0123456789]", buffer)) {
            puts("No digits entered!");
            }
            else {
            year = strtol(buffer, &bp, 10);
            if (bp != buffer+4) {
            puts("Fewer than 4 digits entered!");
            }
            else {
            printf("You entered the (valid) year %d.\n", year);
            }
            }

            k = getchar();
            if (!isspace(k)) {
            puts("You left some input at the end of your line.");
            }
            ungetc(k, stdin);

            return 0;
            }

            [color=blue]
            > So, how to test for 4 digits? Well, the easiest way I can think of is to
            > just test for a valid (or invalid) range:
            >
            > if (year < 1900 || year > 2030) /* or whatever */[/color]

            Presumably the OP's problem was that he wanted *four digits of input*,
            not merely a number in the range 1000-9999; i.e., counting leading
            zeroes.
            [color=blue]
            > So that takes care of that. Now we can improve the input. sscanf is a royal
            > pain to work with, because if the user just keeps pressing enter, it'll
            > merrily echo blank lines till doomsday (it ignores leading whitespace,
            > including newlines). And if the user presses, say, a letter when scanf is
            > looking for a number, that letter persists on the input stream the /next/
            > time you try to read a number, further wreaking havoc.[/color]

            OTOH, sometimes that's a nice behavior, not least because it's easy
            to predict its behavior. As opposed to...
            [color=blue]
            > Better to force a single line of input, and then process it.[/color]

            ....fgets, which reads either up to the first newline, *or* until its
            buffer runs out, whichever comes first. :) Not that it's impossible
            to do good stuff with fgets; it's not. But scanf's behavior isn't
            entirely illogical, either.

            HTH,
            -Arthur

            Comment

            • Leor Zolman

              #7
              Re: Help with scanf

              On Thu, 01 Apr 2004 05:29:51 GMT, "Simon Biber" <news@ralminNOS PAM.cc>
              wrote:
              [color=blue]
              >"Leor Zolman" <leor@bdsoft.co m> wrote:[color=green]
              >> if (count != 1)
              >> {
              >> printf("Bad input. You lose.\n");
              >> exit(1);
              >> }[/color]
              >
              >1 is not a portable exit code (termination status); it might have unexpected
              >effects. If you want to indicate failure, do so explicitly with the macro
              >EXIT_FAILURE . The only portable exit codes are 0, EXIT_SUCCESS and
              >EXIT_FAILURE . The value of EXIT_SUCCESS may or may not be zero, but has the
              >same effect.
              > exit(EXIT_FAILU RE);
              >EXIT_FAILURE is defined in <stdlib.h> which you should already have included
              >for the exit function itself. It might expand to 1 or to any other value
              >other than zero.[/color]

              Sorry, old habits die hard ;-)
              [color=blue]
              >
              >Also, please don't post tabs to newsgroups. Indenting should be done with
              >spaces.[/color]

              Arghh. I forgot this time; but generally I've been pretty good. In fact,
              I'd just come up with an Epsilon macro that with one keystroke a)
              untabifies my edit buffer, b) copies it into the clipboard, and c)
              re-tabifies. Now I just have to remember to /use/ it.
              [color=blue]
              >
              >[...]
              >[color=green]
              >> #include <stdio.h>
              >>
              >> int main()
              >> {
              >> int year;
              >> char buffer[100];
              >>
              >> while (1)
              >> {
              >> printf("Please enter a year: ");[/color]
              >
              >Here you need to flush the stdout buffer to ensure that the prompt is
              >displayed to the screen before you wait for user input.
              > fflush(stdout);[/color]

              Is that actually required for stdout if you then pause to read from stdin?
              Where'd I get the impression there was some sort of special case for
              that...
              [color=blue]
              >[color=green]
              >> fgets(buffer, 100, stdin);[/color]
              >
              >I'd avoid magic numbers and check for success/failure
              > if(fgets(buffer , sizeof buffer, stdin) == NULL)
              > {
              > printf("There was a problem reading your input.\n");
              > exit(EXIT_FAILU RE);
              > }
              >
              >At this point if the user has entered a number outside the range of int, the
              >behaviour of sscanf will be undefined. This is a good point to validate the
              >input before continuing.
              > if(strlen(buffe r) != 5 || strspn(buffer, "0123456789 \n") != 5)
              > {
              > printf("That wasn't a four digit number! Try again...\n");
              > }
              > else
              >[color=green]
              >> if (sscanf(buffer, "%d", &year) != 1)
              >> printf("that wasn't a number! Try again....\n");
              >> else if (year < 1900 || year > 2030)
              >> printf("Bad year! Must be between 1900 and 2030."
              >> " Try again...\n");
              >> else
              >> break;
              >> }
              >>
              >> printf("Success ! your year is %d\n", year);
              >>
              >> return 0;
              >> }[/color][/color]

              Thanks!
              -leor

              --
              Leor Zolman --- BD Software --- www.bdsoft.com
              On-Site Training in C/C++, Java, Perl and Unix
              C++ users: Download BD Software's free STL Error Message Decryptor at:
              An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

              Comment

              • Leor Zolman

                #8
                Re: Help with scanf

                On Thu, 1 Apr 2004 02:54:31 -0500 (EST), "Arthur J. O'Dwyer"
                <ajo@nospam.and rew.cmu.edu> wrote:
                [color=blue]
                >
                >On Thu, 1 Apr 2004, Leor Zolman wrote:[color=green]
                >>
                >> On 31 Mar 2004 19:54:06 -0800, matula@tamu.edu (Sivarn) wrote:[color=darkred]
                >> >I'm writing a program for that takes dates as input using scanf. I
                >> >want to verify that the user is inputting a full 4 digits for the
                >> >year. How do I do this?[/color][/color]
                >[color=green]
                >> Let's go ahead and use scanf, because that's how you say you're
                >> doing it (but scanf is essentially useless if you need
                >> serious validation of input. More on that later...):[/color]
                >
                > Now, just a minute there! I'll grant you that scanf is less than
                >completely useful for *complex* validation, but *serious* validation
                >can indeed be performed, if you're willing to put up with the syntax.
                >I like scanf. :-)[/color]

                I think the way it ignores leading newlines alone is enough to justify
                dispensing with it for interactive use. Do you really want to have to
                include text such as "If you press Enter first and nothing happens, don't
                keep wailing on the Enter key! Try something /else/!" as part of your
                prompt?
                [color=blue]
                >
                >#include <stdio.h>
                >#include <stdlib.h>
                >#include <ctype.h>
                >
                >int main(void)
                >{
                > char buffer[5] = {0};
                > char *bp;
                > int k;
                > int year;
                >
                > puts("Enter a 4-digit year now.");
                > if (1 != scanf(" %4[0123456789]", buffer)) {
                > puts("No digits entered!");
                > }
                > else {
                > year = strtol(buffer, &bp, 10);
                > if (bp != buffer+4) {
                > puts("Fewer than 4 digits entered!");
                > }
                > else {
                > printf("You entered the (valid) year %d.\n", year);
                > }
                > }
                >
                > k = getchar();
                > if (!isspace(k)) {
                > puts("You left some input at the end of your line.");
                > }
                > ungetc(k, stdin);
                >
                > return 0;
                >}
                >
                >[color=green]
                >> So, how to test for 4 digits? Well, the easiest way I can think of is to
                >> just test for a valid (or invalid) range:
                >>
                >> if (year < 1900 || year > 2030) /* or whatever */[/color]
                >
                > Presumably the OP's problem was that he wanted *four digits of input*,
                >not merely a number in the range 1000-9999; i.e., counting leading
                >zeroes.[/color]

                I'm not sure that's what he really wanted; It looks like he only wanted the
                leading zeros as part of his printf-related validation scheme for the
                input. I don't know, but the fact he's putting the year into an int pretty
                much excludes his possible use for those leading zeros...
                [color=blue]
                >[color=green]
                >> So that takes care of that. Now we can improve the input. sscanf is a royal
                >> pain to work with, because if the user just keeps pressing enter, it'll
                >> merrily echo blank lines till doomsday (it ignores leading whitespace,
                >> including newlines). And if the user presses, say, a letter when scanf is
                >> looking for a number, that letter persists on the input stream the /next/
                >> time you try to read a number, further wreaking havoc.[/color]
                >
                > OTOH, sometimes that's a nice behavior, not least because it's easy
                >to predict its behavior. As opposed to...
                >[color=green]
                >> Better to force a single line of input, and then process it.[/color]
                >
                >...fgets, which reads either up to the first newline, *or* until its
                >buffer runs out, whichever comes first. :) Not that it's impossible
                >to do good stuff with fgets; it's not. But scanf's behavior isn't
                >entirely illogical, either.[/color]

                A sane user interface has always been /much/ easier to get working for me
                using fgets/sscanf, but of course YMMV.
                [color=blue]
                >
                >HTH,
                >-Arthur[/color]

                I expected to get some critique of this from folks, and wasn't
                disappointed. I still don't quite have all the "modern C" idioms down
                (EXIT_FAILURE seems to come real hard, perhaps because I just balk at
                having to type it out...), but I'm slowly getting there ;-)
                -leor


                --
                Leor Zolman --- BD Software --- www.bdsoft.com
                On-Site Training in C/C++, Java, Perl and Unix
                C++ users: Download BD Software's free STL Error Message Decryptor at:
                An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

                Comment

                • olaf

                  #9
                  Re: Help with scanf

                  Hello

                  If you want some string to be has leading zero's

                  use
                  printf("%04d",1 ); => "0001"
                  printf("%04d",1 0001); => "10001"
                  printf("%04.4d" ,10001); => "1000"
                  printf("%4d",1) ; => " 1"


                  for a complete set of pssiblelities look in a good C manual.

                  Greetings



                  matula@tamu.edu (Sivarn) wrote in message news:<be3c6353. 0403311954.5260 7dea@posting.go ogle.com>...[color=blue]
                  > I'm writing a program for that takes dates as input using scanf. I
                  > want to verify that the user is inputting a full 4 digits for the
                  > year. How do I do this? I know that the return value on printf is the
                  > number of printed characters; so if I could somehow get my year
                  > variable to store the leading zeros, I could just run a check:
                  >
                  > int dummy = 0;
                  >
                  > dummy = printf("%d", year);
                  >
                  > if (dummy != 4)
                  > {
                  > ...do something...
                  > }[/color]

                  Comment

                  • Simon Biber

                    #10
                    Re: Help with scanf

                    "Leor Zolman" <leor@bdsoft.co m> wrote:[color=blue]
                    > "Simon Biber" <news@ralminNOS PAM.cc> wrote:[color=green]
                    > >"Leor Zolman" <leor@bdsoft.co m> wrote:[color=darkred]
                    > > > printf("Please enter a year: ");[/color]
                    > >
                    > > Here you need to flush the stdout buffer to ensure that the
                    > > prompt is displayed to the screen before you wait for user
                    > > input.
                    > > fflush(stdout);[/color]
                    >
                    > Is that actually required for stdout if you then pause to read
                    > from stdin? Where'd I get the impression there was some sort
                    > of special case for that...[/color]

                    Although many systems do implement a special case that they flush stdout's
                    buffer when you read from stdin, it is still required on some systems so
                    it's a good idea to include the fflush(stdout) when writing portable code.

                    --
                    Simon.


                    Comment

                    • Leor Zolman

                      #11
                      Re: Help with scanf

                      On Thu, 01 Apr 2004 13:45:02 GMT, "Simon Biber" <news@ralminNOS PAM.cc>
                      wrote:
                      [color=blue]
                      >"Leor Zolman" <leor@bdsoft.co m> wrote:[color=green]
                      >> "Simon Biber" <news@ralminNOS PAM.cc> wrote:[color=darkred]
                      >> >"Leor Zolman" <leor@bdsoft.co m> wrote:
                      >> > > printf("Please enter a year: ");
                      >> >
                      >> > Here you need to flush the stdout buffer to ensure that the
                      >> > prompt is displayed to the screen before you wait for user
                      >> > input.
                      >> > fflush(stdout);[/color]
                      >>
                      >> Is that actually required for stdout if you then pause to read
                      >> from stdin? Where'd I get the impression there was some sort
                      >> of special case for that...[/color]
                      >
                      >Although many systems do implement a special case that they flush stdout's
                      >buffer when you read from stdin, it is still required on some systems so
                      >it's a good idea to include the fflush(stdout) when writing portable code.[/color]

                      Well, then, that's the last straw. I've been driven to actually create a
                      checklist, because there's no /way/ I'm going to remember to flush my
                      stdout before reading stdin unless I have that written down somehwere
                      (grumble...) ;-)
                      -leor


                      --
                      Leor Zolman --- BD Software --- www.bdsoft.com
                      On-Site Training in C/C++, Java, Perl and Unix
                      C++ users: Download BD Software's free STL Error Message Decryptor at:
                      An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

                      Comment

                      • Dan Pop

                        #12
                        Re: Help with scanf

                        In <be3c6353.04033 11954.52607dea@ posting.google. com> matula@tamu.edu (Sivarn) writes:
                        [color=blue]
                        >I'm writing a program for that takes dates as input using scanf. I
                        >want to verify that the user is inputting a full 4 digits for the
                        >year. How do I do this?[/color]

                        Try engaging your brain. What is the smallest 4-digit number? 1000.
                        What is the largest 4-digit number? 9999. So, the test for 4-digit
                        numbers (with no preceding zeros) becomes:

                        if (year >= 1000 && year <= 9999) /* good input */ ;
                        else /* bad input */ ;

                        Now, if your number is supposed to be a year, you may want to restrict
                        the valid input range even further, using exactly the same technique.

                        Dan
                        --
                        Dan Pop
                        DESY Zeuthen, RZ group
                        Email: Dan.Pop@ifh.de

                        Comment

                        • Mike Wahler

                          #13
                          Re: Help with scanf

                          "Leor Zolman" <leor@bdsoft.co m> wrote in message
                          news:563o60l3sg d9r9d1clkcfidkp cpf4l59bj@4ax.c om...
                          [color=blue][color=green][color=darkred]
                          > >> printf("Please enter a year: ");[/color]
                          > >
                          > >Here you need to flush the stdout buffer to ensure that the prompt is
                          > >displayed to the screen before you wait for user input.
                          > > fflush(stdout);[/color]
                          >
                          > Is that actually required for stdout if you then pause to read from stdin?[/color]

                          Yes, if you want to guarantee the output appears before
                          the input request.
                          [color=blue]
                          > Where'd I get the impression there was some sort of special case for
                          > that...[/color]

                          Probably from C++, where cin/cout are 'tied', causing
                          the output to appear first automatically.

                          -Mike


                          Comment

                          • Mike Wahler

                            #14
                            Re: Help with scanf


                            "Leor Zolman" <leor@bdsoft.co m> wrote in message
                            news:1s7o60p5kt i737p0e3nhhe7ds hrir3b4ck@4ax.c om...[color=blue]
                            > On Thu, 01 Apr 2004 13:45:02 GMT, "Simon Biber" <news@ralminNOS PAM.cc>
                            > wrote:
                            >[color=green]
                            > >"Leor Zolman" <leor@bdsoft.co m> wrote:[color=darkred]
                            > >> "Simon Biber" <news@ralminNOS PAM.cc> wrote:
                            > >> >"Leor Zolman" <leor@bdsoft.co m> wrote:
                            > >> > > printf("Please enter a year: ");
                            > >> >
                            > >> > Here you need to flush the stdout buffer to ensure that the
                            > >> > prompt is displayed to the screen before you wait for user
                            > >> > input.
                            > >> > fflush(stdout);
                            > >>
                            > >> Is that actually required for stdout if you then pause to read
                            > >> from stdin? Where'd I get the impression there was some sort
                            > >> of special case for that...[/color]
                            > >
                            > >Although many systems do implement a special case that they flush[/color][/color]
                            stdout's[color=blue][color=green]
                            > >buffer when you read from stdin, it is still required on some systems so
                            > >it's a good idea to include the fflush(stdout) when writing portable[/color][/color]
                            code.[color=blue]
                            >
                            > Well, then, that's the last straw. I've been driven to actually create a
                            > checklist, because there's no /way/ I'm going to remember to flush my
                            > stdout before reading stdin unless I have that written down somehwere
                            > (grumble...) ;-)[/color]

                            Or you see your program behave 'wrongly', i.e. it waits
                            for input before a 'prompt' appears (I've actually had this
                            happen to me on some older DOS platforms -- my lesson
                            was a 'real-world' one, so it stuck. :-)).

                            -Mike


                            Comment

                            • Jeremy Yallop

                              #15
                              Re: Help with scanf

                              Simon Biber wrote:[color=blue]
                              > "Leor Zolman" <leor@bdsoft.co m> wrote:[color=green]
                              >> "Simon Biber" <news@ralminNOS PAM.cc> wrote:[color=darkred]
                              >> >"Leor Zolman" <leor@bdsoft.co m> wrote:
                              >> > > printf("Please enter a year: ");
                              >> >
                              >> > Here you need to flush the stdout buffer to ensure that the
                              >> > prompt is displayed to the screen before you wait for user
                              >> > input.
                              >> > fflush(stdout);[/color]
                              >>
                              >> Is that actually required for stdout if you then pause to read
                              >> from stdin? Where'd I get the impression there was some sort
                              >> of special case for that...[/color]
                              >
                              > Although many systems do implement a special case that they flush stdout's
                              > buffer when you read from stdin, it is still required on some systems so
                              > it's a good idea to include the fflush(stdout) when writing portable code.[/color]

                              Which systems require the flush?

                              Jeremy.

                              Comment

                              Working...