1

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");
}
student
  • 111
  • 10

3 Answers3

0

"totalvictories" must be declred outside of while

int totalvictories = 0;
while (!ws(readFile).eof()){
    readFile >> data.name
        >> data.nbmatches
        >> data.nbvictories;

    data.nbvictories += totalvictories;
    cout << data.nbvictories << endl;
    cout << totalvictories << endl;
    cout << endl;
}
blank_popup
  • 231
  • 2
  • 9
  • Not only does `totalvictories` need to be initialized outside the loop, the line in the middle should be reversed: `totalvictories += data.nbvictories`. – Brian L Nov 09 '15 at 01:10
  • You could declare it as a static variable. Also, you can merge your two answers in one answer. – CroCo Nov 09 '15 at 01:30
  • You're right. I am not good at editing text. Thank you your concerning. – blank_popup Nov 09 '15 at 01:55
0

Your problem in the following code is

int totalvictories = 0; //<- a non-static variable with local scope 
data.nbvictories += totalvictories; //<- you are adding zero every iteration

This data.nbvictories is modified twice every iteration. Once when you assign it with the number you get from the file and the second with += which has not effect since totalvictories = 0. If totalvictories is supposed to store the total, then this variable should be modified not data.nbvictories, therefore, switch the statement to be totalvictories += data.nbvictories;

This is the 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 ( readFile >> data.name >> data.nbmatches >> data.nbvictories   ){

            //int totalvictories = 0;
            static int totalvictories = 0;
            //data.nbvictories += totalvictories;
            totalvictories += data.nbvictories; 

            cout << data.nbvictories << endl;
            cout << totalvictories   << endl;
            cout << endl;
        }
    }
    system("pause");

    return 0;
}

The result is

41
41

323
364

2
366

Extra: avoid using system("pause"); and eof().

CroCo
  • 4,982
  • 7
  • 47
  • 74
0

Move int totalvictories = 0 before your while loop, you are resetting it to 0 every iteration.

Michael B.
  • 145
  • 1
  • 8