Equation not working

Hey there, so I have a program that is supposed to do an equation to get a special number but for some reason when I run it, this is how it comes out in the console when I choose B and follow the steps.

a: Display an example
b: Try it out yourself
c: Exit

make your choice, press enterb
Enter a 3 digit number, whose first digit is greater than its last
801
The number you entered is:801
The reverse of your input is 1
The reverse of your input is 10
The reverse of your input is 108
Subtraction of the reversal from the original number is -108
The reverse of the resulting number is 0
Addition of the number to the un-reversed form is -10 our magic number
Press any key to continue . . .

And this is the code. Any help would be great.


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 #include "stdafx.h"
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int number1, number2 = 0, number3 = 0, number4 = 0, number5 = 0, ex;
	int reverse(int num);

	char choice;

	cout << "a: Display an example\n";
	cout << "b: Try it out yourself\n";
	cout << "c: Exit\n";
	cout << "\n";
	cout << "make your choice, press enter";
	cin >> choice;
	cin.ignore(numeric_limits<streamsize>::max(), '\n'); // http://www.cplusplus.com/forum/beginner/121194/

	if (choice == 'A' || choice == 'a')
	{
		cout << "First number: 901\n";
		cout << "The number reversed: 109\n";
		cout << "Subtract the reversal from the original number: 901-109=792\n";
		cout << "Finally, reverse the resulting number: 792\n";
		cout << "Add it to its un-reversed form: 297+792 and\n";
		cout << "the program will output the final result (1089 if you did it right)\n";
		cout << "PRESS ANY KEY AND ENTER TO EXIT THIS PROGRAM\n";
		cin >> ex;
	}

	else if (choice == 'B' || choice == 'b')
	{
		
		cout << "Enter a 3 digit number, whose first digit is greater than its last\n";
		cin >> number1;
		cout << "The number you entered is:" << number1<<endl;
		
		while (number1 > 0)
		{
			number2 = number2 * 10 + (number1 % 10);
			number1 = number1 / 10;
			cout << "The reverse of your input is " << number2<<endl;
			
			
		}
		number3=(number1-number2);
		cout << "Subtraction of the reversal from the original number is " << number3<<endl;
		

		{
			number4 = number4 * 10 + (number1 % 10);
			number3 = number3 / 10;
			cout << "The reverse of the resulting number is " << number4<<endl;

		}
		number5 = (number3 + number4);
		cout << "Addition of the number to the un-reversed form is " << number5 << " our magic number" << endl;

			
	system("PAUSE");
	return(0) ;
}
	}
Your while on line 40 will continue loop until number1 is no longer greater than zero, and because you're iteratively dividing it by 10, number1 will be exactly zero. Therefore line 48 will store in number3, 0 - number2, which is the same as -number2.

It seems that you forgot to write something on line 51. Probably a while.
One thing I see is you are printing to cout every iteration of the while loop on line 44; if you don't want to print ever iteration move it somewhere else.

Could you list what other problems you are having and where in the code they are occurring?
Well its running fine, as in no errors but when it gets to the math problem it begins to have issues, I tried what u/helios said but it just removed the section "the reverse of the resulting number is" from the console.
Topic archived. No new replies allowed.