Thread: why do i always get true in the loop?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497

    why do i always get true in the loop?

    hello, i really dont know why ?
    to let you know what i intend to do , please read this question "
    [Finding a palindrome number ]

    "A palindrome is a number or a text phrase that reads the same backwards as forwards.
    For example, each of the following five-digit integers is a palindrome: 12321, 55555,
    45554 and 11611. Write a program that reads in a five-digit integer and determines
    whether it is a palindrome. [Hint: Use the division and modulus operators to separate
    the number into its individual digits.]"

    after going through the usual way of separating of digits ... to show that if two numbers
    (the original one and the new generated number) i used the logical And operator (&&) ..
    so that if the two numbers are the same it would generate the number 1
    which is considered true in C++ ... and if not ,, it will produce 0 that is False.
    but when compiling the program . i always get "true" in loop continuation condition
    and thus i fail ! i tried to use equality operator (==) to check
    whether two numbers are the same , but again no luck ! i get that damn true in loop -continuation condition
    !then after some time of contemplating i come up with the second solution ,
    that only works with 5 digit numbers ! ( and im not fond of using it , cuz i think this kind of solution is not right , and it just doesnt feel good !

    now i just wana know why this happens , and where i made my mistake ! and what is (or are ) my mistake(s) ...
    any comment is greatly appreciated .
    first snippets of code:

    Code:
    //In the Name Of God
    /*
    4.26 A palindrome is a number or a text phrase that reads the same
     backwards as forwards. For example, each of the following five-digit integers
     is a palindrome: 12321, 55555, 45554 and 11611. Write a program that reads
     in a five-digit integer and determines whether it is a palindrome
    . [Hint: Use the division and modulus operators to separate the number into its individual digits.]
    
    */
     #include <iostream>
     using std::cout;
     using std::cin;
     using std::endl;
     using std::boolalpha;// causes bool values to print as "true" or "false"
    
     #include <iomanip>
     using std::setw;
    int main()
    {
        int num;
        int temp;
        int shadow;
        int mirror;/* a copy of original number is created to be compared
     with the new generated number to find out where it is a palindrome number !*/
    
    
        const int digit =5;
    
        cout<<"Please Enter a 5 digit number (ex:55555): ";
        cin>>num;
        mirror=num;
    
    /*The while works fine , but when i use && to assure that
     two numbers are the same , it always gives a True condition! why?
    */this made me to write that kind of statement.
        while( num!=0)
        {
    
            temp=num/10;
            shadow=num&#37;10;//new number to generate 
            num=temp;
    
        }
    
    
    
      if ( (shadow&&mirror)==true )
        {    
       
            cout<<boolalpha<<endl<<setw(5)<<(shadow&&mirror);
            cout<<"\n\n A Palindrome number found! ";
            cout<<"\n The number You have Entered is a Palindrome number ("<<mirror<<")\n\n";
    
        }
        else //if ( (shadow&&mirror)== false )
        {
            cout<<boolalpha<<!(shadow&&mirror);
            cout<<"\nThe number You have Entered Is not  Palindrome "<<endl;
              
        }
       
        return 0;
    }
    program number 2: using a very terrible way to solve the assignment !:
    Code:
    //In the Name Of God
    /*
    4.26 A palindrome is a number or a text phrase that reads the same backwards as forwards
    . For example, each of the following five-digit integers is a palindrome: 12321, 55555,
     45554 and 11611. Write a program that reads in a five-digit integer and determines
     whether it is a palindrome. [Hint: Use the division and modulus operators to separate
     the number into its individual digits.]
    
    */
     #include <iostream>
     using std::cout;
     using std::cin;
     using std::endl;
     using std::boolalpha;// causes bool values to print as "true" or "false"
    
     #include <iomanip>
     using std::setw;
    int main()
    {
        int num;
        int temp;
        int shadow,last2;
        int mirror,first2;
    
    
        const int digit =5;
    
        cout<<"Please Enter a 5 digit number (ex:55555): ";
        cin>>num;
        mirror=num;
    //  the while here is useless! see the comment underneath !
        while( num!=0)
        {
    
            temp=num/10;
            shadow=num%10;
            num=temp;
    
        }
    // here is the solution i found to this.! 
    first2=mirror%100;
    last2=mirror/1000;
    
        if ( (first2==last2) )
        {
            
            cout<<"\n\n A Palindrome number found! ";
            cout<<"\n The number You have Entered is a Palindrome number ("<<mirror<<")\n\n";
    
        }
        else //if ( (shadow&&mirror)== false )
        {
            
            cout<<"\nThe number You have Entered Is not A Palindrome "
                <<"\n\nfirst2 && last2 = "<<(last2&&first2)<<"  but Why?!\n\n";
        }
        cout<<"\n\n\n\n\nfirst2 digits (%100) "<<first2
        <<"\n\nFrom  "<<mirror<<endl<<endl
        <<"last2 digits (/1000) "<<last2
        <<"\n\nfirst2 && last2 = "<<(last2&&first2)<<" !\n\n";
        return 0;
    }
    thanks a million in advance !
    (by the way i use code::blocks 8.2 compiler )
    Last edited by Masterx; 09-11-2008 at 04:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a map
    By DanFraser in forum C# Programming
    Replies: 7
    Last Post: 01-23-2009, 06:23 AM
  2. Loop seg error
    By Zishaan in forum Game Programming
    Replies: 2
    Last Post: 03-28-2007, 01:27 PM
  3. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  4. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM