C++ Trouble doing simple addition in while() loop -


hopefully can tell me i'm doing wrong. i'm reading specific point on each line within text file , trying add value value on next line , continue doing until end of file/loop. @ moment, add values first 2 lines , not...

123 + 456 + 789 = totalpayroll.

my code follows:

instream.open("staffmembers.txt");  while(getline(instream.ignore(256, '$'), line)) {     totalpayroll = stoi(line) + stoi(line); }  instream.close();  cout << "$" << totalpayroll << endl; 

my text file formatted follows:

1 | person 1 | $123 2 | person 2 | $456 3 | person 3 | $789 

in loop, you're reassigning totalpayroll value of stoi(line) + stoi(line) every line, ends being 2*789.

you need keep continuous sum:

totalpayroll = totalpayroll + stoi(line); 

this has short form using compound assignment operator, same thing:

totalpayroll += stoi(line); 

Comments