Member Avatar for dmmckelv

I am new to C++. This is a homework problem. So if anyone gives up their time to help me it is greatly appreciated. I have perfect number program, I can get it to run if all my code is in the main. The assignment requests that I use a function to check numbers to see if they are perfect. I am getting an error when I use the function. I checked MSDN to find some help to no avail. So here is the code. Thanks again for any help!:cheesy:

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int perfect (int);

int main ()
{
    int number = 1;
    int perfect = 0;
    
    for(number=1; number < 1000; number++)
    {
        perfect = perfect(number);  //here is where I call the function
        if (perfect > 0)
        {
            cout << perfect << " is a perfect number." << endl;
            cout << "It's factors are: ";

            for ( int y = 1; y < perfect/2; y++ )
            {
                int divisor = perfect / y;
                
                if ( perfect % y == 0 && y <= divisor)
                {    
                    cout << divisor << " " << y << endl;
                }
            } // ends factor perfect
        } // ends if perfect
    } // ends for to 1000
}// ends main

    int perfect (int counter)  //here is where the function starts
{
    int sum = 0;
    int divisor = 0;
    int y = 0;
    for (y = 1; y < counter/2; y++)
    {
        divisor = counter / y;
        if ( counter % y == 0 && y <= divisor)
        {
            int factors = y + divisor;
            sum += factors;
        }
    }
    if (sum - counter == counter)
    {
        return counter;
    }
    else
        return 0;
}//ends function

Hello,

The problem is, your function is called 'perfect' and your variable is too. You should change one of the two like so:

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
 
int perfect (int);
 
int main ()
{
int number = 1;
int p = 0; //instead of perfect
//etc....

yep thats fine.. also make sure you return 0 when you declare int main(), otherwise declare void main()
The final program should look like this: (I changed the name of the function perfect(...) to perfect_function(...) and i added a return 0 at the end of main)

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
 
int perfect_function (int);
 
int main ()
{
    int number = 1;
    int perfect = 0;
    
    for(number=1; number < 1000; number++)
    {
        perfect = perfect_function(number);  //here is where I call the function
        if (perfect > 0)
        {
            cout << perfect << " is a perfect number." << endl;
            cout << "It's factors are: ";
 
            for ( int y = 1; y < perfect/2; y++ )
            {
                int divisor = perfect / y;
                
                if ( perfect % y == 0 && y <= divisor)
                {    
                    cout << divisor << " " << y << endl;
                }
            } // ends factor perfect
        } // ends if perfect
    } // ends for to 1000
    return 0;
}// ends main
 
int perfect_function (int counter)  //here is where the function starts
{
    int sum = 0;
    int divisor = 0;
    int y = 0;
    for (y = 1; y < counter/2; y++)
    {
        divisor = counter / y;
        if ( counter % y == 0 && y <= divisor)
        {
            int factors = y + divisor;
            sum += factors;
        }
    }
    if (sum - counter == counter)
    {
        return counter;
    }
    else
        return 0;
}//ends function

otherwise declare void main()

Nope. Just use return 0;

... otherwise declare void main()

main() is an int function and officially cannot be be declared as void. In fact, some compilers flag a warning if void is used. Forget what M$ claims in their help. They are wrong. :confused:

return 0 returns the number 0 (obviously.. hehe) to the Operating System. This is used to denote that the function main() was successfully completed. You can use void main() but as my friends above also noted, it is not recommended... so my final thoughts...DO use

int main()
...
return 0; // to the OS

perfect = perfect(number); //here is where I call the function

Well, the problem is that you have a variable (int perfect), as well as a function named perfect, which is not allowed, so you can either change the variable name, or the function name

commented: Check the dates and avoid replying to dead threads. -6

Well, the problem is that you have a variable (int perfect), as well as a function named perfect, which is not allowed, so you can either change the variable name, or the function name

How is that any different from what I said 3 years ago in this same thread?

The problem is, your function is called 'perfect' and your variable is too. You should change one of the two

Lesson learned: Read thread first, reply later!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.