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");
}
11 Replies - 872 Views - Last Post: 15 March 2011 - 06:39 PM
#1
How can I write a program that gives the sum of the digits in a number
Posted 15 March 2011 - 05:05 PM
Replies To: How can I write a program that gives the sum of the digits in a number
#3
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
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
[
#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
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.
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
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
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
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?
Errors:
7 n was not declared in this scope
9 expected unquaiolified id before '{' token
9 expected ',' or ';' before '{' token
#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
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
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.
Try that, then you can try creating and using a couple of variables in the main() function.
Edit:- Simplfied code.
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
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
Re: How can I write a program that gives the sum of the digits in a number
Posted 15 March 2011 - 06:39 PM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|