Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

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 double variable.
  • Conditions decide the grade (else if in 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() and bestStudent() compute and return a result, addPoints() and fixScore() change the data, and printResults() and printRanking() print.
  • References pass the vector without copying it. With const, a function only reads it; without const, it can change it. And printRanking() 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 over Student& changes them.
  • A map counts the grades and keeps them sorted.
  • Algorithms with lambdas do jobs you would otherwise write loops for: std::max_element finds the best student, std::sort puts the class in order, and std::find_if finds 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 as main.cpp, go to its folder in the Terminal with cd, and compile it: clang++ main.cpp -o main. This creates a program called main, which you run with ./main.
  • Linux: install the g++ compiler, for example on Ubuntu with sudo apt install g++. Then compile your program with g++ main.cpp -o main and 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::cin works: 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 Dragon can be a kind of Monster, with everything a monster has and an attack of its own. With virtual methods, 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 .cpp files 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. 1

    At the end of printResults(), under the call of printRanking(), create a map std::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. 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
Back

8/8

  1. 1. Test Results
  2. 2. Grades
  3. 3. Class Average
  4. 4. Top Score
  5. 5. Ranking
  6. 6. Bonus Points
  7. 7. Finding a Student
  8. 8. Congratulations!

Try it yourself

Do this exercise right here in your browser. No account needed.