0

I'm a somewhat-more-than-beginner to C++ and programming. I'm working on writing a function that will take an integer and reverse its digits, e.g. reverseDigit(2376) = 6732. I have run into a problem that I absolutely cannot understand, though I have spent hours working with the code, tracing it, printing out results to track what it's doing.

The problem is that the function inexplicably (to me) works differently for ints with odd numbers of digits than for ints with even numbers of digits. However, if I print the result of each step along the way, I see no difference in what the code is doing at any step.

Here is the code for the function:

void reverseDigit(int num)
{

    int digCount = digitCount(num); 
    int flippedNum = 0; 

    for (int i = 1; i <= digCount; i++)
    {
        cout << "Run #" << i << ": " << endl;
        cout << "((num % 10)*pow(10, digCount - i)) = " << ((num % 10)*pow(10, digCount - i)) << endl;  
        cout << "num = " << num << ", flippedNum = " << flippedNum << endl; 
        flippedNum += ((num % 10)*pow(10, digCount - i)); 
        num = num / 10; 
    }

    cout << "The new number is " << flippedNum << endl; 
}

And here are the results I get for an even int and then an odd int (with intermediate results printed):

Text

So you see what is going wrong here: when I enter 256 (or any int with odd number of digits), somehow flippedNum gets decremented by 1. I cannot for the life of me find where in the code I have told the computer to do this! flippedNum is initialized to 0, and the value I add to it is equal to 600, yet somehow flippedNum gets the value 599!

nothingIsMere
  • 273
  • 2
  • 3
  • 6
  • Are you aware that `pow` returns a *floating-point number*, not an *integer*? – walnut Mar 22 '20 at 05:18
  • I was aware of that, but I wasn't mindful of it while writing my code. I'm sure the problem lies there. – nothingIsMere Mar 22 '20 at 05:34
  • The problem is very likely that `pow` returns a value slightly under `100`, but `cout <<` will round to a certain number of decimals, resulting in "exactly" `600`. But when you add the same value to the integer `flippedNum`, the result of the summation is cast back to the integer type and in doing so gets truncated, rather than rounded. That is why I linked the question about `pow`. – walnut Mar 22 '20 at 05:50
  • 1
    @GhostRepeater -- Reversing the digits of a number does not need any usage of `pow` or even anything to do with powers (even if `pow` gave the exact answer you're looking for, or anything to do with counting the number of digits.. All that is required is division by 10, modulus of 10, and addition / multiplication of integers. There are plenty of examples, even written in other languages that can easily be translated to C++, that shows a 2 or 3 line loop to do the work. – PaulMcKenzie Mar 22 '20 at 06:22
  • @GhostRepeater Since the question is now closed, [look at this example](http://coliru.stacked-crooked.com/a/10864864f5b4a9bf). That is more or less the defacto way of solving the "reverse the digits" question. – PaulMcKenzie Mar 22 '20 at 06:30

0 Answers0