Thread: first time using C

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    12

    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:

    1. Create a struct person object
    2. Call the fill_person function
    3. 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 *'




  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you really forget to call the functions you made?

    And int main() should return 0

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    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);

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    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..

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    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);

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    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);
    }

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    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);

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    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...

  9. #9
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    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.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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]
    Last edited by hk_mp5kpdw; 11-07-2012 at 06:57 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [HELP] Code to Convert Army Time to Normal Time?
    By Kipper DeVera in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2011, 11:50 PM
  2. Replies: 23
    Last Post: 05-22-2011, 11:20 PM
  3. Replies: 7
    Last Post: 11-21-2009, 01:02 AM
  4. Replies: 3
    Last Post: 06-13-2003, 06:47 AM
  5. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM