Need help with structs.

closed account (zbX9z8AR)
I'm trying to print out the names of the students enrolled in the course. I have to do it through an enrollment array which is e. But the first and last name of the student is in another struct called student as well as their ID. How can I be able to search through the course and print out its roster through the enrollment array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  void print_students_in_course(student* s, course* c, enrollment* e)
{
        string coursename;
        int coursenumber, numberofcourses;
        float *studentenrollment;

        cout << "Enter a course name. ";
        cin >> coursename;
        cout << "Enter a course number. ";
        cin >> coursenumber;

        //searches for course
        for(int j=0; j<num_courses; j++)
        {
                //if the course is found
                if(coursename == e[j].name && coursenumber == e[j].course)
                {
                        cout << "\nStudent Name\tStudent Number\n";
                        cout << "------------\t-------------\n";
                        //searches enrolled students
                        for(int i=0; i<c[j].roll; i++)
                        {
                                //prints out student name and id
                                cout << s[i].lastname << ", " << s[i].firstname << "\t" << e[i].student << endl;
                        }
                        break;
                }
        }
        //if the class can't be found
        if(numberofcourses == num_courses)
        {
                cout << "This course is not found.\n";
        }
        return;
}
Last edited on
you basically have to pick the ID from E and search the other array for that, for every entry.

I think you are close. Just like you have the if statement to find the coursename, you need one for the ID.

not sure exactly what you have but something like

for(...)
if(e.student == s.id)
cout ....

around line 23.
closed account (zbX9z8AR)
Well, for line 16, I had to change the e[j].name and e[j].course to c[j].name and c[j].id. So I don't have anything in enrollment that connects to the student or course structs. That's what I'm trying to figure out.
I can't help you.. I am missing some vital piece of info about how the 2 arrays are tied together. If both arrays do not contain the student ID value, and you were not instructed to do something cool like having the id number = the index in the C array, then I don't know.

c[e[x].id] would be how that would look.
Topic archived. No new replies allowed.