Have no idea what to do here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Implement the sumOfDigits() function (see below) so that the following
   // code will work properly.

   // Declare an integer for input and the sumOfDigits() function.
   int n, sumOfDigits(int);

   // Get a positive integer from the user.
   do
   {
      cout << "Enter a positive number: ";
      cin >> n;
   }
   while (n <= 0);

   // Print out the sum of that number's digits.
   cout << "The sum of " << n << "'s digits is " << sumOfDigits(n) << ".\n";


Please help me with this!!!!
Basically they want you to write a function that will sum the digits of an int. For example, the number 1013 would have an answer of 1+0+1+3 or 5. What problems were you having writing that function?
Do you mean while n <=0? You have declared sumOfDigits (int) but how is it defined?
I think you are asking the definition of sumOfDigits(int) function...
1
2
3
4
5
6
7
8
9
10
11
int sumOfDigit(int a)
{
        int sum = 0;
        while(a > 0)
        {
        sum += a%10;
        a = a/10;
         }

         return sum;
}


this is not run on machine so please make some correction if needed. :)
Topic archived. No new replies allowed.