# Calculating Sum of Integers of Unknown Length

This is a discussion on Calculating Sum of Integers of Unknown Length within the C++ Programming forums, part of the General Programming Boards category; My assignment, which I've attached (.pdf), is to write a C++ program that will accept a file containing an unknown ...

1. ## Calculating Sum of Integers of Unknown Length

My assignment, which I've attached (.pdf), is to write a C++ program that will accept a file containing an unknown number of integers as input. For each number in the input file, output the original number and the sum of the digits of the number.

Here's what I have so far:
Code:
```// CS 1361
// Lab27

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

int main()
{
int number, digit, sumDigits;
ifstream fin; // Input File
ofstream fout; // Output File
fin.open("lab27.dat");

if (!fin)
{
cerr << "Error opening input file." << endl;
return 1;
}
fout.open("lab27.out");

if (!fout)
{
cerr << "Error opening output file." << endl;
return 1;
}

fout << setfill('-') << setw(27) << '-' << endl;
fout << "Number Sum of Digits" <<endl;
fout << setfill('-') << setw(27) << '-' << endl;
fout << setfill(' ');

while (fin >> number)
{
sumDigits = 0;
fout << number;
if (number != 0)
{
sumDigits = sumDigits + (number % 10);

}
}
fout << setfill('-') << setw(27) << '-' << endl;

//close the files
fin.close();
fout.close();
return 0;```
I'm having some trouble with this... Any guidance will be hugely appreciated.

2. Code:
```if (number != 0)
{
sumDigits = sumDigits + (number % 10);

}```
Why are you using "if" here;

if number was "123" then you would get "3", I think the above section of code needs something to make 2 and 1 added to sumDigits.

Hint: For problems like this you normally use integer division to shift through number's digits.

Note: I deleted my first post because I mistook ">>" as C shift op.

Tim S.

3. stahta01: Thanks for your reply. Here's the solution I came up with.
Code:
```#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

int main()
{
int number, digit, sumDigits;
ifstream fin; // Input File
ofstream fout; // Output File
fin.open("lab27.dat");

if (!fin)
{
cerr << "Error opening input file." << endl;
return 1;
}
fout.open("lab27.out");

if (!fout)
{
cerr << "Error opening output file." << endl;
return 1;
}

fout << setfill('-') << setw(27) << '-' << endl;
fout << "Number        Sum of Digits" << endl;
fout << setfill('-') << setw(27) << '-' << endl;
fout << setfill(' ');

while (fin >> number)
{
sumDigits = 0;
fout << setw(11) << right << number;
while(number != 0)
{
digit = number % 10;
sumDigits = sumDigits + digit;
number = number / 10;
}
fout << setw(16) << right << abs(sumDigits) << endl;
}
fout << setfill('-') << setw(27) << '-' << endl;

//close the files
fin.close();
fout.close();
return 0;
}```

4. for grins (and nothing more), you could try a string object and the sstream class.

Code:
```string number = "123";
int sum = 0, nval;

for(int i=0; i < number.length(); ++i) {
string nstr(number, i, 1);
istringstream sdigit(nstr);
sdigit >> nval;
sum += nval;
}

cout << number << " sum of digits is " << sum;```