I'm trying to add numbers from a text file which looks like something like this :
player1
132
41
player2
1150
323
player3
60
2
The output should give something like this:
41
41
323
364
2
366
Where the second number of the pair would display the sum of the previous numbers' first number of the pair. However, I end up getting this:
41
0
323
0
2
0
The data seems to be displaying correctly but I don't understand why the sum isn't even though it's in the same loop. Here's my code :
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct player{
string name;
int nbmatches;
int nbvictories;
};
player data;
int main(){
ifstream readFile;
readFile.open("note.txt");
if (readFile.fail()){
cout << "not found" << endl;
}
else
{
readFile.clear();
while (!ws(readFile).eof()){
readFile >> data.name
>> data.nbmatches
>> data.nbvictories;
int totalvictories = 0;
data.nbvictories += totalvictories;
cout << data.nbvictories << endl;
cout << totalvictories << endl;
cout << endl;
}
}
system("pause");
}