Finding a Student

Lucy has come to complain: the teacher added up her points wrong. She should have 35, not 27. The program needs a function that fixes a student's score by name. But first, it has to find the student in names, which means finding their index.

Searching is a pattern from the lesson on loops: the loop goes through the elements, and when it finds the right one, it remembers its index, and break ends the loop, because there's no need to search any further. The variable for the index starts at -1. No vector has such an index. If the loop finds nothing, the variable stays -1, which means that what you're looking for isn't in the vector:

#include <iostream>
#include <string>
#include <vector>

int findSubject(const std::vector<std::string>& subjects, std::string wanted) {
    int index = -1;
    for (int i = 0; i < subjects.size(); i++) {
        if (subjects[i] == wanted) {
            index = i;
            break;
        }
    }
    return index;
}

int main() {
    std::vector<std::string> timetable = {"Maths", "Physics", "History"};
    std::cout << findSubject(timetable, "Physics") << std::endl;
    std::cout << findSubject(timetable, "Chemistry") << std::endl;
    return 0;
}
1
-1

You compare strings with ==, and the comparison is exact: "lucy" is not the same as "Lucy".

The function fixScore() gets both vectors. It only reads names, so it gets them as a const reference. It changes scores, so it gets them as a reference without const.

The order of the calls in main() matters. Lucy scored 35 points in the test, and she gets the 3 points for the broken question like everyone else. So the fix goes before the call of addPoints(scores, 3). After it, the fix would overwrite Lucy's bonus, and she'd have just 35 points.

And what if the teacher types a name that isn't in the vector? Max, for example, is in the class next door. findStudent() returns -1, and scores[-1] is outside the vector. You know from the lesson on vectors that an index out of range is a bug: the program can crash, or quietly overwrite memory that doesn't belong to the vector. That's why fixScore() checks the index first.

Instructions

Under addPoints(), write a function int findStudent(const std::vector<std::string>& names, std::string name) that returns the index of the student called name, or -1 if they aren't in the vector. When it finds the student, end the loop with break.

Under it, write a function void fixScore(const std::vector<std::string>& names, std::vector<int>& scores, std::string name, int newScore) that finds the student's index with findStudent() and sets their score to newScore.

In main(), above the call of addPoints(), fix Lucy's score: fixScore(names, scores, "Lucy", 35);. Lucy now has 35 + 3 = 38 points, and the results end like this:

Lucy: 38 points, grade B
Average score: 38.2
Best: Finn (50 points)

If Lucy has 35 points, you call the fix after addPoints().

Add a check to fixScore(): if findStudent() returns -1, print Max is not in the class. with the name the function got instead of Max, and don't change the scores. Then try it out: under Lucy's fix, add fixScore(names, scores, "Max", 40);. This appears above the results:

Max is not in the class.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

6/7