459,678 Members | 1,285 Online
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 #include #include using namespace std; int main() { int sum,sum1=0,sumt=0,num,root,dev,counter=0; const int SENTINEL=-100;   cout<<"ENTER # : ";   //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 are: 1 "<<"  "< is an invalid number.Number must be between 1 and 1000. Please     enter another number.\n"<
6 Replies

 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

 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 #include #include using namespace std; int main() { int sum,sum1=0,sumt=0,num,root,dev,counter=0; const int SENTINEL=-100;   cout<<"ENTER # : ";   //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 are: 1 "<<"  "< is an invalid number.Number must be between 1 and 1000. Please     enter another number.\n"<

 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

 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