1

So I cant figure out why this is happening. This program asks the user for an input integer and either doubles the digits or triples the digits depending on whether it is an even or odd number respectively. The problem is that for only the even integer loop it subtracts the first digit by 1 for each loop iteration. The loop for the odd integers is exactly the same as the even integers with the exception tripling the digits instead of doubling and it works just fine. Any ideas?

Ex:

Input: 12
Expected output: 1122
Actual output: 1121

Input: 13
Expected output: 111333
Actual output: 111333

//This program asks the user to enter an integer and depending on whether the integer is even or odd, doubles or triples
//each digit in the integer respectively. It then reads out the result and asks the user if they would like to enter
//another integer and run the program again.
int main()
{
    string restart;
    int integer, remainder;

    while (restart != "n" && restart !="N")
    {
        cout << "Enter an integer: ";
        cin >> integer;
        cout << endl;

        //Creates variable "temp" and "mycount" and divides "integer" by 10 until remainder is 0 and counts number of
        //steps to do this to count the number of significant digits of the integer
        int temp = integer, mycount = 0;
        while (temp != 0)
        {
            temp = temp / 10;
            mycount++;
        }

        //Computes if the integer is even or odd by determining the remainder: 0 remainder = even, Non-0 remainder = odd
        //Creates two integer variables "exponent" and "sum" and sets them to 0
        //Assigns variable "temp" to the integer value
        remainder = integer % 2;
        int exponent = 0;
        int sum = 0;
        temp = integer;

        //If integer is even, doubles each significant digit
        if (remainder == 0)
        {
            //Begins for loop which runs for the number of times equal to the number of significant digits stored in "mycount"
            for (int i = mycount; i > 0; i--)
            {
                //Stores current significant digit in "digit"
                //Removes current significant digit by dividing by 10
                //Multiplies current significant digit by 11 and stores it in "timesTwo"
                //Multiplies current significant digit by 10^exponent then adds it to other modified digits
                //Adds 2 to current exponent to increase digit multiplier by 100
                int digit = temp % 10;
                temp = temp / 10;
                int timesTwo = digit * 11;
                sum = (timesTwo * pow(10, exponent)) + sum;
                exponent = exponent + 2;
                cout << sum << endl;
                cout << endl;
            }

            cout << "Number is even, doubling each digit in the integer ..." << endl;
            cout << sum << endl;
        }

        //If integer is odd this runs the same as the above function except it triples the digit and adds 3 to the multiplier
        else
        {
            for (int i = mycount; i > 0; i--)
            {
                int digit = temp % 10;
                temp = temp / 10;
                int timesThree = digit * 111;
                sum = (timesThree * pow(10, exponent)) + sum;
                exponent = exponent + 3;
                cout << sum << endl;
            }
            cout << "Number is odd, tripling each digit in the integer ..." << endl;
            cout << "Result: " << sum << endl;
        }

        cout << "Would you like to enter another integer? (y/n): ";
        cin >> restart;
    }

    return 0;
}
pja0807
  • 13
  • 2
  • 3
    Have you stepped through the code with the debugger, in order to see, for yourself, how the numbers are moved around? If not, why not? It works as expected for me, after fixing the example to be a [mcve]. – Sam Varshavchik Feb 27 '16 at 21:28
  • I don't see the bug you mentioned happening. I would suggest you NOT to use `int` for `sum`. Use `ulong` or it is for just display purposes, append value each time to a string and display the reverse of it. – Erobrere Feb 27 '16 at 21:29
  • http://stackoverflow.com/questions/7937286/return-value-of-pow-gets-rounded-down-if-assigned-to-an-integer –  Feb 27 '16 at 22:32
  • I tried using the debugger and stepping through it and it showed everything working fine until a magical -1 comes in at the very end after the first loop runs. – pja0807 Feb 28 '16 at 02:38

1 Answers1

1

To debug this, I'd print all inputs in the first loop, not just the output. The only suspicious thing I can see is that pow() returns a float and this might get rounded down in the conversion back to an integer.

Why not avoid pow() and implicit conversions altogether and use an integer factor that you multiply up each round instead?

sum = (timesTwo * factor) + sum;
factor += 100;

Btw: It's not really necessary to count the digits and have different loops for both cases -- you can simplify the core of the program to something like

bool even = (integer & 1) == 0;
int digitFactor = even ? 11 : 111;
int stepFactor = even ? 100 : 1000;
int result = 0;
while(integer != 0) {
  int digit = integer % 10;
  result += digit * digitFactor;
  integer /= 10;
  digitFactor *= stepFactor;
}
Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51
  • So I am still pretty new to coding and do not understand what is going on with the digitFactor and stepFactor lines in your example. Could you explain whats going on and how they relate to the bool statement above them? – pja0807 Feb 28 '16 at 02:40
  • digitFactor is the factor the currnet digit needs to get multiplied with to get "duplicated" or "tripled" and into the right place. stepFactor is the factor that digitFactor needs to get multiplied with at each step to "shift" the next digit to the right place (it moves digitFactor from 11 to 1100, then to 110000 etc). Printing all the values in the while loop may make more clear what is going on in detail. – Stefan Haustein Feb 28 '16 at 03:09