#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    First time using C


    It's my first time using C and I'm doing an assignment which I have a structure that holds information and a couple of functions that use that information:
    My structure is for a person and each person has a name, age, and gpa.
    All three (name,age,gpa) are defined by user input, which is one of my functions : fill_person.
    I'm having trouble running it because its saying that the name, age and gpa are not defined but it's in my function to have them defined by user input.
    Here's my code:
    Code:
      1 #include<stdio.h>
      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, &name);
     13       printf("Enter age of student:");
     14       scanf("%d", &age);
     15       printf("Enter GPA of student:");
     16       scanf("%f", &gpa);
     17 
     18     }
     19     void show_person(struct Person* per)
     20    {
     21       fgets("%c", per->name);
     22       printf("%d", per->age);
     23       printf("%f", per->gpa);
     24    }
     25 
     26 
     27 
     28 int main()
     29 {
     30 }
    This is the error I'm getting:
    Code:
    assignment12.c: In function 'fill_person':
    assignment12.c:12: error: 'name' undeclared (first use in this function)
    assignment12.c:12: error: (Each undeclared identifier is reported only once
    assignment12.c:12: error: for each function it appears in.)
    assignment12.c:14: error: 'age' undeclared (first use in this function)
    assignment12.c:16: error: 'gpa' undeclared (first use in this function)
    assignment12.c: In function 'show_person':
    assignment12.c:21: warning: passing argument 2 of 'fgets' makes integer from pointer without a cast
    /usr/include/stdio.h:604: note: expected 'int' but argument is of type 'char *'
    assignment12.c:21: error: too few arguments to function 'fgets'
    I don't get how I'm getting this error because I'm having all three variables in my structure that are being used in the function be defined by user input..
    Any help with what is wrong with program??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Inside fill_person(), you want to use the pointer like you did inside show_person.

    per->name, for instance, or &per->age
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    37
    Originally Posted by bdb
    &per->age
    Except you don't want that ampersand there.
    Edit: I thought you wrote &per->name there sorry about that

    The problem is that name, age, and gpa aren't declared in fill_person. Those things are declared in your Person struct. Your compiler thinks you're trying to scanf into variables local to the fill_person function. You need to pass a pointer to a Person, which contains those fields, to your function fill_person. As bdb was saying, you need to use the member select operator "->" to say where you're getting those fields from, which in this case is your pointer per.

    Your compiler can't, or rather shouldn't, simply guess that you mean per->name or per->gpa as opposed to some local variable called name or gpa when you just say name or gpa and the & operator which you're misusing doesn't magically accomplish this distinction. If you had local variables called name, age, and gpa like you did in your person struct then it would make sense to do the scanfs the way you're doing, but that would require extra code to then transfer that data to the struct you're passing to your function on top of those extra declarations.

    Use it like this:

    (a pointer to a struct with the fields you want to scanf into)
    ->
    (the field of that struct you want to scanf into)

    instead of &(the field of that struct you want to scanf into) which makes no sense.
    Last edited by jakotheshadows; November 5th, 2012 at 07:20 PM.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    37
    I'm also not sure what you're trying to do with fgets, perhaps you should explain what you think it does. I also suggest you read this: fgets
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Thank you... Now that I fixed that error when I try to compile it I still get this error:
    Code:
    assignment12.c: In function 'fill_person':
    assignment12.c:12: warning: passing argument 2 of 'fgets' makes integer from pointer without a cast
    /usr/include/stdio.h:604: note: expected 'int' but argument is of type 'char *'
    assignment12.c:12: error: too few arguments to function 'fgets'
    assignment12.c: In function 'show_person':
    assignment12.c:21: warning: passing argument 2 of 'fgets' makes integer from pointer without a cast
    /usr/include/stdio.h:604: note: expected 'int' but argument is of type 'char *'
    assignment12.c:21: error: too few arguments to function 'fgets'
    clearly all my error is in my function fill_person and show_person but I'm not whats wrong with them

    fgets:
    My teacher told us to use fgets, and after looking it up I thought that it was what you used for input when using a char variable.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    37
    First of all, you're reading in several characters into an array of characters. Second, pay attention to the number of arguments fgets takes, and what they are supposed to be. Read the link I gave you and report back on what you learn.

    Hint: the order of the arguments matters.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    fgets


    Originally Posted by jakotheshadows
    First of all, you're reading in several characters into an array of characters. Second, pay attention to the number of arguments fgets takes, and what they are supposed to be. Read the link I gave you and report back on what you learn.

    Hint: the order of the arguments matters.
    So I read the fgets reference you posted and from what I understand fgets passes 3 arguments and for me its char name, 100, and per.
    so I changed my code:
    Code:
      1 #include<stdio.h>
      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 }
    this code still will not compile for me :(
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    37
    Originally Posted by computermajor12
    So I read the fgets reference you posted and from what I understand fgets passes 3 arguments and for me its char name, 100, and per.
    No. You're right about the first two, but wrong about the third. You're also not doing what you say you're doing in your code.

    You say you're passing name as the first argument, which is what you need to be doing as long as you understand the previous point I've made about how to access name and how that applies to passing it to a function, but you aren't doing that in your code. What you're passing is "%c" which is a string literal and is completely wrong.

    You seem to think for some reason that fgets uses format specifiers like %c, and that a string literal like "anythinginquotes" will do anything you want it to do, but it doesn't.

    Your 2nd argument is correct, keep that part as is.

    You apparently completely skipped the reading part on the third argument. Reread that and don't make any more assumptions. If there is something in the reading part that you don't understand let us know. I'm only going to meet you half way on this it is your homework and you have to read the manuals on the functions you're using if you don't understand how they work.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    37
    I misread bdb's post with regard to the &. You do need & in front of per->age and per->gpa for your scanfs like he said.

IMN logo majestic logo threadwatch logo seochat tools logo