0
Palindrome in C++
https://code.sololearn.com/cY9AEAmzTtqs/?ref=app Could someone tell me whatâs wrong with this code?
4 Answers
+ 3
Line number 6: both the variables `num` and `reversed` and uninitialized and will have undefined values by default. That is why using them in calculations doesn't work as expected. You need to initialize `num` with `x` and `reversed` with 0 to make the code work.
Also, on line 18, instead of
`if (reversed != x)`
use `else`. It won't make any difference in how your code works, but when you use an `if` statement, the compiler thinks that there is a possibility that the function that is meant to return bool, returns nothing. That is why, the compiler gives warning saying
"control reached end of non-void function"
+ 3
VISHNU AJAY
why did you remove the variable `num`. I said *initialize* `num` with the value of `x`. Now, you are directly changing the value of `x`, so when the while-loop is over, the value of `x` will always be 0, and hence `reversed == x` will always return false.
Instead, don't change the value of `x`, and before the while-loop make a variable with the value of `x` and use that instead of `x` inside the loop. Here is the fixed code
https://code.sololearn.com/c1og9s9YDhy8/?ref=app
+ 2
VISHNU AJAY
In your code value of x will change so it will not be equal with reverseValue.
So store x in temp then compare temp with reverseValue.
if(temp == reversed)
0
fixed what you suggested!
still giving wrong output! :/