473,395 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Logical error when 0 is input into loop

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
6 1847
rhitam30111985
112 100+
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
2,367 Expert 2GB
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
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
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
4,677 Expert Mod 4TB
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
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

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

Similar topics

8
by: sachin bond | last post by:
The following code(in c++) is supposed to divide a file into 'n' different files. suppose i'm having a file called "input.zip". The execution of the following code should divide "input.zip"...
5
by: hpy_awad | last post by:
I wrote that example from a book and there is en error in the display module that it does not showing all the records are entered in the input module. I traced with some printf statments without...
8
sammyboy78
by: sammyboy78 | last post by:
I'm trying to create a class "WeeklyPay" that contains the methods that class "WeeklyPayTest" will use to compute the weekly pay of an employee when the user inputs employee name, hours worked, and...
9
by: Soneji | last post by:
Hello yet again! I seem to be having a problem with a logical OR inside a while loop. Basically, I've got it taking user input for a string, and I want them to type 'exit' to... well, exit. ...
4
by: Hypnotik | last post by:
Hello everyone. I'm working on a program that crashes whenever I enter the value that is supposed to stop the program. The program takes in various pieces of info, has a struct and a class. The...
13
by: problem. | last post by:
#include <stdio.h> #include <stdlib.h> int main(void) { int a, b; float result = 0.0f; char symbol = '\0'; int loop = 0;
0
by: forgedascendant | last post by:
Good day everyone, I am new to this site so please forgive me if this post isn't completly correct. For the past 2 weeks I have been beating my head against the wall trying to figure out what is...
3
by: anchitgood | last post by:
I have tried a lot but unable to correct just 1 logical error in the following code. This error is in the for loop, when fine and minutes are printed as the garbage values. Please check it out: ...
7
vikas251074
by: vikas251074 | last post by:
I am getting error above in following code since few days giving tension day and night. How can I solve this? I am facing since Oct.25. in line no. 362 After doing a lot of homework, I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.