How would you solve this?

If you had a bunch of test score and you were write a code to add a specific persons total score, any suggestions of how to do that?

For example:

sam 150
mary 200

sam 350
mary 100

sam 450
mary 2300

sam 180
mary 500

sam 10
mary 20

sam 10
mary 400

sam 600
mary 600
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <map>
#include <string>
#include <iostream>
#include <fstream>

int main()
{
std::map<std::string, int> scores;
std::ifstream input("Somefile");
std::string name;
int score;
while(input >> name >> score)
    scores[name] += score;
for(auto v: scores)
    std::cout << "Total score of " << v.first << " is " << v.second << std::endl;
return 0;
}
Last edited on
Thanks for the reply. What if the scores were randomly generated, would you use the same format?
Yes, for example:
1
2
3
4
for(int tests = 0; tests < 100; ++tests) {
    scores["Mike"] += random(101);
    scores["Jane"] += random(101);
}
Last edited on
Topic archived. No new replies allowed.