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

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    Classroom structure program


    I'm doing an assignment which is to make a program where users can enter students of a class (their name, gpa, and age), and then they can also add more members, or delete members, or show the whole class... Here's the code I have so far: (It compiles but my main functions aren't working (the program isn't displaying as it should)...
    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)
    {
    
     struct Person student;
     fill_person(&student);
    }
    void delete_a_person(struct Classroom* crp)
    {
     int i;
     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++;
    }
    void show_class(struct Classroom* crp)
    {
      while(0<crp->num_students)
      {show_person(crp->student);
      }
    }
    
    int main()
      {
     struct Classroom crp;
     int num_students=0;
     add_a_person(&crp);
     show_class(&crp);
      }
    Does any one know any ways of editing this code so it will run properly?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by computermajor12
    I'm doing an assignment which is to make a program where users can enter students of a class (their name, gpa, and age), and then they can also add more members, or delete members, or show the whole class... Here's the code I have so far: (It compiles but my main functions aren't working (the program isn't displaying as it should)...
    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)
    {
    
     struct Person student;
     fill_person(&student);
    }
    void delete_a_person(struct Classroom* crp)
    {
     int i;
     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++;
    }
    void show_class(struct Classroom* crp)
    {
      while(0<crp->num_students)
      {show_person(crp->student);
      }
    }
    
    int main()
      {
     struct Classroom crp;
     int num_students=0;
     add_a_person(&crp);
     show_class(&crp);
      }
    Does any one know any ways of editing this code so it will run properly?
    Try changing main like so...

    Code:
    int main()
    {
    	struct Classroom crp;
    	int num_students=0;
    	fill_person(&crp.student[0]);/*first student*/
    	show_person(&crp.student[0]);/*first student*/
    
    	return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo