Example of struct in book causing a headache

This is a discussion on Example of struct in book causing a headache within the C Programming forums, part of the General Programming Boards category; Can someone point out what is wrong with this small program it is similar to an example in book I ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Example of struct in book causing a headache

    Can someone point out what is wrong with this small program it is similar to an example in book I am reading but I am getting a segment core dump? Thanks.

    Code:
          struct football {
              int yards;
              int points;
              char team[30];
              } nfl;
         
           void print_it(struct football new);
         
          int main()
         {
        
               printf("Who is your favorite team?\n");
              scanf("%s", nfl.team);
         
              printf("How many points did they score?\n");
              scanf("%d", nfl.points);
         
              printf("How many yards did they gain?\n");
              scanf("%d", nfl.yards);
         
              print_it(nfl);
    
              return 0;
          }
    
             void print_it(struct football new)
             {
             printf("Your fav team is:%s They scored %d points Gained %d yards\n", new.team, new.points, new.yards);
             }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try raising the warning level, as it should be able to complain about scanf() needing a pointer as the argument receiving the result.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by matsp View Post
    Try raising the warning level, as it should be able to complain about scanf() needing a pointer as the argument receiving the result.

    --
    Mats
    not all compilers do it...

    but yes - should be
    Code:
    scanf("%d", &nfl.points);
    Why do you use global vars? there is no need here
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Made following changes and it works but still confused

    I made the changes below but I am confused if I need user input for the struct members. How would I do this properly?

    Code:
     struct football {
              int yards;
              int points;
              char team[30];
              } nfl;
         
           void print_it(struct football new);
         
          int main()
         {
                      
              strcpy(nfl.team,"Pittsburgh Steelers"); 
         
              nfl.points = 42;
              nfl.yards = 250;
         
              print_it(nfl);
    
              return 0;
          }
    
             void print_it(struct football new)
             {
             printf("Your fav team is:%s They scored %d points Gained %d yards\n", new.team, new.points, new.yards);
             }

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    115
    I am trying to use global variables in my studies because that is how the programs here at work are done. I figure I might as well get used to this now rather then later. Thanks for the feedback. I really appreciate everyones help.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    How would I do this properly?
    Code:
    if(scanf("%d", &nfl.points) == 1)
    {
       /* number read */
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    how the programs here at work are done
    horrible... better start learning good practices... Worst will hunt you by theirself...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

    Could you be more detailed in the if statement please

    When this happens:
    Code:
    if(scanf("%d", &nfl.points) == 1)
    {
       /* number read */
    }
    I am unsure what to place in between if statement. I see you said number read but nfl.points would now already hold the value based on user input right? Still confused a little but what you posted is making some sense. I just want to get it right now to avoid mistakes in the future.

    Thanks.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    scanf will return the "number of items successfully read", so if the format string contains "%d", then you should expect to get 1 back. Anything else means something went wrong - if it's zero, someone probably typed in an illegal number (such as "a" instead of "123"), if you get -1 (EOF), then the first character read in scanf() was EOF - so no data is available.

    It would in some ways, make more sense to do:
    Code:
    if(scanf("%d", &nfl.points) != 1)
    {
       /* read error */
    }
    The red bits indicate my changes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    That made sense - Thanks

    Thank you very much for the detailed explanation. I can now see where and how I can use this. Thanks everyone!!

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    One more question about this

    If I use

    Code:
         85     if(scanf("%d", &nfl.points) != 1)
         86     {
         87      printf("Please enter a number!/n");
         88     }
    How could I force the user to enter a correct value before proceeding?

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    I'd do it like:

    Code:
    while (scanf("%d", &nfl.points) != 1)
    {
         printf("Please enter a number!\n");
    }
    If you want to you could of course also add the original line asking for the number after printing "Please enter a number". This way it will loop until scanf... is equal to 1.

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    I'd do it like:
    what is missing here - cleaning of the stdin from the garbage left there...
    also note that if the stdin was redirected to be read from file - it could be nothing left to read from there...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Endless Loop

    When I changed the code to

    Code:
           while(scanf("%d", &nfl.points) != 1)
             {
              printf("Please enter a number!/n");/* read error */
             }
    and enter a character the program is stuck in printf statement. Am I missing something?

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Sorry about that. I usually don't use scanf, but he's right, you should clean the stdin before looping again.

    The reason I'm not worried about reaching the end of the file however is because in this case he asks a specific question to the user, so I assume the stdin won't be redirected to read from file in this case.

Page 1 of 2 12 LastLast
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21