Text File Reading Program

I'm writing a program which reads a couple of lines from a text file, each line contains a name and a score, for example: "Robert Plant: 50". The program then outputs which person had the highest score and the score of that person.

But I'm stuck on how to modify the line with the name and score, to only the name; another example: "Peter Griffin: 50" to "Peter Griffin".

I'm also not sure on how to determine which person has won, I know how to output the highest score, but how do I connect the name to the score?

My code is down below (don't mock me for it being badly written, I'm a beginner):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

void creatingName(vector<string> *rader){
    // HOW DO I DO THIS? HERE IS WHERE THE NAME IS SUPPOSED TO BE SEPARATED FROM THE SCORE.
}

void creatingNumber(vector<int> *number, vector<int> *assembledNumber){
    int n = number->size(), sum = 0;
    for(int x=0;x<n;x++){
        sum += (*number)[x]*pow(10,n-1);
    }
    assembledNumber->push_back(sum);
    number->clear();
}

void determiningRows(vector<string> *rader){
    fstream file;
    file.open("exam.txt");
    string line;
    while(getline(file, line)){
        if(line == "")
            break;
        rader->push_back(line);
    }
}

int main(){
    vector<string> rader;
    fstream file;
    vector<int> number, assembledNumber;
    file.open("exam.txt");
    determiningRows(&rader);
    for(int x=0;x<rader.size();x++){
        int n = rader[x].size();
        for(int y=0;y<n;y++){
            char c = rader[x][y];
            if(isdigit(c)){
                int a = c-48;
                number.push_back(a);
            }
        }
        creatingNumber(&number, &assembledNumber);
    }
    sort(assembledNumber.begin(), assembledNumber.end());
    // NOT FINISHED HERE!
    file.close();
}


PS. If you have any suggestions for shortening the code that I've already written, go ahead.
Last edited on
1
2
3
4
5
std::string name;
int score;
std::geline( std::cin, name, ':' );
std::cin >> score;
std::cin.ignore();
Topic archived. No new replies allowed.