Palindrome in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Palindrome in C++

https://code.sololearn.com/cY9AEAmzTtqs/?ref=app Could someone tell me what’s wrong with this code?

2nd Mar 2021, 3:59 AM
VISHNU AJAY
VISHNU AJAY - avatar
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"
2nd Mar 2021, 4:22 AM
XXX
XXX - avatar
+ 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
2nd Mar 2021, 5:46 AM
XXX
XXX - avatar
+ 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)
2nd Mar 2021, 5:43 AM
A͢J
A͢J - avatar
0
fixed what you suggested! still giving wrong output! :/
2nd Mar 2021, 5:12 AM
VISHNU AJAY
VISHNU AJAY - avatar