I just wrote so much and accidentally closed my browser... Let's try again.
You can also use a while loop.
1 2 3 4 5
|
while (condition) {
expression;
expression;
...
}
|
The first a computer does when he 'sees' a while-statement, is evaluating the condition. If the condition is false, he will just ignore the whole while loop. If it is true, he will repeat the loop as many times as the condition is true. Or in other words; he will repeat it until the condition is false. The computer executes every expression inside the loop and then jumps back up to the head, to check what the condition evaluates to, and then acts accordingly. Either repeat it again or ignore and go on after the while loop. That is after the closing curly brace.
In your case you want to repeat the loop as often as there are students in the classroom. So you need a variable with the amount of students, which you will input yourself, and a counter variable that gets incremented everytime the while loops. With the counter variable, you know which student you are currently doing. Therefore your while-head looks like this:
while (counter < NumStudents)
.
To input a value into a variable, you use
cin >>
.
To output a value of a variable, you use
cout <<
.
You basically already wrote the whole code:
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 36 37
|
// Get students first name.
cout << "Enter student's first name. ";
cin >> studFirstName;
// Get students last name.
cout << "Enter student's last name. ";
cin >> studLastName;
// Get exam 1 score.
cout << "grade from exam 1 = ";
cin >> Exam1;
// Get exam 2 score.
cout << "grade from exam 2 = ";
cin >> Exam2;
// Get exam 3 score.
cout << "grade from exam 3 = ";
cin >> Exam3;
// Calculate exam total.
studScore = Exam1 + Exam2 + Exam3;
// Calculate exam total.
studAvg = studScore/3;
|
This is what you want to execute multiple times. As many times as there are students in classroom. This is the code you want to have inside the while loop.
Try the tutorial on this site.
Look for "thenewboston" on youtube, I also used to learn with him :)