hey im stuck on this problem. whatever i do, i cant get out of the infinite while loop when a negative number is entered. i want it to end the program when a number less than 0 is entered. when i put any negative number, it repeats
"Divisors for "num" are:
1"
thanks
Code:
#include <iostream>
using namespace std;
int main()
{
int x, num, sum=0, test;
while (num>0) {
cout<<"\nEnter a positive number between 1 and 1000."<<endl;
cout<<"Enter -99 to exit.\n"<<endl;
cin>>num;
do {
test=5;
cout<<"\nDivisors for "<<num<<" are: "<<endl;
cout<<" 1";
for (x=2;x<num;x++)
{
if ((num%x)==0)
{
cout<<" "<<x;
test=0;
sum=(sum+x);
}
else if (test!=0)
test=1;
}
sum=(sum+1);
if (num==sum)
cout<<"\nThe number "<<num<<" is a perfect number"<<endl;
if (test==1)
{ cout<<" "<<num<<endl;
cout<<"\nThe number "<<num<<" is prime."<<endl;
}
} while (num<0 || num>1000);
}
system("pause"); //Pauses the console
return 0;
}