scanf() problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suzanna
    New Member
    • Jul 2007
    • 5

    scanf() problem

    I'm just starting C language classes, and I have searched around to understand why that part of my code behaves the way it does and come to find this site, a page titled "Warning against Scanf" where it says scanf() is less prone to errors when it's used along with fgets().

    Now, we haven't studied fgets() yet and I don't know the correct syntax for it. This is the part of my code that causes a problem.

    =============== =============== =============

    /*asks a user to enter a number less than 25 and gives a warning if a number greater than 25 is entered*/

    int x=26;

    printf("enter a number");

    while(x>25)
    {
    scanf("%d",&x);

    if(x>25)
    printf("Enter a number less or equal to 25");
    else
    printf("the number you entered is %d",x);
    }
    =============== =============== =============== =

    Now this segment works fine as long as the user enters a number, but it starts acting weird if a character or an array of characters is entered.

    how do I use fget() in this case? and is there a way to handle this issue without using fget()? like tweaking scanf() or something?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by suzanna
    I'm just starting C language classes, and I have searched around to understand why that part of my code behaves the way it does and come to find this site, a page titled "Warning against Scanf" where it says scanf() is less prone to errors when it's used along with fgets().

    Now, we haven't studied fgets() yet and I don't know the correct syntax for it. This is the part of my code that causes a problem.

    =============== =============== =============

    /*asks a user to enter a number less than 25 and gives a warning if a number greater than 25 is entered*/

    int x=26;

    printf("enter a number");

    while(x>25)
    {
    scanf("%d",&x);

    if(x>25)
    printf("Enter a number less or equal to 25");
    else
    printf("the number you entered is %d",x);
    }
    =============== =============== =============== =

    Now this segment works fine as long as the user enters a number, but it starts acting weird if a character or an array of characters is entered.

    how do I use fget() in this case? and is there a way to handle this issue without using fget()? like tweaking scanf() or something?
    You're not supposed to tweak anything that is part of the Standard ;-) Note that
    scanf() returns the number of actually scanned items. So if the user types anything
    else but a number scanf() will return 0 (zero) given your "%d" scan format string.
    Your program should handle that situation.

    kind regards,

    Jos

    Comment

    • suzanna
      New Member
      • Jul 2007
      • 5

      #3
      Originally posted by JosAH
      You're not supposed to tweak anything that is part of the Standard ;-) Note that
      scanf() returns the number of actually scanned items. So if the user types anything
      else but a number scanf() will return 0 (zero) given your "%d" scan format string.
      Your program should handle that situation.

      kind regards,

      Jos
      When I try and enter a character, it goes into an infinite loop. Why does that happen?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by suzanna
        When I try and enter a character, it goes into an infinite loop. Why does that happen?
        As I wrote: scanf() doesn't 'eat' the faulty character. Suppose I enter "a123" and
        scanf should scan a "%d" (i.e. an integer number); it 'sees' the 'a' and simply
        bails out, leaving that 'a' in the input stream. Then you try again: same result;
        and again and again.

        According to the scanf() return value you should discard that offending character
        or even the entire line or whatever suits you fine. Simply retrying things again
        and again in a loop doesn't work: i.e. scanf() would reject that same character
        again and again.

        kind regards,

        Jos

        Comment

        • suzanna
          New Member
          • Jul 2007
          • 5

          #5
          and an fgets() would flush out the garbage character? so what is the correct syntax?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Use getch() to eat the offending byte. fgets() is for extracting an entire C string from the input buffer.

            Comment

            • suzanna
              New Member
              • Jul 2007
              • 5

              #7
              This is out of my current knowledge, i'm tied to the total basics. maybe you could show me an example of how getch() works? i mean , what to white inside brackets?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                [CENTER]
                Originally posted by suzanna
                This is out of my current knowledge
                No it is not; when/if your scanf() call doesn't return 1 (one), eat that offending
                character and see how your program behaves.

                kind regards,

                Jos

                Comment

                • suzanna
                  New Member
                  • Jul 2007
                  • 5

                  #9
                  Thank you! I got it. you were very helpful. It took some brain storming but I finally got it.

                  Comment

                  Working...