Please Help!

Ok, my program takes file input (which is an unknown number of positive integers, , outputs the original number and then uses another nested event-controlled while loop to calculate and print the sum of the digits of the number.

My problem is, it just keeps adding the running total, rather than printing each number's sum.

fout << "------------------------------" << endl
<< setw(12) << "Number" << setw(18) << "Sum of Digits" << endl
<< "------------------------------" << endl;

while (fin >> inval)
{
fout << setw(11) << inval;
while (inval > 0)
{

digit = inval % 10;
inval = inval / 10;
sum = sum + digit;
digitsum += digit;
}

fout << setw(14) << digitsum << endl;
numinput = numinput + 1;
}

total = sum++;

fout << "------------------------------" << endl << endl
<< setw(7) << " Number of Integors:" << setw(6) << numinput << endl
<< setw(5) << " Overall Sum of Digits:" << setw(5) << total << endl;


Output

------------------------------
Number Sum of Digits
------------------------------
2301 6
3302 14
1234567890 59
147483648 104
789 128
5 133
------------------------------

Number of Integors: 6
Overall Sum of Digits: 133

What I need output to be:

--------------------------------------------
Number Sum of Digits
--------------------------------------------
2301 6
3302 8
1234567890 45
147483648 45
789 24
5 5
--------------------------------------------

Number of integers: 6
Overall sum of digits: 133


Thanks!


After you output the sum (digitsum), you have to reset it to zero.
Topic archived. No new replies allowed.