-4

I'm currently working on a program that outputs the number 1089 (i.e the Magic Number) of a three digit integer who's first digit is greater than its last. I have some code typed up, but am not receiving 1089, instead I'm receiving 891. Could anyone offer some explanation as to why.

 //Uses a cout to inform user will be using the number 412 as an example.
        //Uses a cout to explain to user the number needs to be reversed.
        cout << "Alright, let's walk through an example, using the number 412." << endl;
        int numExample = 412;
        cout << "Please input 412" << endl;
        cin >> numExample;
        cout << "First, the number 412 is reversed." << endl;
        //The following is done for reversing the number 412:
            int reverseNum = 0, remainder = 0;
            while (numExample)
            {
                remainder = numExample % 10;
                reverseNum = (reverseNum * 10) + remainder;
                numExample = numExample / 10;
            }
                cout << "The reverse of 412 is " << reverseNum << endl;
        cout << "Next, we want to subtract the reverse of the original number from its reverse" << endl;
        cout << "This gives us 198" << endl;
        cout << "From there, we want to reverse 198." << endl;
        //The same procedure is used to reverse 198
        int numExample2 = 198;
        cout << "Please enter 198" << endl;
        cin >> numExample2;
            int reverseNum2 = 0, remainder2 = 0;
            while (numExample2)
            {
                remainder2 = numExample2 % 10;
                reverseNum2 = (reverseNum2 * 10) + remainder2;
                numExample2 = numExample2 / 10;
            }
        cout << "The reverse of 198 is " << reverseNum2 << endl;
        int magicNumber = (reverseNum2 + numExample2);
        cout << "Following that, we want to add 891 to 189 which gives us " << magicNumber << endl;
deathcat05
  • 429
  • 1
  • 5
  • 18
  • 1
    Hint: What is the value of `numExample2` after the `while (numExample2)` loop? (If you don't know, print it.) – molbdnilo Feb 19 '16 at 18:01
  • I believe I defined it above, but `numExample2` is an int equal to 198. – deathcat05 Feb 19 '16 at 18:09
  • 1
    Really? Add `cout << numExample2 << endl;` immediately after you print `reverseNum2`. And think a bit more about what causes the loop to finish. – molbdnilo Feb 19 '16 at 18:30

1 Answers1

0

Try writing a function to handle this so your code is cleaner (It will also make it easier for people to help you!)

#include <iostream> 
#include <cmath>
using namespace std;

int reverseDigit(int num); // For example purposes

int _tmain(int argc, _TCHAR* argv[])
{
    int Number1,
    Number2,
    temp1,
    temp2,
    Result;

    cout << "Enter the number 412: ";
    cin >> Number1;

    temp1 = reverseDigit(Number1);

    temp1 = Number1 - temp1;

    cout << "Enter the number 198: ";
    cin >> Number2;

    temp2 = reverseDigit(Number2);

    Result = temp1 + temp2; 

    cout << "The magic number is: " << Result << endl; 

    return 0; 
}

int reverseDigit(int num) 
{
    int reverseNum = 0;
    bool isNegative = false;

    if (num < 0)
    {
        num = -num;
        isNegative = true;
    }

    while (num > 0)
    {
        reverseNum = reverseNum * 10 + num % 10;
        num = num / 10;
    }

    if (isNegative)
    {
        reverseNum = -reverseNum;
    }

    return reverseNum;
}

I realize you're not working with negatives so you can remove that bit if you chose to use this, you can also expand on it... That being said this will make the actual " Reversing process " easier to work with and improve upon and read.

Nacho
  • 1,104
  • 1
  • 13
  • 30
suroh
  • 917
  • 1
  • 10
  • 26
  • @molbdnilo Or just do it better, and write a function so you don't get lost in your own work! (PLEASE NOTE: The usage of you and your is general and not aimed at you personally but to any one that read the comment..) – suroh Feb 19 '16 at 18:32