New to Functions

I'm new to functions so I'm not sure where I'm going wrong with this. It's supposed to display all perfect numbers from 1 to 10000, but instead it displays every number from 1 to 10000.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
  Name: kjkl
  Date: 10/13/10 1:06
  Description: Assignment 4, Perfect Number Functions
*/

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int possible_divisor = 1;
int sum_of_divisors = 0;
const int LIMIT = 10000;

int sum_of_possible_divisors(int number){
    while (possible_divisor < number){
          if(number % possible_divisor == 0)
          sum_of_divisors = sum_of_divisors + possible_divisor;
          possible_divisor++;
          }}
bool is_perfect(int number){
     if (sum_of_possible_divisors(number) != number)
        return false;
     return true;}

int main(){
    ofstream outfile;
    outfile.open("perfect.out");
    if (outfile.fail()) {
       cerr << "Can't open perfect.out for input\n";
       return 1;
       }
    for (int number = 1; number <= LIMIT; number++){
          if (is_perfect(number))
             outfile << "Just thought I'd let you know that "<< number << " is perfect.\n";
             }
    outfile.close();
    return 0;
}

                                  

    
What means a perfect number?
A perfect number is an
integer p>1 such that the sum of the positive divisors of p equals p. For ex-
ample, 6 is a perfect number since 1+2+3 = 6; 28 is a perfect number since
1+2+4+7+14 = 28.
I think the problem lies here:

1
2
3
4
bool is_perfect(int number){
     if (sum_of_possible_divisors(number) != number)
        return false;
     return true;}


I'd do like this:

1
2
3
4
5
6
7
bool is_perfect(int number){
     if (sum_of_possible_divisors(number) != number)
        return false;
     else{
     return true;}

}



But I could very well be wrong, i'm very tired now.
gtg sleep...
Last edited on
That didn't do anything. Thanks for trying.
Last edited on
I couldn't find the problem. The globals and the long variable names don't help much. But this should work:

1
2
3
4
5
6
7
8
9
bool isPerfect(int n)
{
    int temp = 0;
    for(int i = 1; i < n; ++i)
    {
        if(n % i == 0) temp += i;
    }
    return n == temp;
}

@xander333: the two snippets you posted are equivalent.

EDIT: found it, I think. Your sum_of_possible_divisors() function doesn't return anything. It shouldn't even compile, AFAIA.
Last edited on
More specifically what im supposed to do:


Write a complete C++ program to write to the fi le perfect.out all perfect numbers between 1 and a specifi ed named integer constant LIMIT. For the purposes
of the assignment, set the value of LIMIT to 10000.
The program is to de fine and use the following two functions:

int sum_of_positive_divisors(int number)

that calculates and returns the sum of all positive divisors of the integer ar-
gument number; candidate divisors of number range from 1 to number / 2.
sum of positive divisors() will only be called by is perfect().

bool is_perfect(int number)

is a predicate (a boolean-valued function) that returns true i the integer argu-
ment number is a perfect number and false otherwise. is perfect() will call
sum of positive divisors(); is perfect() will only be called by main().
I edited my post at the same time you posted, so you might have missed it.
I've tried making it return something, but when I do. It doesn't output anything at all. Just a blank page.
How about:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int sum_of_positive_divisors(int n)
{
    int temp = 0;
    for(int i = 1; i < n; ++i)
    {
        if(n % i == 0) temp += i;
    }
    return temp;
}

bool is_perfect(int n)
{
    return n == sum_of_positive_divisors(n);
}
?
It worked just changing the sum_of_possible_divisors function. Thanks. Can you explain what I was doing wrong?
Your function wasn't returning the sum of positive divisors. Since you were using global variables, you could make it void and use the global variable to pass the value around, but that's a terrible use of globals and it makes the program harder to follow.
I get it now thank you.
Topic archived. No new replies allowed.