Thread: Example of struct in book causing a headache

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    This could help Flush the input buffer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Help me understand why code below works

    Code:
         84     if (scanf("%d", &nfl.points) != 1)
         85             printf("Please try again\n");
         86
         87     while ((ch = getchar()) != '\n' && ch != EOF);
         88      {
         89     if (scanf("%d", &nfl.yards) != 1)
         90             printf("Please try again\n");
         91      }
    -reading what getchar does I am puzzled how this while loop solved my problems. I declared int ch; in program also. This loop seems to clear out string from previous if statement. Can someone explain why this works now? I am using link you posted above but still confused.

    Well I am guessing it just verifies there is data in stdin and overwrites those contents??

    -Also how could I add code to make user enter a correct number instead of using printf and exiting with no valid input?

    And more importantly utilize the quotee below:
    fgets( buff, BUFSIZ, stdin );
    Then validate buff in whatever way you choose, and copy the data to it's final destination.
    Do I just assign buff = nfl.points and then see ifisdigit?
    Last edited by cjohnman; 05-02-2008 at 09:26 AM.

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if user pesses something like
    qw<ENTER>
    scanf("&#37;d"...
    will not be able to read anything from the stream leaving 3 chars there
    'q' , 'w' , '\n'

    while loop added just reads all chars till it encounters \n - leaving stdin empty

    so probably your alltogether loop should be
    Code:
    while(scanf("%d",&nfl.points) != 1)
    {
       /* clean the stdin */
       int ch;
       while ((ch = getchar()) != '\n' && ch != EOF);
    
       /* and try again */
    
       printf("Please try again\n");
    }
    
    /* succesfully read nfl.points number - could continue*/
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Do I just assign buff = nfl.points and then see ifisdigit?
    no, if you want to use fgets approch - it will be like
    Code:
    char buffer[BUFSIZ];
    char* res;
    while((res = fgets(buffer,sizeof buffer, stdin)) != NULL)
    {
       if(sscanf(buffer, "&#37;d" , &nfl.points) != 1) /* we can use strtol to make more carefull testing of the user input */
       {
          /* successfully read number */
          break;
       }
       /* line format was incorrect */
       printf("Please try again\n");
    }
    
    if(res == NULL)
    {
       /* fgets failed - probably EOF riched - need to abort */
       return -1;
    }
    
    /* successfully read number - we can continue */
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Thank you

    I will add to my program both methods you explained. Thanks for the help!

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    You are freakin awesome!!

    Works very well. Thanks again!

  7. #22
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by cjohnman View Post
    I will add to my program both methods you explained. Thanks for the help!
    do not mix fgets and scanf in the same program - stick to one method - or read everything with fgets and then convert

    or use scanf - without need for convert...

    mixing them could bring unexpected bugs in your code due to scanf leaving \n in stdin and next fgets reading it instead of the next string...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #23
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Ok - I used the approach below

    Should this be ok?

    Code:
         95     printf("Please enter total points\n");
         96     while(scanf("%d",&nfl.points) != 1)
         97     {
         98      /* clean the stdin */
         99      while ((ch = getchar()) != '\n' && ch != EOF);
        100
        101      /* and try again */
        102      printf("Please try again\n");
        103     }
        104
        105     printf("Please enter total yards\n");
        106      while(scanf("%d",&nfl.yards) != 1)
        107           {
        108            /* clean the stdin */
        109            while ((ch = getchar()) != '\n' && ch != EOF);
        110
        111            /* and try again */
        112            printf("Please try again\n");
        113           }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. My final data does not display
    By p1c1078 in forum C Programming
    Replies: 3
    Last Post: 04-23-2003, 06:32 AM