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. Everytime I run this it always says that the number I have input is perfect, which is obviously incorrect.
I Would appriciate some feedback on what I am doing workng here, be it in the main or the function.
Thanks here is the code:
// Sara Ongaro csci 160
// 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);
// Returns perfect numbers up to a user specified maximum
// ASSUMES the user will input an integer value between 1 and 1000 inclusive.
int main ()
{
int testNumber, result;
// cout explaining program will go here
// cout prompt to get a the largest perfect candidate to test
cout << "largest candidate:";
// cin of n from user of largest perfect candidate
cin >> testNumber;
//function call
result = isPerfect(testNumber);
// cout of perfect numbers (if there are any)
// This is the first statement after the function is performed
cout << "The perfect number is: " << testNumber << endl;
// Determine the maximum possible divisor of n
maxDivisor = n/0.5;
// loop while i <= to the maxDivisor
for (i=1.0; i <=maxDivisor; i++)
{
// if n % i == 0 then i is a divisor of n, add the value of i to
// the sumOfDivisors
if (n % i == 0)
{
sumOfDivisors = sumOfDivisors + i;
}
}
// if n = sumOfDivisors then n is a perfect number return true to the main
if (n == sumOfDivisors)
return true;
else // n is not perfect, do nothing
return false;
int PlayerGuess =NULL;
int Answer = rand() % 100 + 1;
cout << "> Let's play a guessing game." << endl;
cout << "> Please choose a number from 1 to 100." << endl;
while(PlayerGuess != Answer){
cin >> PlayerGuess;
system("CLS");
if(PlayerGuess == Answer){
system("CLS");
cout << "> Great you won!! The number is " << PlayerGuess << endl;
cout << "" << endl;
cout << "> Press Ctrl c to exit." << endl;
}
if(PlayerGuess > Answer){
cout << "> Sorry the number is lower try again!"<< endl;
}
if(PlayerGuess < Answer){
cout << "> Sorry the number is higher try again!"<< endl;
}
if(PlayerGuess <= 0 || PlayerGuess > 100){
system("CLS");
cout << ">Invalid! You must enter a number between 1 and 100."<< endl;
cin.clear();
while(cin.get() != '\n');
}