writing & calling functions

Oct 25, 2011 at 10:08pm
Hi I am trying to write a function that will return true if a given whole number is perfect and otherwise false. I am not sure if I am calling the function properly or if there is something else wrong with my code in the function. I am having trouble undertanding the variables involved in functions in general.

Everytime I run this it says that the number I have input is perfect, which is obviously incorrect.

Would appriciate some feedback on what I am doing workng here, be it in the main or the function.

Thanks here is the code:



// This program uses calls a function which dertermines whether or not an
// integer entered by the user is a perfect number.

#include <iostream>
using namespace std;

bool isPerfect (int n);
//ASSUMES the user will input an integer value between 1 and 1000 inclusive.

int main ()
{
int n, x;


// cout explaining program will go here


// cout prompt to get a number to test for perfect
cout << "type an number:";


// cin of n from user
cin >> n;

//function call
if(isPerfect(n));


// cout of perfect numbers (if there are any)
cout << "The number " << n << " is perfect";

return 0;
}


//=============================================================================
// This is the function

bool isPerfect(int n)
{
int maxDivisor, sumDivisors, i;

maxDivisor = n/0.5;

for (i=1.0; i <=maxDivisor; i++)
{
if (n % i == 0)
{
sumDivisors = sumDivisors + i;
}

}
if (n = sumDivisors)
return true;
else
return false;
}

Oct 26, 2011 at 2:59am
if (n = sumDivisors)

= or ==?
Oct 26, 2011 at 6:12am
I caught that error too, but I have compiled again it is still just output any number as the perfect number.

I suspect my problem either relates to the function call or mixing up the use of my variable, as I am having difficulty understanding how to write a boolean function call.

Does it look like that is the issue? Or prehaps it is a mistake with the parameter that I am using in the function call.
Oct 26, 2011 at 1:58pm
Your suspect on function call is correct:

if(isPerfect(n));
Oct 26, 2011 at 2:06pm
if(isPerfect(n)); // <- wrong semicolon

'sumDivisors' is uninitialzed and will hence result random values.
Last edited on Oct 26, 2011 at 2:21pm
Oct 26, 2011 at 2:20pm
@taraoski
The main problem you're having, is that you are not checking the result of the variable 'result' as true or false. The next small problem was in the isPerfect routine. Dividing by 0.5 is NOT the same as dividing by 2.
Here is your program, dressed up a bit, and checking numbers 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Perfect Number.cpp : Defines the entry point for the console application.
//

#include <iostream>

using namespace std;

bool isPerfect (int n);
//ASSUMES the user will input an integer value between 1 and 1000 inclusive.

int main ()
{
	int n;
	bool result;

	// cout explaining program will go here
	cout << "\tProgram to check for a Perfect Number" << endl << endl << endl;
	cout << "A perfect number is a positive integer that is equal\nto the sum of its proper divisors."<<endl;
	cout << "The smallest perfect number is 6, which is the sum of 1, 2, and 3." << endl << endl;
	cout << "This program will check for a perfect number from 1 to 10000." << endl << endl;
	do
	{
		// cout prompt to get a number to test for perfect
		cout << "Enter the number to check : ";


		// cin of n from user
		cin >> n;
		cout << endl << endl;

		if ( n >0 && n <= 10000)
			result = isPerfect( n);
		else
			return 0;
	
		//function call
		if( result )
				// cout of perfect numbers (if there are any)
				cout << "The number " << n << ", is perfect.";
			else
				cout << "The number " << n << ", is NOT perfect.";
			cout << endl << endl;
	} while (n > 0 && n <=10000);
	cout << endl << endl;
	return 0;
}

//=============================================================================
// This is the function

bool isPerfect( int n)
{
	int i, num,sum = 0;
	num = n/2;
	for( i=1; i<=num; i++)
	{
		if (n%i==0)
			sum = sum + i;
	}

	cout << "The Sum of " << n << " is " << sum << "." << endl << endl;
	if (n == sum)
		return true;
	else
		return false;
} 
Oct 26, 2011 at 4:44pm
@friedEggHead:

Err... your function doesn't check for perfect numbers. It only checks for numbers between 1 and 1000.
Last edited on Oct 26, 2011 at 5:39pm
Oct 26, 2011 at 5:22pm
@Disch
You're not referring to my function, are you? I'm not sure, since I don't see a user named 'friedEggHead' in any of the responses. The way my routine is right now, it will show if it's a perfect number or not. The cout << "The Sum of " << n << " is " << sum << "." << endl << endl; is just to show what the sum is according to your input. Type in a 6, 28, 496 or 8,128, and it will print "The number xx, is perfect.". Anything else, is NOT perfect.
Oct 26, 2011 at 5:39pm
No I wasn't referring to yours. friedEggHead must have deleted his post.
Oct 26, 2011 at 5:45pm
Okay, thanks for the reply..
Topic archived. No new replies allowed.