The first thing your inner loop does is read from the file into Positive_Integers . However, the first number was already read to determine if it was a sentinel in the outer loop. So you're basically discarding the first number. The next number should be read after the current number is displayed and added to the total.
However, your inner loop isn't meaningful (in fact, it can produce erroneous output depending on the contents of the file). It can be removed entirely in favor of a single loop like so:
datain >> Positive_Integers;
while (Positive_Integers != sentinel)
{
cout<<count<<setw(4)<<"|"<<setw(20)<<Positive_Integers<<"\n"<<endl;
The_Sum = The_Sum + Positive_Integers;
datain>>Positive_Integers;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54
Increment count in the loop:
while (Positive_Integers != sentinel) {
cout<<count++<<setw(4)<<"|"<<setw(20)<<Positive_Integers<<"\n"<<endl;
The_Sum = The_Sum + Positive_Integers;
datain>>Positive_Integers;
}
Ok nevermind. I forgot that I needed to add this:
You've re-added the bug I removed. :icon_rolleyes:
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54
I'm not really sure why I re-added it.
I suspect because you didn't fully understand my first post before running off and playing with your code. I wouldn't recommend mindless copy/paste of code you find on the web.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54