How to create a for loop using class data?

Hi all,
I need to create a for loop for five students using my class. In that for loop, I need to have another loop that asks for the grade for each of the three grades and then call the examScore function in the Student class. How can I do this using what I already have in my main function?

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
 int main(int argc, char *argv[])
{
    string first, last, id;
    double exam1, exam2, exam3, average;
    cout << "First student: " << endl;
    cin >> first;
    cin >> last;
    cout << "ID #: " << endl;
    cin >> id;
    Student student1 = Student(first, last, id);
    cout << endl;
    cout << "Enter ";
    student1.printName();
    cout << "exam scores." << endl;
    cout << "Exam 1: " << endl;
    cin >> exam1;
    student1.examScore(1, exam1);
    cout << "Exam 2: " << endl;
    cin >> exam2;
    student1.examScore(2, exam2);
    cout << "Exam 3: " << endl;
    cin >> exam3;
    student1.examScore(3, exam3);
    student1.calculateGrade();
    average = student1.Grade();
    student1.printStudent();
    cout << "Average grade: " << average << endl;
    cout << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Arrays are what you will mainly need

first you need an array of objects of Student class, then an array of exam grades something like this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Student stud [ 5 ];
double exam [ 3 ];

// I need to create a for loop for five students using my class :
for ( int i = 0; i < 5; ++i ) {
    stud [ i ].someFunction ();
    
    //  In that for loop, I need to have another loop that
    // asks for the grade for each of the three grades
    for ( int j = 0; j < 3; ++j ) {
        // Prompt
        cin >> exam [ j ];
        stud [ i ].someFunction ( exam [ j ] );
    }
    // compute
}
Last edited on
I haven't learned how to do arrays yet so I have to do this without one.
Topic archived. No new replies allowed.