11 Replies - 1166 Views - Last Post: 15 March 2011 - 06:39 PM Rate Topic: -----

#1 pat_454  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,865
  • 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

Please use code tags!!!!

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

You might want to look at this link.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 MaverickDavidian  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 pat_454  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 MaverickDavidian  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#6 pat_454  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 MaverickDavidian  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • 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

View Postpat_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.
Was This Post Helpful? 0
  • +
  • -

#8 pat_454  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#9 MaverickDavidian  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • 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

View Postpat_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.
Was This Post Helpful? 1
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1368
  • View blog
  • Posts: 4,735
  • 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

Was This Post Helpful? 1
  • +
  • -

#11 pat_454  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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");
}


Was This Post Helpful? 1
  • +
  • -

#12 MaverickDavidian  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • 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

View Postpat_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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1