By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,678 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,678 IT Pros & Developers. It's quick & easy.

Logical error when 0 is input into loop

P: 64
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...

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4. int main()
  5. {
  6. int sum,sum1=0,sumt=0,num,root,dev,counter=0;
  7. const int SENTINEL=-100;
  8.  
  9. cout<<"ENTER #<enter exit to exit> : ";   //constant and integers
  10. while (num)                      //the loop that reruns the prog due an invalid entry                           
  11. {
  12.     cin>>num;                            //# entered by user
  13.     counter++;
  14.     if (num==SENTINEL)                   //a # to exit the prog
  15.         break;
  16. if (0<num && num<1001)                   //declaring the range
  17. {     sum=0,sum1=0,sumt=0;
  18.     root=pow(num,0.5);                   //second root of that #
  19.     cout<<"devisors for input "<<counter<<" <#"<<num<<"> are: 1 "<<"  "<<num<<"   ";
  20.  
  21.     for (root;1<root;root--)             //the loop and conditions
  22. {
  23.         if(num%root==0)                                  
  24. {
  25.     dev=num/root;
  26.     cout<<root<<"    "<<dev<<"     ";   //formula to calculate the sum of products
  27.     sum=dev+root;
  28.     sum1+=sum;
  29.  
  30.  
  31.  
  32. }
  33.        else
  34.     continue;      
  35.  
  36. }
  37.     sumt=sum1+1;
  38.     cout<<"SUM="<<sumt<<endl<<endl;          //determination for perfect number
  39.     if(sumt==num)
  40.         cout<<num<<" is a perfect Nmber"<<endl;
  41.  
  42. }
  43.     else
  44.     {
  45.         cout<<"Input "<<counter<<" <#"<<num<<"> is an invalid number.Number must be between 1 and 1000. Please     enter another number.\n"<<endl;
  46. continue;               //error for invalid entry and loop for back to data entry
  47.     }
  48. }
  49.  
  50.     return 0;
  51. }
Oct 25 '07 #1
Share this Question
Share on Google+
6 Replies


rhitam30111985
100+
P: 112
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;

}[/code]
num is 0... SENTINEL is -100... 0!=-100.. if condition is false ... hence ...rest of the program will be excecuted .. unless i am missing something ..
Oct 25 '07 #2

amitpatel66
Expert 100+
P: 2,367
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...

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4. int main()
  5. {
  6. int sum,sum1=0,sumt=0,num,root,dev,counter=0;
  7. const int SENTINEL=-100;
  8.  
  9. cout<<"ENTER #<enter exit to exit> : ";   //constant and integers
  10. while (num)                      //the loop that reruns the prog due an invalid entry                           
  11. {
  12.     cin>>num;                            //# entered by user
  13.     counter++;
  14.     if (num==SENTINEL)                   //a # to exit the prog
  15.         break;
  16. if (0<num && num<1001)                   //declaring the range
  17. {     sum=0,sum1=0,sumt=0;
  18.     root=pow(num,0.5);                   //second root of that #
  19.     cout<<"devisors for input "<<counter<<" <#"<<num<<"> are: 1 "<<"  "<<num<<"   ";
  20.  
  21.     for (root;1<root;root--)             //the loop and conditions
  22. {
  23.         if(num%root==0)                                  
  24. {
  25.     dev=num/root;
  26.     cout<<root<<"    "<<dev<<"     ";   //formula to calculate the sum of products
  27.     sum=dev+root;
  28.     sum1+=sum;
  29.  
  30.  
  31.  
  32. }
  33.        else
  34.     continue;      
  35.  
  36. }
  37.     sumt=sum1+1;
  38.     cout<<"SUM="<<sumt<<endl<<endl;          //determination for perfect number
  39.     if(sumt==num)
  40.         cout<<num<<" is a perfect Nmber"<<endl;
  41.  
  42. }
  43.     else
  44.     {
  45.         cout<<"Input "<<counter<<" <#"<<num<<"> is an invalid number.Number must be between 1 and 1000. Please     enter another number.\n"<<endl;
  46. continue;               //error for invalid entry and loop for back to data entry
  47.     }
  48. }
  49.  
  50.     return 0;
  51. }

I think the program terminates because WHILE(0) returns false when num is passed as 0
Oct 25 '07 #3

P: 64
I think the program terminates because WHILE(0) returns false when num is passed as 0
Ok tanx. but why? 0 is like the other numbers ,isnt it?
what should i do about it?
Oct 25 '07 #4

P: 64
num is 0... SENTINEL is -100... 0!=-100.. if condition is false ... hence ...rest of the program will be excecuted .. unless i am missing something ..
yes u r right, but that is my problem
num=0 and SENTINEL=-100
so num!=-100
and the rest should be executed.
and it gives the right error ((( 0is an invalid number.Number must be between 1 and 1000. Please enter another number)). then in terminates the WHILE(num) and the program!!!!?
Oct 25 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
Ok tanx. but why? 0 is like the other numbers ,isnt it?
A while loop has a true/false condition to see if it should keep looping. Most (if not all) C/C++ compilers will implement 0 as false and any other number as true (including negative numbers).

what should i do about it?
Not pass it 0 until you want it to exit.
Oct 25 '07 #6

P: 64
A while loop has a true/false condition to see if it should keep looping. Most (if not all) C/C++ compilers will implement 0 as false and any other number as true (including negative numbers).
TANX very much now at least i know why 0 is not working!!!!!


Not pass it 0 until you want it to exit.
i think thats what i'll do. i guess there is nothing else i can do right?
THANK YOU
Oct 26 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.