Test Results
Welcome to the last lesson of the C++ course! In it, you'll put together everything you've learned and build one useful program step by step: the results of a test, which a math teacher prints after marking it. When you finish, your program will print this:
Max is not in the class.
TEST RESULTS
Adam: 45 points, grade A
Emma: 41 points, grade B
Finn: 50 points, grade A
Jack: 17 points, grade F
Lucy: 38 points, grade B
Average score: 38.2
Best: Finn (50 points)
Ranking: Finn, Adam, Emma, Lucy, Jack
A grades: 2
B grades: 2
F grades: 1The program prints each student's score and grade, works out the average, finds the best student, puts the class in order, gives everyone bonus points, fixes a mistake in one student's score and counts the grades. You'll use conditions, loops, functions, references, strings, structs, maps and algorithms with lambdas. Each exercise continues where the previous one ended (the editor starts with its finished code) and adds one piece.
The data. The results are at the start of main(), in a vector of the struct Student. Each student has a name and a score, the number of points they got (50 at most). What belongs together stays together in one element: students[0] is Adam with 42 points, students[1] is Emma with 38, and so on. In a real school, the data would come from the school's online system. The programs here get their data from variables.
The vector is written one student per line. Each exercise also runs your program with other data, with another score in one of these lines or with one more student, so the program has to work everything out from the vector. Leave the students' lines as they are.
The plan. The program is split into functions, each with one job. So far, there's one: printResults(), which prints the results. For now, it prints only the heading. The struct is at the top, the functions are under it, and main(), which uses them, is at the bottom: a type or a function must come earlier in the program than the place where you use it.
printResults() gets the vector as a const reference, const std::vector<Student>&. It works with the vector from main() without copying it, and it can't change it. That's all printing needs. Go through the students as const references too, as you went through the monsters in the lesson on structs:
for (const Student& student : students) {
std::cout << student.name << std::endl;
}In each pass, student is the next element of the vector itself, not a copy of it, and const means that the loop only reads it. You get to its members with a dot. Without const, the compiler refuses, because the function has promised not to change the vector:
main.cpp:14:19: error: binding reference of type 'Student' to value of type 'const Student' drops 'const' qualifierInstructions
- 1
In
printResults(), under the line that prints the heading, write a loopfor (const Student& student : students). In the loop, print each student's name (student.name) on a line of its own. - 2
Complete the line in the loop so that it also shows the score after the name: after the name, a colon and a space, then
student.score, a space and the wordpoints. The program prints:TEST RESULTS Adam: 42 points Emma: 38 points Finn: 48 points Jack: 14 points Lucy: 27 points