- #1
Freyster98
- 49
- 0
I am supposed to write a program to test a user-provided number to see if it is perfect. Also, I have to use a subroutine to do the test, and call to that in the main program. Finally, I must use a boolean to control the final output. I understand that it would be SO MUCH easier if I could just write a simple program to do this without the boolean and subroutine, but an assignment is an assignment. Here is what I have so far...it runs, but no matter what number I enter when running the program, it comes back as perfect. Can anyone see what I am doing wrong? Any help would be appreciated.
Code:
#include<iostream>
#include<cmath>
using namespace std;
void Perfect(int number,int&ans,bool&valid)
{
int sum=0;
valid=false;
for(int i=1;i<=number/2;i++)
{
if(number%i==0)
sum+=i;
}
if(sum==number)
valid=true;
ans=sum;
}
void Perfect(int,int&,bool&);
char ch;
void main()
{
int num,answer;
bool found;
cout<<"Enter the number you want to test to see if it is perfect: ";
cin>>num;
Perfect(num,answer,found);
if(found=true)
{
cout<<num<<" is a perfect number.";
}
else
{
cout<<num<<" is NOT a perfect number.";
}
cin>>ch;
}