Determine integer in .txt file

I am making an eVoting program which takes input from .txt file and outputs in the same .txt file. I need to ask the user to enter the candidate they wish to vote and then read the previous tally from the .txt file and add one to it. The problem is determining the numbers in a .txt file and adding one to it.

For example voting_Tally.txt contains:

Bloomberg 1234
Bill De Blasio 6789

How would it be possible to first determine the name and then add one to their tally. For Example:

Bloomberg 1235
Bill De Blasio 6790
Ideally you would have data with an obvious deliminator. But since you don't, you have to test each character to determine what data goes where. Something like this:

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
54
55
56
57
58
59
60
#include <iostream>
#include <cctype>
#include <string>
#include <fstream>
#include <vector>


class CANDIDATE
{
private:
    std::string Name;
    unsigned Votes;

public:

    CANDIDATE(std::string Data_Line)
    {
        size_t Pos = 0; //Declare 'Pos' OUTSIDE of the for loop so that it can be used anywhere within the constructors scope.
        for(Pos = 0; !isdigit(Data_Line[Pos]) && (Pos < Data_Line.length()); ++Pos) ; //Find position of first numerical character.

        //Store temporary objects in member variables.
        Name = Data_Line.substr(0, Pos - 1);
        Votes = std::stoul(Data_Line.substr(Pos, std::string::npos), nullptr);
    }

    unsigned Vote_For() //Not used in this example.
    {
        return ++Votes;
    }

    friend std::ostream& operator<<(std::ostream& Out, CANDIDATE& Candidate); //Allow operator access to otherwise private data.
};

std::ostream& operator<<(std::ostream& Out, CANDIDATE& Candidate)
{
    Out << Candidate.Name << " " << Candidate.Votes; //Fill std::ostream

    return Out; //Return std::ostream
}

int main()
{
    std::ifstream iFile("In.txt", std::ios_base::in);
    std::string Data_Line;
    std::vector<CANDIDATE> Candidates; //This can be replaced with a normal array of constant size. If done, modify the while & for loops accordingly.

    while(std::getline(iFile, Data_Line)) //Store data read from 'iFile' in 'Data_Line' while "std::getline()" returns true.
    {
        Candidates.push_back(CANDIDATE(Data_Line)); //Creates a TEMPORARY object that std::vector.push_back then uses the default copy operator to create and store an instance from.
    }

    for(unsigned i = 0; i < Candidates.size(); ++i) //Iterate through vector
    {
        std::cout << Candidates[i] << "\n"; //Output data from overloaded stream operator
    }

    iFile.close();

    return 0;
}


My guess is that there are one or two features here the you can't use because you "haven't been taught that yet" and a lot of instructors seem compelled to make a point of squashing any attempt to show initiative. I'm not saying "Here, copy this" but if you need me to make adjustments to make the reference easier then we can.
Last edited on
Thank you so much for your help.
Topic archived. No new replies allowed.