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. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    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.
    Attached Images Attached Images

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,612
    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.
    Last edited by stahta01; 10-26-2010 at 01:13 PM. Reason: Added hint

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    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;
    }

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    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;
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a file of unknown length
    By the bassinvader in forum C Programming
    Replies: 2
    Last Post: 07-12-2006, 03:06 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. How to declare global arrays of unknown length?
    By ajm218 in forum C Programming
    Replies: 3
    Last Post: 08-07-2003, 09:13 AM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. turning strings of unknown length into arrays
    By hankspears in forum C Programming
    Replies: 3
    Last Post: 04-11-2002, 12:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21