-
first time using C
Usually I use C++ but was recently given an assignment in C...
here's the assignment:
Declare a structure named Person. The Person structure should have the following fields:
- char name[100];
- int age;
- float gpa;
Write a function with the following signature:
void fill_person(struct Person* per)
Use printf to prompt the user for a name, age, and gpa. For input use fgets for the name and scanf for the age and gpa. The values should be stored in the structure pointed to by per.
Write a second function with the following signature:
void show_person(struct Person* per)
This function should use printf to print out each of the members of the structure pointed to by per.
Your main function needs only three lines:
- Create a struct person object
- Call the fill_person function
- Call the show_person function
I've started my code but I can not get it to compile, can any one help?
Here's my code:
Code:
1 #include<stdio.h>
Code:
2 struct Person
3 {
4 char name[100];
5 int age;
6 float gpa;
7
8 };
9 void fill_person(struct Person* per)
10 {
11 printf("Enter name of student:");
12 fgets("%c",100, per->name);
13 printf("Enter age of student:");
14 scanf("%d", per->age);
15 printf("Enter GPA of student:");
16 scanf("%f", per->gpa);
17
18 }
19 void show_person(struct Person* per)
20 {
21 fgets("%c",100, per->name);
22 printf("%d", per->age);
23 printf("%f", per->gpa);
24 }
25
26
27
28 int main()
29 {
30 }
here is the error I'm recieving:
Code:
assignment12.c: In function 'fill_person':
Code:
assignment12.c:12: warning: passing argument 3 of 'fgets' from incompatible pointer type
/usr/include/stdio.h:604: note: expected 'struct FILE * __restrict__' but argument is of type 'char *'
assignment12.c: In function 'show_person':
assignment12.c:21: warning: passing argument 3 of 'fgets' from incompatible pointer type
/usr/include/stdio.h:604: note: expected 'struct FILE * __restrict__' but argument is of type 'char *'
-
Did you really forget to call the functions you made? ;)
And int main() should return 0
-
Code:
printf("Enter name of student:");
12 fgets("%c",100, per->name);
13 printf("Enter age of student:");
14 scanf("%d", per->age);
15 printf("Enter GPA of student:");
16 scanf("%f", per->gpa);
When scanning for a name you need to use %s not %c(which is just one single character).
Your scanf statments need an '&' before the variable you are trying to store the information in.
ex) Code:
scanf("%f", &per->gpa);
-
Thank you Camel-man.
After fixing those issues I am still getting this error, I've looked into fgets and from what I understand it has to pass 3 arguments
Code:
assignment12.c: In function 'fill_person':
assignment12.c:12: warning: passing argument 3 of 'fgets' from incompatible pointer type
/usr/include/stdio.h:604: note: expected 'struct FILE * __restrict__' but argument is of type 'char (*)[100]'
assignment12.c: In function 'show_person':
assignment12.c:21: warning: passing argument 3 of 'fgets' from incompatible pointer type
/usr/include/stdio.h:604: note: expected 'struct FILE * __restrict__' but argument is of type 'char *'
I don't understand why I'm getting this error..
-
char *fgets(char *s, int n, FILE *stream);
That is fgets prototype.
What you have is fgets("%c",100, per->name); I take it you are typing in the name from the command line, in that case you want to replace per->name with the file stream stdin which is your keyboard. fgets(per->name,100, stdin);
-
Thank you camel-man.
Now should my struct be in my main?
I was going to have my main be like this:
Code:
int main()
{
struct Person* per;
fill_person(&per);
show_person(&per);
}
-
Yes your struct should be in main, however you are creating a struct Person pointer, therefore when you call your function you do not need to use the '&'.
Code:
fill_person(&per);
show_person(&per);
should be Code:
fill_person(per);
show_person(per);
-
I got my code to compile, but now when I run it, It prompts me to enter the students name, and when I do that, and press enter I get
"segmentation fault"
and the program ends...
-
Right, I did not notice that you are using an invalid pointer. You create your struct Person pointer yet you do not point it to a valid memory address. Have you used malloc before? You can either use malloc or create a local variable in main and point per to that location.
-
You should not be using a pointer to the struct in main. You are instructed to create an object of type struct person in main, an actual object and not a pointer to one. As you pass this object to the two functions you are calling, you will need to use the address-of operator to get the object's address.
[edit]Never mind, looks like you have another post that seems to address everything I mentioned here. Oh well.[/edit]