scanf in a for loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    scanf in a for loop

    In Stephen G. Kochan's book "Programmin g in C" there is a program with the
    following:

    for(i = 1; i <= numOfGrades; ++i) {
    printf("Enter grade #%i: ", i);
    scanf("i%", &grade);
    ...
    }

    Does the scanf command pause the loop until the user input is received?
    When I run this program the loop keeps on going without pausing for the
    user input.

    What is going on here? Can anyone please help me?

    JoshO.

  • skaoth

    #2
    Re: scanf in a for loop

    Yes, the scanf should cause the for loop to pause and allow you to
    enter your integer.
    The syntax error that I see is that
    scanf("i%", &grade);
    should be
    scanf("%i", &grade);

    Comment

    • Malcolm

      #3
      Re: scanf in a for loop


      wrote[color=blue]
      > for(i = 1; i <= numOfGrades; ++i) {
      > printf("Enter grade #%i: ", i);
      > scanf("i%", &grade);
      > ...
      > }
      >
      > Does the scanf command pause the loop until the user input is received?
      > When I run this program the loop keeps on going without pausing for the
      > user input.
      >
      > What is going on here? Can anyone please help me?
      >[/color]
      Yoy've mistyped the "%i" as "i%". This means that scanf() is looking for the
      letter I followed by the field specifier, followed by a NUL, which is
      illegal. On your system it is simply ignoring it and returning.

      When calling scanf() you should check the return value, which gives the
      number of fields successfully converted, mainly to guard against bad user
      input but also to catch this sort of error.


      Comment

      • reachsachin@gmail.com

        #4
        Re: scanf in a for loop


        "" <> wrote:[color=blue]
        > In Stephen G. Kochan's book "Programmin g in C" there is a program[/color]
        with the[color=blue]
        > following:
        >
        > for(i = 1; i <= numOfGrades; ++i) {
        > printf("Enter grade #%i: ", i);
        > scanf("i%", &grade);
        > ...
        > }
        >
        > Does the scanf command pause the loop until the user input is[/color]
        received?[color=blue]
        > When I run this program the loop keeps on going without pausing for[/color]
        the[color=blue]
        > user input.
        >
        > What is going on here? Can anyone please help me?
        >
        > JoshO.[/color]

        Comment

        • sachin

          #5
          Re: scanf in a for loop

          hi josho
          first of all, the format specifier in the scanf statement is incorrect,
          as mentioned by others.

          Secondly, the scanf statement is not waiting for further values of
          grade as expected just because it always find a character in its input
          buffer.
          So one probable solution is to clear that character, which you enter
          for one scanf statement from the input buffer, so that next scanf
          statement DOES NOT find the previously keyed in character, and waits
          for the new fresh value of 'grade'.

          for(i = 1; i <= numOfGrades; ++i) {
          printf("Enter grade #%i: ", i);
          fflush(stdin);
          scanf("i%", &grade);
          ....
          }
          I tested the code, its running on solaris platform. Moreover, I believe
          fflush is a generic syscall independent of the OS.

          Regards
          Sachin

          Comment

          • Keith Thompson

            #6
            Re: scanf in a for loop

            "sachin" <reachsachin@gm ail.com> writes:[color=blue]
            > hi josho
            > first of all, the format specifier in the scanf statement is incorrect,
            > as mentioned by others.
            >
            > Secondly, the scanf statement is not waiting for further values of
            > grade as expected just because it always find a character in its input
            > buffer.
            > So one probable solution is to clear that character, which you enter
            > for one scanf statement from the input buffer, so that next scanf
            > statement DOES NOT find the previously keyed in character, and waits
            > for the new fresh value of 'grade'.
            >
            > for(i = 1; i <= numOfGrades; ++i) {
            > printf("Enter grade #%i: ", i);
            > fflush(stdin);
            > scanf("i%", &grade);
            > ...
            > }
            > I tested the code, its running on solaris platform. Moreover, I believe
            > fflush is a generic syscall independent of the OS.[/color]

            fflush(stdin) invokes undefined behavior.

            <http://www.eskimo.com/~scs/C-faq/q12.26.html>

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • Mac

              #7
              Re: scanf in a for loop

              On Sat, 25 Dec 2004 20:14:48 +0000, Keith Thompson wrote:
              [color=blue]
              > "sachin" <reachsachin@gm ail.com> writes:[/color]
              [snip][color=blue][color=green]
              >> printf("Enter grade #%i: ", i);
              >> fflush(stdin);[/color][/color]
              [snip][color=blue]
              >
              > fflush(stdin) invokes undefined behavior.[/color]

              Yes, but sachin should have written "fflush(stdout) ", obviously.

              --Mac

              Comment

              • Keith Thompson

                #8
                Re: scanf in a for loop

                Mac <foo@bar.net> writes:[color=blue]
                > On Sat, 25 Dec 2004 20:14:48 +0000, Keith Thompson wrote:[color=green]
                >> "sachin" <reachsachin@gm ail.com> writes:[/color]
                > [snip][color=green][color=darkred]
                >>> printf("Enter grade #%i: ", i);
                >>> fflush(stdin);[/color][/color]
                > [snip][color=green]
                >>
                >> fflush(stdin) invokes undefined behavior.[/color]
                >
                > Yes, but sachin should have written "fflush(stdout) ", obviously.[/color]

                Actually, I think sachin meant to clear the input characters. The way
                to do that is to read and discard them. (I'm too lazy to work out the
                details.)

                Merry Christmas!

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                We must do something. This is something. Therefore, we must do this.

                Comment

                • infobahn

                  #9
                  Re: scanf in a for loop

                  Mac wrote:[color=blue]
                  > On Sat, 25 Dec 2004 20:14:48 +0000, Keith Thompson wrote:
                  >
                  >[color=green]
                  >>"sachin" <reachsachin@gm ail.com> writes:[/color]
                  >
                  > [snip]
                  >[color=green][color=darkred]
                  >>>printf("Ente r grade #%i: ", i);
                  >>>fflush(stdin );[/color][/color]
                  >
                  > [snip]
                  >[color=green]
                  >>fflush(stdi n) invokes undefined behavior.[/color]
                  >
                  >
                  > Yes, but sachin should have written "fflush(stdout) ", obviously.[/color]

                  And he should have written corrected the OP's i% to %i in the
                  scanf call. And he should have demonstrated how to test the
                  return value from scanf properly...

                  Comment

                  • Mac

                    #10
                    Re: scanf in a for loop

                    On Sat, 25 Dec 2004 23:06:42 +0000, Keith Thompson wrote:
                    [color=blue]
                    > Merry Christmas![/color]

                    Merry Christmas to you, too!

                    ;-)

                    --Mac

                    Comment

                    • Mac

                      #11
                      Re: scanf in a for loop

                      On Sun, 26 Dec 2004 02:47:06 +0000, infobahn wrote:
                      [color=blue]
                      > Mac wrote:[color=green]
                      >> On Sat, 25 Dec 2004 20:14:48 +0000, Keith Thompson wrote:
                      >>
                      >>[color=darkred]
                      >>>"sachin" <reachsachin@gm ail.com> writes:[/color]
                      >>
                      >> [snip]
                      >>[color=darkred]
                      >>>>printf("Ent er grade #%i: ", i);
                      >>>>fflush(stdi n);[/color]
                      >>
                      >> [snip]
                      >>[color=darkred]
                      >>>fflush(stdin ) invokes undefined behavior.[/color]
                      >>
                      >>
                      >> Yes, but sachin should have written "fflush(stdout) ", obviously.[/color]
                      >
                      > And he should have written corrected the OP's i% to %i in the
                      > scanf call.[/color]

                      Actually, he did. It's still visible above...


                      --Mac

                      Comment

                      • infobahn

                        #12
                        Re: scanf in a for loop

                        Mac wrote:[color=blue]
                        > On Sun, 26 Dec 2004 02:47:06 +0000, infobahn wrote:
                        >
                        >[color=green]
                        >>Mac wrote:
                        >>[color=darkred]
                        >>>On Sat, 25 Dec 2004 20:14:48 +0000, Keith Thompson wrote:
                        >>>
                        >>>
                        >>>
                        >>>>"sachin" <reachsachin@gm ail.com> writes:
                        >>>
                        >>>[snip]
                        >>>
                        >>>
                        >>>>>printf("En ter grade #%i: ", i);
                        >>>>>fflush(std in);
                        >>>
                        >>>[snip]
                        >>>
                        >>>
                        >>>>fflush(stdi n) invokes undefined behavior.
                        >>>
                        >>>
                        >>>Yes, but sachin should have written "fflush(stdout) ", obviously.[/color]
                        >>
                        >>And he should have written corrected the OP's i% to %i in the
                        >>scanf call.[/color][/color]
                        ^^^^^
                        [color=blue]
                        > Actually, he did. It's still visible above...[/color]

                        No, he didn't, and no, it isn't.

                        Comment

                        • Emmanuel Delahaye

                          #13
                          Re: scanf in a for loop

                          sachin wrote on 25/12/04 :[color=blue]
                          > fflush(stdin);[/color]

                          No. Please, read the FAQ.

                          --
                          Emmanuel
                          The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
                          The C-library: http://www.dinkumware.com/refxc.html

                          "Clearly your code does not meet the original spec."
                          "You are sentenced to 30 lashes with a wet noodle."
                          -- Jerry Coffin in a.l.c.c++

                          Comment

                          • Mac

                            #14
                            Re: scanf in a for loop

                            On Sun, 26 Dec 2004 07:25:14 +0000, infobahn wrote:
                            [color=blue]
                            > Mac wrote:[color=green]
                            >> On Sun, 26 Dec 2004 02:47:06 +0000, infobahn wrote:
                            >>
                            >>[color=darkred]
                            >>>Mac wrote:
                            >>>
                            >>>>On Sat, 25 Dec 2004 20:14:48 +0000, Keith Thompson wrote:
                            >>>>
                            >>>>
                            >>>>
                            >>>>>"sachin" <reachsachin@gm ail.com> writes:
                            >>>>
                            >>>>[snip]
                            >>>>
                            >>>>
                            >>>>>>printf("E nter grade #%i: ", i);
                            >>>>>>fflush(st din);
                            >>>>
                            >>>>[snip]
                            >>>>
                            >>>>
                            >>>>>fflush(std in) invokes undefined behavior.
                            >>>>
                            >>>>
                            >>>>Yes, but sachin should have written "fflush(stdout) ", obviously.
                            >>>
                            >>>And he should have written corrected the OP's i% to %i in the
                            >>>scanf call.[/color][/color]
                            > ^^^^^
                            >[color=green]
                            >> Actually, he did. It's still visible above...[/color]
                            >
                            > No, he didn't, and no, it isn't.[/color]

                            My apologies. You are correct on both counts. I was looking at the
                            printf() call, as you seem to have surmised.

                            --Mac

                            Comment

                            • king arthur

                              #15
                              Re: scanf in a for loop

                              On Sat, 25 Dec 2004 02:48:27 -0500, "" <> wrote:
                              [color=blue]
                              >In Stephen G. Kochan's book "Programmin g in C" there is a program with the
                              >following:
                              >
                              >for(i = 1; i <= numOfGrades; ++i) {
                              > printf("Enter grade #%i: ", i);
                              > scanf("i%", &grade);
                              > ...
                              > }
                              >
                              >Does the scanf command pause the loop until the user input is received?
                              >When I run this program the loop keeps on going without pausing for the
                              >user input.
                              >
                              >What is going on here? Can anyone please help me?
                              >
                              >JoshO.[/color]

                              First of all I believe the scanf statement as listed above has a
                              slight error. The "i%" should be "%i".

                              Retry the program with this correction and see what happens.

                              Comment

                              Working...