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?