• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

C++ Palindrome Number

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All, I am new to C++ programming and I'm trying to write a C++ program to find if a number is a palindrome. The problem is that the program returns false even when the number is a palindrome. Can anyone know where I can practice for palindrome problems?
 
Rancher
Posts: 396
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

If you're new to C++ it might just be a problem with the C++ rather than the palindrome side of the code. Have you tried adding output statements at various places to show you what the code is (or isn't) doing?
 
Shivam Bhatele
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my code

 
John Matthews
Rancher
Posts: 396
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I would add some output inside the loop, to output the values of i, i-1, the digits being compared, and the current value of check.
 
John Matthews
Rancher
Posts: 396
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I meant len-i, not i-1. And len (before the loop).
 
Shivam Bhatele
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still getting error
 
John Matthews
Rancher
Posts: 396
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Printing some output isn't going to fix the problem, but it will show you what/where it is going wrong.

Can you show the output you are getting after adding the output statement(s) inside the loop?
 
Marshal
Posts: 75844
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

The code in lines 10‑11 looks incorrect to me.
 
John Matthews
Rancher
Posts: 396
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the approach is not the best/easiest - it's easier to assume the number is a palindrome, then look for a reason why it isn't.

But I'm hoping a bit of debugging will demonstrate that.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic