ok i have this code that executes for all # between 0 and 1001 perfectly. it is a cod to find the factors of inputed data and then determine it as a perfect number or not, also it suppose to loop bach to data entry when user inputs an out of range number.
my problem is number 0 when i input 0 the program terminates while is suppose to loop bach up with an error.
does any body see the error? if yes can you tell me?TANX
here is my code...
[Code=cpp]
#include<iostre am>
#include<cmath>
using namespace std;
int main()
{
int sum,sum1=0,sumt =0,num,root,dev ,counter=0;
const int SENTINEL=-100;
cout<<"ENTER #<enter exit to exit> : "; //constant and integers
while (num) //the loop that reruns the prog due an invalid entry
{
cin>>num; //# entered by user
counter++;
if (num==SENTINEL) //a # to exit the prog
break;
if (0<num && num<1001) //declaring the range
{ sum=0,sum1=0,su mt=0;
root=pow(num,0. 5); //second root of that #
cout<<"devisors for input "<<counter< <" <#"<<num<<"> are: 1 "<<" "<<num<<" ";
for (root;1<root;ro ot--) //the loop and conditions
{
if(num%root==0)
{
dev=num/root;
cout<<root<<" "<<dev<<" "; //formula to calculate the sum of products
sum=dev+root;
sum1+=sum;
}
else
continue;
}
sumt=sum1+1;
cout<<"SUM="<<s umt<<endl<<endl ; //determination for perfect number
if(sumt==num)
cout<<num<<" is a perfect Nmber"<<endl;
}
else
{
cout<<"Input "<<counter< <" <#"<<num<<"> is an invalid number.Number must be between 1 and 1000. Please enter another number.\n"<<end l;
continue; //error for invalid entry and loop for back to data entry
}
}
return 0;
}[/Code]
my problem is number 0 when i input 0 the program terminates while is suppose to loop bach up with an error.
does any body see the error? if yes can you tell me?TANX
here is my code...
[Code=cpp]
#include<iostre am>
#include<cmath>
using namespace std;
int main()
{
int sum,sum1=0,sumt =0,num,root,dev ,counter=0;
const int SENTINEL=-100;
cout<<"ENTER #<enter exit to exit> : "; //constant and integers
while (num) //the loop that reruns the prog due an invalid entry
{
cin>>num; //# entered by user
counter++;
if (num==SENTINEL) //a # to exit the prog
break;
if (0<num && num<1001) //declaring the range
{ sum=0,sum1=0,su mt=0;
root=pow(num,0. 5); //second root of that #
cout<<"devisors for input "<<counter< <" <#"<<num<<"> are: 1 "<<" "<<num<<" ";
for (root;1<root;ro ot--) //the loop and conditions
{
if(num%root==0)
{
dev=num/root;
cout<<root<<" "<<dev<<" "; //formula to calculate the sum of products
sum=dev+root;
sum1+=sum;
}
else
continue;
}
sumt=sum1+1;
cout<<"SUM="<<s umt<<endl<<endl ; //determination for perfect number
if(sumt==num)
cout<<num<<" is a perfect Nmber"<<endl;
}
else
{
cout<<"Input "<<counter< <" <#"<<num<<"> is an invalid number.Number must be between 1 and 1000. Please enter another number.\n"<<end l;
continue; //error for invalid entry and loop for back to data entry
}
}
return 0;
}[/Code]
Comment