#include <iostream>
#include <string>
usingnamespace std;
int checkprime(int n)
{
int i;
int prime=1;
for(i=2;i<n/2;i++)
{
if(n%i==0)
{
prime=0;
break;
}
}
return prime;
}
int isperfect(int x)
{
int sum=0;
int i;
for(i=1;i<x;i++)
{
if(x%i==0)
{
sum=sum+i;
}
}
if(sum==x)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
string input;
do
{
int b;
int i;
int prime=1;
cout << "**************************************************" <<endl;
cout << "Enter number: ";
cin >> b;
cout << "**************************************************" <<endl;
if (b<0|| b>1000)
{
cout << "Please enter an integer that is within the range." <<endl;
cout <<"**************************************************" <<endl;
cout << "Do you want to quit?" << endl
<< "[Y]es [N]o: " ;
cin >> input;
cout << "**************************************************";
cout << "\nEnter number: ";
cin >> b;
cout << "**************************************************" <<endl;
}
if(isperfect(b)==1)
{
cout << b << " is a perfect number. \n";
}
else
{
cout << b << " is not a perfect number. \n";
}
if(checkprime(b)==1)
{
cout << b << " is a prime number. \n";
}
else
{
cout << b << " is not a prime number. \n";
}
{
prime=0;
cout << "Divisors of "<< b << ":" << " ";
for(i=2;i<b;i++)
if(b%i==0)
{
cout << i << " ";
prime=1;
}
b++;
}
cout <<"\n**************************************************" <<endl;
cout << "Do you want to quit?" << endl
<< "[Y]es [N]o: " ;
cin >> input;
} while ((input == "N") || (input == "n"));
return 0;
}
**************************************************
Enter number: 1212
**************************************************
Please enter an integer that is within the range.
**************************************************
Do you want to quit?
[Y]es [N]o: n
**************************************************
Enter number: 1212
**************************************************
1212 is not a perfect number.
1212 is not a prime number.
Divisors of 1212: 2 3 4 6 12 101 202 303 404 606
**************************************************
Do you want to quit?
[Y]es [N]o:
EDIT: It would be a good programming practice if you remove the extra spaces in your program and line up the curly braces right, it helps us read the code easier so we dont need to scroll through 20-30 arbitrary lines of spaces.
Your error check doesnt check the value after you initially check it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if (b<0|| b>1000)
{
cout << "Please enter an integer that is within the range." <<endl;
cout <<"**************************************************" <<endl;
cout << "Do you want to quit?" << endl
<< "[Y]es [N]o: " ;
cin >> input;
cout << "**************************************************";
cout << "\nEnter number: ";
cin >> b;
cout << "**************************************************" <<endl;
}
once you enter b for the second time it doesnt check if b is within valid range again, it just continues on with the loop. You would need to check the code to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
while(b<0|| b>1000)
{
cout << "Please enter an integer that is within the range." <<endl;
cout <<"**************************************************" <<endl;
cout << "Do you want to quit?" << endl
<< "[Y]es [N]o: " ;
cin >> input;
cout << "**************************************************";
cout << "\nEnter number: ";
cin >> b;
cout << "**************************************************" <<endl;
}
so that WHILE b is less than zero or greater than 1000, it will continue to ask for valid input.
**************************************************
Enter number: 1005
**************************************************
Please enter an integer that is within the range.
**************************************************
Do you want to quit?
[Y]es [N]o: y
**************************************************
Enter number: