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, 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 many students failed the test. Add a line with the number of F grades at the end of the results. It's counting with a condition, from the lesson on loops: a counter starts at 0, a loop goes through everyone's scores, and each time grade() returns 'F', the counter goes up by 1.
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 and the counter. 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 hold the names and the scores. What belongs together has the same index.
- Loops go through the students to print them, add up their scores, find the best one, find a student by name (with
break) and count the F grades. - Functions split the program into parts with clear jobs:
grade(),average()andbestStudent()compute and return a result,addPoints()andfixScore()change the data, andprintResults()prints. - References pass the vectors without copying them. With
const, a function only reads them; withoutconst, it can change them. - Strings (
std::string) are the students' names, and you compare them with==. - 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.
You didn't need pointers in the project: for function parameters, references are simpler and safer. But you'll see them 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. 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. - Structs and classes. Instead of two vectors that you have to keep in the same order, you can create your own type with
struct, for examplestruct Student { std::string name; int score; };, and put all the students in onestd::vector<Student>. Classes (class) add functions of their own to the data. std::map. It stores values under keys: with astd::map<std::string, int>, you set a score by name, likescores["Lucy"] = 35;, and you don't need a loop to find the student.- Files. The
<fstream>library can read data from a file and write to one, so the results don't disappear when the program ends. - Algorithms. The
<algorithm>library has ready-made functions for what you wrote loops for:std::sortsorts a vector,std::max_elementfinds the largest element andstd::findsearches. Now you know how they work inside. - 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
At the end of printResults(), under the line with the best student, add a line with the number of students who failed. A counter starts at 0, and a loop increases it by 1 for each student whose grade() is 'F'. Our class has one:
Failed: 1Check that the number after "Failed: " is printed from the counter variable, not written straight into the code. Only then will the program count correctly for another class too.
Start programming for free
7/7