Write a program that lets a lecturer keep track of test scores for their students.
-sample-
How many students are in your class? -5
Invalid number of students, try again.
How many students are in your class? 3
How many test in this class? -10
Invalid number of tests, try again.
How many test in this class?
Ok, lets’ begin . . .
**** Student #1 ****
Enter score for test #1: -50
Invalid score, try again.
Enter score for test #1: 50
Enter score for test #2: 75
Average score for student #1 is 62.50
**** Student #2 ****
Enter score for test #1: 100
Enter score for test #2: 90
Average score for student #2 is 95.00
**** Student #3 ****
Enter score for test #1: -10
Invalid score, try again.
Enter score for test #2: -20
Invalid score, try again.
Enter score for test #2: -30
Invalid score, try again.
Enter score for test #1: 90
Enter score for test #2: 80
Average score for student #3 is 85.00
Average score for all student is 80.83
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int numberStudents = 0;
while (true)
{
cout << "How many students are in your class? ";
cin >> numberStudents;
if (numberStudents <= 0) cout << "Invalid number of students, try again." << endl;
else break;
}
int numberTest = 0;
cout << endl;
while(true)
{
cout << "How many test in this class? ";
cin >> numberTest;
if (numberTest <= 0) cout << "Invalid number of tests, try again." << endl;
else break;
}
cout << endl;
cout << "Ok, lets begin..." << endl;
double sum = 0;
double score = 0;
double totalSum = 0;
cout << fixed << setprecision(2);
for (int i = 1; i <= numberStudents; i++)
{
cout << "**** Student #" << i << "****" << endl;
for (int j = 1; j <= numberTest; j++)
{
while(true)
{
cout << "Enter score for test #" << j << ": ";
cin >> score;
if (score < 0) cout << "Invalid score, try again." << endl;
else break;
}
sum += score;
}
totalSum += sum;
cout << "Average score for student #" << i << " is " << sum / numberTest << endl << endl << endl;
sum = 0;
}
cout << "Average score for all student is " << totalSum / (numberStudents * numberTest) << endl;
return 0;
}
Comments
Leave a comment