So I'm trying to write a program that reads an input file and calculates the sum of numbers.
My output should look like this:
inData Sum of numbers
2301 6
3302 8
1234567890 45
But its turning out like this:
inData Sum of numbers
2301 1
1
4
6
3302 8
8
11
14
1234567890 14
23
31
38
44
49
53
56
58
59
So it keeps adding every itineration instead of stopping and going the next input number
Here is my code:
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
ofstream fout;
ifstream indata;
fout.open(" .out");
indata.open(" .dat");
if(!indata)
{
fout<<"File does not exist";
exit(1);
}
int number,
sumDigits=0,
digit;
while (indata>>number)
{
fout<<number<<setw(6);
while (number>0)
{
digit=number%10;
sumDigits = sumDigits + digit;
number=number/10;
fout<<sumDigits<<endl;
}}
return 0;
}
Here