# why do i always get true in the loop?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-11-2008
Masterx
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 )
• 09-11-2008
matsp
Code:

`  if ( (shadow&&mirror)==true )`
What does this test?

--
Mats
• 09-11-2008
C_ntua
Shadow is the first digit. Mirror is the whole number. Why would you test them? You want to test the first and the last digit I suppose? Then the second with the fourth etc etc.
In any case use the == for numbers. If you replace the && with == it will give you always that this is not a palindrome, I don't know why you say it gives you true
• 09-11-2008
Masterx
Quote:

Originally Posted by matsp
Code:

`  if ( (shadow&&mirror)==true )`
What does this test?

--
Mats

Quote:

its supposed to compare two numbers , one as the original number and the other one as the new number ( that is generated inside the while loop) !
why is it like this, is int supposed to contain all of the digits rather than only the first one ? !
here is a sample output!
• 09-11-2008
QuantumPete
Quote:

Originally Posted by Masterx
its supposed to compare two numbers , one as the original number and the other one as the new number ( that is generated inside the while loop) !
why is it like this, is int supposed to contain all of the digits rather than only the first one ? !

You misunderstand the && operator and boolean values in C/++ ANYTHING that's non-zero is TRUE. So if you have num = 55555 and shadow = 5 (or whatever) you do:
5 && 55555, which is evaluated to 1 && 1, which is TRUE AND TRUE == TRUE
Your number comparison is therefore wrong.
• 09-11-2008
Masterx
Quote:

Originally Posted by QuantumPete
You misunderstand the && operator and boolean values in C/++ ANYTHING that's non-zero is TRUE. So if you have num = 55555 and shadow = 5 (or whatever) you do:
5 && 55555, which is evaluated to 1 && 1, which is TRUE AND TRUE == TRUE
Your number comparison is therefore wrong.

thanks,but how to avoid that when using "&&" operator? ! is it even possible to use && for comparison ?or lets say how/where should i use that operator ? !
and again should i only use == operator in solving this assignment! ?
• 09-11-2008
matsp
Quote:

Originally Posted by Masterx
thanks,but how to avoid that when using "&&" operator? ! is it even possible to use && for comparison ?or lets say how/where should i use that operator ? !
and again should i only use == operator in solving this assignment! ?

The && operator checks if both sides are non-zero (true) or not. If both sides are non-zero, then the result is true, if either or both sides are zero, then it's false.

So you can't use it to check if the numbers are equal - you'd use == for that - that is what == is for.

--
Mats
• 09-11-2008
tzakieta
If I understood corectly, it seems logic to compare 2 numbers like this:

Code:

`if(shadow == mirror) {}`
and you should do this for each digit/char in the number/string.
• 09-11-2008
C_ntua
I run you first program, but I guess you already knew it doesn't work. My bad.
Your second program doesn't seem wrong (as an idea) to solve the problem. You don't have to limit it for 5 digits.
Count the digits, store it to an int, divide by 2 (works for both odd and even number of digits). Let call the int pal. Then do 10^pal. So now you can do /10^pal and %10^pal and see if it is a palindrome or not using the == as noted above.

I am a bit confused of what was your original code exactly and why you don't like the second solution you give...
• 09-11-2008
matsp
The second solution doesn't work on 12321 - since it compares 12321 %100 => 21 with 12321 / 1000 => 12.

--
Mats
• 09-11-2008
C_ntua
Quote:

Originally Posted by matsp
The second solution doesn't work on 12321 - since it compares 12321 %100 => 21 with 12321 / 1000 => 12.

--
Mats

True. My previous suggestion is also wrong (for even two reasons)!

Easiest way to go is compare one by one digits.
Count number of digits. Then for the first digit do num%10^1 and num /10^(numDigits-1). For the second digit you do (num%10^2 ) / 10^1 and (num/10^(numDigits-2))%10^1.

Generally in a loop this should work
(num%10^i)/10^(i-1) and (num/10^(numDigits-i))%10^(i-1)
starting from i=1 to numDigits/2.

In this example 12321 you would get for the second check:
(12321%100)/10 = 21/10 = 2
(12321/1000)%10 = 12%10 = 2

Haven't tested it but you get my idea how this can be done inside a loop for a variable number of digits
• 09-11-2008
Masterx
thanks alot ..
but please note that im not supposed to use any kind of array including strings ( that is a kind of array i think ) ...
i didnt like the second solution i used, because i think its a humble way of solving a problem without knowing why the previous one fails ! so cuz i couldnt understand why " &&" operator generates that result, i couldn't leave it alone ! thats it ....
the second solution fails too because i forgot that "12321" is a palindrome number , i thought
palindrome number is sth like "55555" or "12312" ... i didnt pay a careful attention to the question itself assuming i know the details well enough to make it work. My bad , i was wrong !

it wasnt until that mats and C_ntua pointed out that shadow only takes one digit (still dont know why it only takes the first digit not the last one since its inside a loop! ) that i found out about my first mistake.

anyway, thanks alot specially for && and == operators ... i knew about nonzero stuff in C++ , but i now can say that i comprehend it thoroughly! thanks again ...

( if there is anything else that is mistaken in my sample program , id be very thankful if you mention it !(or again them !)

gotta find another way to solve this problem ? or should i continue refining the second solution ?
c_ntua i will consider your sample code and algorithm , thanks again.
• 09-11-2008
matsp
You can reverse a number using two variables and two lines of code [or maybe three], using % 10, * 10 and / 10 inside a loop. Once you have the reversed number, just compare it with the original one (you'd have to keep a copy of that if you "destroy it" in the loop above, which you probably need to do).

As an alternative, you can pick off the top and bottom digits and compare if the top and bottom is equal. Keep doing that until you have nothing to compare (zero or one digit left).

--
Mats
• 09-11-2008
C_ntua
Note that these solutions DON'T use an array. A way to solve it with an array is to put in an array one by one digits. There are already functions that convert a number to a string. So if you could use an array (thus string) it would be fairly simple, since you can perform a check with the first and the last element of the array the second with etc etc
• 09-11-2008
Masterx
tanx again , here is the code i wrote ,
again it doesnt feel good to me !, cuz i couldnt implement what mats told me to ! ,
i couldnt figure it out how it is possible to reverse a number when
first: you dont know how many digits it has
second:how to write it down in just 3 lines of code!
---------
it is do able i mean , i can place a counter to count the number of digits , but i cant get the reversed number in only one loop as mats noted ! if i try to do what mats told me , first there would be a loop (a while for example) , and i would use division and modulus to separate the digits ! mean while a counter counts the digits , here when the number of digits are revealed , i use "number=copy; " to restore the original numbers data(meaning the original number again) , and once again place a loop (again that while ) and this time i reverse the number ! or to make it shorter simply i would place a goto label (which destroys the structured programing style) or a continue (after number=copy) to transfer the control to the while continuation condition again (and inside the loop would be a condition that test a variable "test==2" indicating this is the second time we enter the loop and there , i get the number reversed !!!

see its more than just 3 lines of codes , ,,,
i would be very thankful if you do me a favor and show me how it is possible to get a number reversed in 3 lines of code , it would be a great help to me cuz im looking for the simplest way possible to a solution ! ...

and after all here is the code i wrote to check a number for being palindrome ..
Code:

```//In the name of God #include <iostream> using std::cout; using std::cin; using std::endl; #include <cmath> using std::pow; int main() {     int digit=5;     int number;     int copy;     int pal_check=0;     int temp;     int shadow,counter=1;     cout<<"\n\n                          In The Name Of God"         <<"\n                          Palindrome Assignment!"         <<"\n\n\n Please Enter a 5 digit number (ex.55236 or 22222) : ";         cin>>number;         copy=number;         cout<<"\nWhen \n";     while (number!=0)     {         temp=number/10;         shadow=number%10;         number=temp;         pal_check+=shadow*pow(10.0,--digit);         cout<<" Iteration = "<<counter++<<" digit = "<<shadow<<" Reversed number is "<<pal_check<<endl;     }     cout<<"\n pal_check result "<<pal_check<<".";     if ( pal_check==copy )     {         cout<<"\n\nThe number you have Entered is a palindrome number\n\n";         cout<<"The reversed number is "<<pal_check<<" And The Original one is "<<copy;     }     else     {         cout<<"\n\nThe Number you have Entered Is not a Palindrome  "             <<"\n\n\nDetails:\n"             <<"The reversed number is "<<pal_check<<" And The Original one is "<<copy             <<" .\nSo As you see they are not The same in reversed Order!\n";     }     return 0; }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last