I am working in Microsoft Visual C++ 2008.

Here is my sum of digits code:

#include <iostream>
using namespace std;

int main() {

int num;
int sum=0;

cout << "Number, please? ";
cin >> num;

while (num>0) {
	sum=sum+num%10;
	num=(num-num%10)/10;
}

cout << "Sum of digits: " << sum << endl;

return 0;
}

It works, the issue is that the program needs to work for an integer of any size. INT, LONG INT, UNSIGNED INT, not good enough. How does one find the sum of digits of a number without being able to store it in the first place?

What I was thinking is somehow inputting each digit as the user types it with cin.get() but my attempts at putting it in some type of loop have failed. NB: <iostream> and <iomanip> libraries only; that restriction keeps me from storing the number as a string and working from there.

I would like only the most general advice/hints please.

Thanks in advance.

If you think even unsigned long long will be insufficient to hold the sum, there may come a case when even num would throw a out of range exception for a 100-digit number.
Your best bet in that case would be to use it as a STL string, but since you cannot use it, try a big array, arr[200].
You can try implementing your own addition using strings, based on simple kindergarten maths (carry & add) & return the result as string. You can by far make use of atoi() function on individual units to add them.

you could always write your own dynamic array class and use that to store the input into it then add up all of the digits. I'm not sure if that would be considered cheating since it uses the same approach as the STL.

you could always write your own dynamic array class and use that to store the input into it then add up all of the digits. I'm not sure if that would be considered cheating since it uses the same approach as the STL.

Probably not, if it can be done with <iostream> and <iomanip> only. Is this the case?

If you think even unsigned long long will be insufficient to hold the sum, there may come a case when even num would throw a out of range exception for a 100-digit number.
Your best bet in that case would be to use it as a STL string, but since you cannot use it, try a big array, arr[200].
You can try implementing your own addition using strings, based on simple kindergarten maths (carry & add) & return the result as string. You can by far make use of atoi() function on individual units to add them.

Thanks for the advice. Unfortunately the assessment will not involve testing numbers (in which case we could count on arr[200] being enough) but looking at the code to see if there is any dependency on the size of the number. I think this has to do with use of iostream commands that are beyond me.

@aj79 You can create you own dynamic array class for this using nothing but standard c++ keywords. You don't even need <iostream>. You should just be able you use cin.get() in a loop though for this problem. get will grab a single character at a time and you can add that to you sum. You will have to remember to subtract '0' from the char to get the actual integer.

Mr. Oliver's advice has worked- I can now see each digit of my number via cin.get(). However once I try to manipulate these numbers, such as by putting in a Total variable to add each digit, they convert to ASCII decimal codes. So for 23, instead of 2+3=5, I get 50+51=101. I have tried combinations of Total = Total + (char) a, Total = Total + (int) a, etc. without success.

How can I add these values without getting sticky ASCII numbers?

Okay to get the a char converted to an integer you have to subtract '0' from it. So if you have

char ch = '2';
int a = ch - '0';

Now a is 2 and not 50. What happens is '2' is 50 and '0' is 48 so 50 - 48 is 2.

Edited 6 Years Ago by NathanOliver: n/a

Ahhhh... yes. The code is now very alive and well.

Thanks very much.

This question has already been answered. Start a new discussion instead.