Congratulations!
Congratulations, your program works, and you've reached the end of the C++ course! It prints every student's score and grade, works out the average, finds the best student, puts the class in order, adds the bonus for the broken question and fixes a score by name. And you wrote all of it yourself.
One last improvement. The teacher wants to see at a glance how the class did: how many students got each grade. That's counting with a map, as in the lesson on maps. The grade is the key and the number of students the value, so the map is a std::map<char, int>. counts[grade(student.score)]++ adds 1 to the count of that grade, and a grade that isn't in the map yet starts at 0. The map keeps its keys sorted, and a char sorts by its code, just like a string, so A comes first. A grade that nobody got isn't in the map at all, and it isn't printed. The map needs #include <map>.
What you've learned. Look at how each part of the course found its place in your program:
- Variables and arithmetic hold the sum of the scores. The average has decimals because you added up in a
doublevariable. - Conditions decide the grade (
else ifin the right order), keep the scores at 50 at most and check whether a student was found. - Vectors and loops hold the whole class and go through it to print the students, add up their scores, give them the bonus and count their grades.
- Functions split the program into parts with clear jobs:
grade(),average()andbestStudent()compute and return a result,addPoints()andfixScore()change the data, andprintResults()andprintRanking()print. - References pass the vector without copying it. With
const, a function only reads it; withoutconst, it can change it. AndprintRanking()gets a copy on purpose, so that it can sort it. - Strings are the students' names, which
fixScore()compares with==. - A struct keeps each student's name and score together, so nothing can get out of order. Loops over
const Student&read the students, and a loop overStudent&changes them. - A map counts the grades and keeps them sorted.
- Algorithms with lambdas do jobs you would otherwise write loops for:
std::max_elementfinds the best student,std::sortputs the class in order, andstd::find_iffinds a student by name, with a lambda that captures the name. Their results are iterators, which you read with*and->. - And when something didn't work, the compiler's error messages told you what was wrong and on which line. It always pays to fix the first one first.
Two parts of the course aren't in this program. A class keeps its data private and lets only its own methods change it, so its rules always hold, as the hero's health did in the monster arena. A student's result has no such rules, so a struct is enough. And you didn't need pointers: for function parameters, references are simpler and safer. But you'll see pointers in other people's code and in libraries, and you'll need them when you learn about dynamic memory.
C++ on your own computer. Here, your browser compiles and runs the programs. On your own computer, you have several options:
- An online compiler needs no installation. For example, on onlinegdb.com, choose C++ as the language, paste your program and run it with the Run button. Input from the keyboard works there too.
- Windows: install Visual Studio Community, which is free, and choose the Desktop development with C++ workload during the installation. Then create a new Console App project, replace the sample code with your program and run it with
Ctrl+F5. Visual Studio Code is a different program: just an editor, for which you'd have to install a compiler separately. - macOS: in the Terminal, install the tools with the command
xcode-select --install(the Xcode Command Line Tools). Save your program asmain.cpp, go to its folder in the Terminal withcd, and compile it:clang++ main.cpp -o main. This creates a program calledmain, which you run with./main. - Linux: install the g++ compiler, for example on Ubuntu with
sudo apt install g++. Then compile your program withg++ main.cpp -o mainand run it with./main.
The -o main part says what the finished program should be called. After every change to the code, you have to compile it again, or you'll run the old version. Here on the website, the Run button did that for you. If the compiler on a Mac reports errors or warnings for newer C++ features, add -std=c++17: clang++ -std=c++17 main.cpp -o main uses the same version of the language as this website.
What's next. You now know the basics that every C++ program is built from, and the most useful tools of its standard library. Here are some ideas for what to learn next:
- Input from the user. On your computer,
std::cinworks:std::cin >> score;waits until the user types a number and presses Enter, and stores it in the variable. (Here in the browser, there's no one to type, so it doesn't work.) The teacher could then type in the scores. - Files. The
<fstream>library can read data from a file and write to one, so the results don't disappear when the program ends. - Inheritance and virtual functions. A class can build on another one: a
Dragoncan be a kind ofMonster, with everything a monster has and an attack of its own. Withvirtualmethods, each kind of monster can do its own thing when the game calls the same method on all of them. - Programs in several files. Larger programs split their code into header files (
.h), which declare the types and functions, and.cppfiles with the code. The compiler then puts them together into one program. - Games. A library such as raylib or SFML opens a window, draws shapes and pictures, plays sounds and reads the keyboard and the mouse. With what you know, you can make a small game in it, such as the monster arena with the monsters on the screen.
- Your own projects and contests. A number-guessing game, a quiz, tic-tac-toe in a nested vector. You learn the most by solving your own problems. C++ is also very popular in programming contests, such as the olympiads in informatics that many countries hold for secondary school students.
And if you'd like to try another language, the Python and JavaScript courses are waiting for you.
Instructions
- 1
At the end of
printResults(), under the call ofprintRanking(), create a mapstd::map<char, int> counts;and, in a loop over the students, add 1 to the count of each student's grade. Add#include <map>at the top. - 2
Under it, print each grade in the map with its count, in a loop over the map, one grade per line. Our class has two A's, two B's and one F:
A grades: 2 B grades: 2 F grades: 1