Logical error when 0 is input into loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alenik1989
    New Member
    • Oct 2007
    • 64

    #1

    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...

    [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]
  • rhitam30111985
    New Member
    • Aug 2007
    • 112

    #2
    Originally posted by Alenik1989
    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 ..

    Comment

    • amitpatel66
      Recognized Expert Top Contributor
      • Mar 2007
      • 2358

      #3
      Originally posted by Alenik1989
      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]

      I think the program terminates because WHILE(0) returns false when num is passed as 0

      Comment

      • Alenik1989
        New Member
        • Oct 2007
        • 64

        #4
        Originally posted by amitpatel66
        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?

        Comment

        • Alenik1989
          New Member
          • Oct 2007
          • 64

          #5
          Originally posted by rhitam30111985
          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!!!!?

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by Alenik1989
            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).

            Originally posted by Alenik1989
            what should i do about it?
            Not pass it 0 until you want it to exit.

            Comment

            • Alenik1989
              New Member
              • Oct 2007
              • 64

              #7
              Originally posted by sicarie
              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!!!!!


              Originally posted by sicarie
              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

              Comment

              Working...