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

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    Error in class program


    Doing an assignment where I'm making a program that adds students to a "class" and so forth..
    I want to do it in pieces and make sure each part I work on runs so heres the code I have so far:
    Code:
      3 #include <stdio.h>
      4 struct Person
      5 {
      6   char name[100];
      7   int age;
      8   float gpa;
      9 };
     10 void fill_person(struct Person* per)
     11 {
     12   printf("Enter name of student:");
     13   fgets(per->name,100,stdin);
     14   printf("Enter age of student:");
     15   scanf("%d", &per->age);
     16   printf("Enter GPA of student:");
     17   scanf("%f", &per->gpa);
     18 }
     19 
     20 void show_person(struct Person* per)
     21 {
     22   printf("name:%s\n", per->name);
     23   printf("age:%d\n", per->age);
     24   printf("GPA:%f\n", per->gpa);
     25 }
     26 
     27 struct Classroom
     28 {
     29    int num_students;
     30    struct Person student[100];
     31 
     32 };
     33 
     34 void add_a_person(struct Classroom* crp)
     35 {
     36  struct Person student;
     37  fill_person(&student);
     38 }
     39 void delete_a_person(struct Classroom* crp)
     40 {
     41 
     42 }
     43 void show_class(struct Classroom* crp)
     44 {
     45   for(0<num_students)
     46   {
     47   show_person(crp->student);
     48   }
     49 }
     50 
     51 int main()
     52   {
     53  struct Classroom crp;
     54  int num_students=0;
     55  add_a_person(&crp);
     56  show_class(&crp);
     57   }
    I'm getting this error:
    Code:
    project.c: In function 'show_class':
    project.c:45: error: 'num_students' undeclared (first use in this function)
    project.c:45: error: (Each undeclared identifier is reported only once
    project.c:45: error: for each function it appears in.)
    project.c:45: error: expected ';' before ')' token
    project.c:45: error: expected expression before ')' token
    Does anyone know why I'm getting this error?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    In line 45, the for-statement is mal-formed. You only gave it one expression whereas it needs three expressions separated by semicolons. That's what those "expected something that wasn't provided" errors are about.

    As fornum_students, you didn't declare it which is why the compiler is complaining that it's undeclared. Now, you did declare a num_students property of the Classroom class, but to refer to that property you need to do it through an object of that class. That object is being pointed to by the crp parameter that you're passing in. Use it: crp->num_students.
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Perhaps the mal-formed for-loop at line 45 should be a while-loop?

    And num_students is a member of the crp object not a variable in its own right.
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Additionally I wonder how your compiler did not complain about main() being declared as returning int but not explicitly returning a value. This is valid in C++ but not in C.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    This is a well formed question - code and error log provided, but note that the forum can add line numbers for you when you use the code-highlight tag rather then the plain code tag. This makes it easier for anyone to copy & paste your code to try it out if necessary.

    For example:

    C Code:
    #include <stdio.h>
    int main()
    {
        printf( "hello, world!\n" ) ;
     
        return 0 ;
    }
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Also, notice you are making objects on the stack, which is fine, but requires care:

    C Code:
    int main()
    {
    	struct Classroom crp;
    	add_a_person(&crp); 
    	show_class(&crp);
    }

    This is fine, but consider what happens when you call add_a_person:

    C Code:
    void add_a_person(struct Classroom* crp)
    {
    	struct Person student;
    	fill_person(&student);
    }

    You created a struct Person object inside this stack frame, which will be freed as soon as it hits the closing brace of the function. This means that once you return to main, this Person is no longer a valid object and thus is "inaccessible" to your show_class function in main.

    Another way to do it might be like this:
    C Code:
    int main()
    {
    	struct Classroom crp = init_Classroom();
    	add_person_to_top(&crp); 
    	show_class(&crp);
    }
     
    void add_person_to_top(struct Classroom* crp)
    {
    	// get a reference to the "topmost" student element
    	int top = crp->num_students;
    	struct Person *student = crp->student[top];
    	fill_person(student);
    	crp->num_students++;
    }
     
    void delete_person_from_top(struct Classroom* crp)
    {
    	if (crp->num_students == 0) {
    		printf("Delete failed: classroom is empty!\n");
    		return;
    	}
    	crp->num_students--;
    }
     
    struct Classroom init_Classroom(void)
    {
    	return (struct Classroom){.num_students = 0};
    }

    Your fill_person, show_person and show_class can remain as they are, other than the syntax errors and the fixes others have suggested.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Thank you for all the help, and I've started to fix the errors, but for the delete_a_person function, it asks the user what name to delete so it will look more like this:
    void delete_a_person(struct Classroom* crp)
    Code:
    { char search_name[100];
    printf("What is the name of the student you would like to delete?\n);
    fgets(search_name, 100, stdin);
     
    i=0;
    while (i<crp->num_students && strcmp(search_name, crp->student[i].name)!=0)
    i++;
    while (strcmp(search_name,crp->student[i].name)!=0 && i<crp->num_students)
    i++;
    
    }
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by computermajor12
    but for the delete_a_person function, it asks the user what name to delete
    To do this, you need some way to add or remove elements from the array. Arrays declared at compile time are fixed in size (100 elements in this case), so a simple way to add/remove elements would be to add an attribute to your struct Person which marks whether it is filled or not:

    Code:
    struct Person
    {
        bool isfilled;
        char name[100];
        int age;
        float gpa;
    };
    Initially, you should make each isfilled element false. When you add a person, you need to search through your array to find some element for which isfilled == false. After you call fill_person, isfilled should become true. After you delete a person, set isfilled to false.

IMN logo majestic logo threadwatch logo seochat tools logo