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....
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
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
may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 13
Solved Threads: 2
otherwise declare void main()
Nope. Just use return 0;
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
... otherwise declare void main()
main() is anint 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:
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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
may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 13
Solved Threads: 2
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 whatI 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!
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403