# Calculating Sum of Integers of Unknown Length

• 10-26-2010
egnorth
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;         }         //output table heading         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.
• 10-26-2010
stahta01
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.
• 10-26-2010
egnorth
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;         }         //output table heading         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; }```
• 10-28-2010
ronin
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;```