Thread: Any help would be appreciated

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

    Any help would be appreciated

    Hello, I'm writing a program that allows an user to enter student information, and then will hold the information and will allow the user to "delete a student", "add a student", show class (which shows all the students entered by user). I've been working on this code for a while and I'm still working out some last minute errors, if anyone could help me with this it would be greatly appreciated.... Here's the code I have:
    Code:
    #include <stdio.h>
    struct Person
    {
      char name[100];
      int age;
      float gpa;
    };
    void fill_person(struct Person* per)
    {
      printf("Enter name of student:");
      fgets(per->name,100,stdin);
      printf("Enter age of student:");
      scanf("%d", &per->age);
      printf("Enter GPA of student:");
      scanf("%f", &per->gpa);
    }
    
    void show_person(struct Person* per)
    {
      printf("name:%s\n", per->name);
      printf("age:%d\n", per->age);
      printf("GPA:%f\n", per->gpa);
    }
    
    struct Classroom
    {
       int num_students;
       struct Person student[100];
    
    };
    
    
    void add_a_person(struct Classroom* crp)
    {
     for(crp.num_students=0;num_students<100;++num_students)
     {
     crp->num_students.fill_person=num_students+1;
     struct Person student;
     fill_person(&student);
     }
    }
    void delete_a_person(struct Classroom* crp)
    {
     char search_name[100];
     printf("what is the name of the student you would like to delete?\n");
     fgets(search_name, 100, stdin);
     int i;
     while (i=0; i<crp->num_students && strcmp(crp->student[i], search_name)!=0)
       i++;
    
     while (strcmp(search_name,crp->student[i].name)!=0 && i<crp->num_students)
       i++;
     if(i==crp->num_students)
     {
       printf("No match\n");
     }
    }
    void show_class(struct Classroom* crp)
    {
      while(0<crp->num_students)
      {
      show_person(crp->student);
      }
    }
    
    int main()
      {
     struct Classroom crp;
     crp.num_students=0;
     while(crp->num_students<100)
     {
       printf("1. Add a student\n");
       scanf("%d", crp->add_a_person);
       printf("2. Show class\n");
       scanf("%d", crp->show_class);
       printf("3. Delete a student\n");
       scanf("%d", crp->delete_a_person);
       printf("4. Exit\n");
     }
    
    
      }
                                                                                                                                                                                                 83,1          Bot
    This is the error I'm recieving:
    Code:
    project_test.c: In function 'add_a_person':
    project_test.c:36: error: request for member 'num_students' in something not a structure or union
    project_test.c:36: error: 'num_students' undeclared (first use in this function)
    project_test.c:36: error: (Each undeclared identifier is reported only once
    project_test.c:36: error: for each function it appears in.)
    project_test.c:38: error: request for member 'fill_person' in something not a structure or union
    project_test.c: In function 'delete_a_person':
    project_test.c:49: error: expected ')' before ';' token
    project_test.c: In function 'main':
    project_test.c:71: error: invalid type argument of '->' (have 'struct Classroom')
    project_test.c:74: error: invalid type argument of '->' (have 'struct Classroom')
    project_test.c:76: error: invalid type argument of '->' (have 'struct Classroom')
    project_test.c:78: error: invalid type argument of '->' (have 'struct Classroom')
    This is one of my first times using c, I usually use C++.I'm not sure how to set a way for when the user enters 4 to get the program to end, also, I'm not sure if my delete a person function and my add a person function are completley correct too...Any help with this program is much needed, thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for(crp.num_students=0;num_students<100;++num_stud ents)
    ...
    > while (i=0; i<crp->num_students && strcmp(crp->student[i], search_name)!=0)
    You managed to get it right in some places.

    If you've got a pointer to a structure, then use ->member, not .member
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    Quote Originally Posted by Salem View Post
    > for(crp.num_students=0;num_students<100;++num_stud ents)
    ...
    > while (i=0; i<crp->num_students && strcmp(crp->student[i], search_name)!=0)
    You managed to get it right in some places.

    If you've got a pointer to a structure, then use ->member, not .member
    Thanks salem, I fixed that to
    Code:
     for(crp->num_students=0; num_students<100; ++num_students)
    I'm still recieving this error when I try to compile it...:
    Code:
    project_test.c: In function 'add_a_person':project_test.c:36: error: 'num_students' undeclared (first use in this function)
    project_test.c:36: error: (Each undeclared identifier is reported only once
    project_test.c:36: error: for each function it appears in.)
    project_test.c:38: error: request for member 'fill_person' in something not a structure or union
    project_test.c: In function 'delete_a_person':
    project_test.c:49: error: expected ')' before ';' token
    project_test.c: In function 'main':
    project_test.c:71: error: invalid type argument of '->' (have 'struct Classroom')
    project_test.c:74: error: invalid type argument of '->' (have 'struct Classroom')
    project_test.c:76: error: invalid type argument of '->' (have 'struct Classroom')
    project_test.c:78: error: invalid type argument of '->' (have 'struct Classroom')

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
     for(crp->num_students=0;crp->num_students<100;++crp->num_students)
    You do not have a variable called just num_students. If you want there to be a separate variable called num_students then you need to declare one locally in your function, or pass another parameter to your function add.
    Last edited by camel-man; 11-14-2012 at 04:25 PM.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    12
    Quote Originally Posted by camel-man View Post
    Code:
     for(crp->num_students=0;crp->num_students<100;++crp->num_students)
    You do not have a variable called just num_students. If you want there to be a separate variable called num_students then you need to declare one locally in your function, or pass another parameter to your function add.
    If int num_students is in my strucutre, I'm confused at why that doesn't mean I have a variable...I thought by putting int num_students in my structure would mean I do have a variable num_students...
    If I declare one locally in my function, can I only do that once?
    or would I have to do that in every function I use it in?

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You have num_students INSIDE the struct, So you can not reference it with out putting the struct name first. You pass in the pointer to the struct, so now you have to refer to num_students by crp->num_students. If you just passed in a copy of the struct you would reference it with the '.' notation ex) crp.num_students

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You have started 5 different threads in the last week for the same program. Please don't do this. This just complicates everything, because people will answer you in different threads about the same problems.

    Quote Originally Posted by computermajor12 View Post
    I'm not sure if my delete a person function and my add a person function are completley correct too
    Both are wrong. I've told you already that in the adding function you have to assign the student you've created to the students array in your classroom struct.

    There are several errors in your code. It looks like you've just written the program in one go and now are desperately trying since a week to get it to compile. I highly recommend to do it step by step. Work on one part/function and only continue to the next one if you're done with the previous one. Read also A development process.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello, I need your help. Any help is appreciated.
    By angico_211 in forum C Programming
    Replies: 11
    Last Post: 06-22-2011, 08:05 AM
  2. Need help, would be very appreciated.
    By DK12 in forum C Programming
    Replies: 16
    Last Post: 07-28-2009, 04:54 PM
  3. Little help will be appreciated
    By nicolassp in forum C Programming
    Replies: 18
    Last Post: 06-24-2009, 06:57 AM
  4. Help would be much appreciated.
    By jitterbug in forum C++ Programming
    Replies: 6
    Last Post: 04-01-2009, 08:02 PM
  5. much appreciated
    By razrektah in forum C++ Programming
    Replies: 3
    Last Post: 09-14-2001, 10:14 AM