Need help finding a logic error

I'm working on a palindrome function for numbers. Whenever I enter a 3 digit number, the reversed number loses a value of 1. I can't find what is causing this and I'm hoping that someone else can spot it.

This is the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
   bool is_palindrome(size_t x, bool yes = false)
    {
        size_t multiplier = 0;
        int backwards = 0;
        size_t y = x;

        while(y / 10 > 0)
        {
            y /= 10;
            ++multiplier;
        }

        y = x;

        for(int i = multiplier; i > -1; --i)
        {
            backwards += (y%10)*pow(10, i);
            y /= 10;
        }

        if(yes)
        cout << backwards << endl;

        return (backwards == x);
    }


In main, for example, I have:

 
is_palindrome(431, true);


I'm getting an ouput of 133 when i should be getting 134. I believe this happens for all 3 digit numbers. Any ideas why ?
It works for me if you change the backwards variable to a size_t.
Thanks man. I tried that. Still no luck. I have no idea what is going on.
I misspoke: Change the backwards variable to a double. Also, try putting a debug statement in the for-loop to see the various values as "i" is decremented.
Last edited on
Topic archived. No new replies allowed.