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);
}
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);
}
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.
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?
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:
Quote:
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?
You are freakin awesome!!
Works very well. Thanks again!
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 }