# How can I write a program that gives the sum of the digits in a number

Page 1 of 1

## 11 Replies - 1755 Views - Last Post: 15 March 2011 - 06:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=222358&amp;s=b48128649e74672fecd1f64bfb0712e4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pat_454

Reputation: 1
• Posts: 5
• Joined: 15-March 11

# How can I write a program that gives the sum of the digits in a number

Posted 15 March 2011 - 05:05 PM

When I compile it, it says my errors are that 'number' and 'sum are undeclared. I can't figure out why because I already declared them but I am horrible at programming. Any advice would be appreciated. Here's what I've got:

#include <iostream>
#include <cmath>
using namespace std;

int sumDigits(long n)
{
int number = abs(n);
int sum = 0;
cout << "Enter an integer: ";
cin >> number;

while (number != 0)
{
int remainder = number % 10;
sum += remainder;
number = number / 10;
}

return sum;
}

int main()
{
cout << "The sum of digits for " << number << " is " << sum << endl;

system("PAUSE");
}

Is This A Good Question/Topic? 0

## Replies To: How can I write a program that gives the sum of the digits in a number

### #2 jimblumberg

Reputation: 5492
• Posts: 17,068
• Joined: 25-December 09

## Re: How can I write a program that gives the sum of the digits in a number

Posted 15 March 2011 - 05:12 PM

You have not declared the variables sum and number inside main.

You might want to look at this link.

Jim

### #3 MaverickDavidian

Reputation: 24
• Posts: 67
• Joined: 15-April 09

## Re: How can I write a program that gives the sum of the digits in a number

Posted 15 March 2011 - 05:13 PM

What you're encountering here is a scope issue. Presently, number and sum are only declared in the sumDigits function. You need to declare them outside both functions, and make sumDigits a void and get rid of the return statement, since it doesn't need to return anything.

### #4 pat_454

Reputation: 1
• Posts: 5
• Joined: 15-March 11

## Re: How can I write a program that gives the sum of the digits in a number

Posted 15 March 2011 - 05:29 PM

Sorry about the tags, first time posting. Does that mean that I would declare the same variables again before the last segment? This is the first time I've written something with a function. Also, thanks for the quick response.

[
#include <iostream>
#include <cmath>
using namespace std;

int sumDigits(long n)
{
int number = abs(n);
int sum = 0;
cout << "Enter an integer: ";
cin >> number;

while (number != 0)
{
int remainder = number % 10;
sum += remainder;
number = number / 10;
}

}

int main()
{
cout << "The sum of digits for " << number << " is " << sum << endl;
]

I don't even think I tagged that right

### #5 MaverickDavidian

Reputation: 24
• Posts: 67
• Joined: 15-April 09

## Re: How can I write a program that gives the sum of the digits in a number

Posted 15 March 2011 - 05:37 PM

There actually should be a code format button in the text bar above where you type. It should be next to the text alignment buttons. It says CODE.

If you re-declare it, it's just a different variable with the same name elsewhere. Look at the link that jimblumberg posted for an enlightening discussion on scope in C++ (the same rules apply in many, many other languages, so it's a worthwhile concept to grasp early and hold onto).

Finally, if you appreciate what posters have to say, please click on the green + button next to Was this Post Helpful? We at DiC get off on that.

### #6 pat_454

Reputation: 1
• Posts: 5
• Joined: 15-March 11

## Re: How can I write a program that gives the sum of the digits in a number

Posted 15 March 2011 - 05:45 PM

I understand the concept of scope after reading that article, what I don't understand is how to make the info from the function available to the main.

### #7 MaverickDavidian

Reputation: 24
• Posts: 67
• Joined: 15-April 09

## Re: How can I write a program that gives the sum of the digits in a number

Posted 15 March 2011 - 05:47 PM

pat_454, on 15 March 2011 - 05:45 PM, said:

I understand the concept of scope after reading that article, what I don't understand is how to make the info from the function available to the main.

As long as a variable is declared in the global scope (that is, outside functions), any function can access or modify it by name.

### #8 pat_454

Reputation: 1
• Posts: 5
• Joined: 15-March 11

## Re: How can I write a program that gives the sum of the digits in a number

Posted 15 March 2011 - 06:11 PM

Am I moving in the right direction here?

```#include <iostream>
#include <cmath>
using namespace std;

int sumDigits(long n);

int number = abs(n);
int sum = 0;
{
cout << "Enter an integer: ";
cin >> number;

while (number != 0)
{
int remainder = number % 10;
sum += remainder;
number = number / 10;

}

int main()

{
int
cout << "The sum of digits for " << number << " is " << sumDigits(n) << endl;

system("PAUSE");

}

```

Errors:
7 n was not declared in this scope
9 expected unquaiolified id before '{' token
9 expected ',' or ';' before '{' token

### #9 MaverickDavidian

Reputation: 24
• Posts: 67
• Joined: 15-April 09

## Re: How can I write a program that gives the sum of the digits in a number

Posted 15 March 2011 - 06:24 PM

pat_454, on 15 March 2011 - 06:11 PM, said:

Am I moving in the right direction here?

Yes, you are moving in a direction that is generally correct. "Outside the functions" doesn't just mean "outside the braces," it means globally, in the same scope that you're doing #include statements and declaring the namespace.

### #10 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: How can I write a program that gives the sum of the digits in a number

Posted 15 March 2011 - 06:32 PM

I don't see that global variables are needed here.

Here is a way without variables. I added return(0); because main() returns an integer.
```int main()
{

cout << "The sum of digits for "
<< 5678
<< " is "
<< sumDigits(5678)
<< endl;

system("PAUSE");
return(0);
}

```

Try that, then you can try creating and using a couple of variables in the main() function.

Edit:- Simplfied code.

This post has been edited by #define: 15 March 2011 - 06:35 PM

### #11 pat_454

Reputation: 1
• Posts: 5
• Joined: 15-March 11

## Re: How can I write a program that gives the sum of the digits in a number

Posted 15 March 2011 - 06:36 PM

Thanks bros, between the 3 of yas and the book I got it. It''s miller time!

```#include <iostream>
#include <cmath>
using namespace std;

int sumDigits(long n)
{
int temp = abs(n);
int sum = 0;

while (temp != 0)
{
int remainder = temp % 10;
sum += remainder;
temp = temp / 10;
}

return sum;
}

int main()
{
cout << "Enter an integer: ";
long n;
cin >> n;

cout << "The sum of digits for " << n << " is " ;
cout << sumDigits(n) << endl;

system ("PAUSE");
}

```

### #12 MaverickDavidian

Reputation: 24
• Posts: 67
• Joined: 15-April 09

## Re: How can I write a program that gives the sum of the digits in a number

Posted 15 March 2011 - 06:39 PM

pat_454, on 15 March 2011 - 06:36 PM, said:

Thanks bros, between the 3 of yas and the book I got it. It''s miller time!

You're welcome. Enjoy the brew.