small calculator issue.....

I made a calculator that does addition, subtraction, division, and Multiplication. After messing around with it for a little bit I finally go it to compile with no errors. But the problem is when I choose a number and press enter the screen goes away. I think it has something to do with the switch and case stuff since I don’t know very much how they work.



#include <iostream>

using namespace std;

int main()
{
system("TITLE Calculator");
system("COLOR 2f");

int a, b, answer;
char choice;

cout << "press the number that corresponds to the operation you would like to perform" << endl;

cout << "1 - addition" << endl;
cout << "2 - Subtraction" << endl;
cout << "3 - Division" << endl;
cout << "4 - Multiplication" << endl;
cin >> choice;


switch (choice){

case 1:
cout << "enter a number" << endl;
cin >> a;

cout << "enter another enter" << endl;
cin >> b;

( answer = a + b );

cout << "the answer is " << answer << endl;
break;


case 2:
cout << "enter a number" << endl;
cin >> a;

cout << "enter another enter" << endl;
cin >> b;

( answer = a - b );

cout << "the answer is " << answer << endl;
break;


case 3:
cout << "enter a number" << endl;
cin >> a;

cout << "enter another enter" << endl;
cin >> b;

( answer = a / b );

cout << "the answer is " << answer << endl;
break;


case 4:
cout << "enter a number" << endl;
cin >> a;

cout << "enter another enter" << endl;
cin >> b;

( answer = a * b );

cout << "the answer is " << answer << endl;
break;


system("PAUSE");
}
return 0;

}

Look at the nesting level of your system( "PAUSE" ); line to see why it doesn't execute.
You need to #include <cstdlib> to use system pause... Im pretty sure of it anyway

Edit: also, the variable 'choice' should be an int, as it is an integer not a character...

Ive written another version of this but using the if statment as im not comfortable with switch and case stuff. Hope this helps (by the way, this compiled & ran fine for me on Visual Studio pro)

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <cstdlib>

using namespace std;

int main ()

{
	int a, b, answer, choice;

	cout << " Please enter the number that corresponds to the operation you would like to perform... \n\n";

	cout << " 1 - Addition\n";
	cout << " 2 - Subtraction\n";
	cout << " 3 - Division\n";
	cout << " 4 - Multiplication\n\n\n";

	cin >> choice;

	{

if (choice == 1) 

	{
		cout << "\nGreat, please enter a number ... \n";
			cin >> a;
		cout << "\nThanks, now enter a number to add to that...\n";
			cin >> b;
		
		(answer = a + b);

		cout << " \nAll done, the answer is " << answer <<"\n\n";
	}

				

{

if (choice == 2) 

	{
		cout << "Great, please enter a number ... \n";
			cin >> a;
		cout << "Thanks, now enter a number to subtract from that...\n";
			cin >> b;
		
		(answer = a - b);

		cout << " All done, the answer is " << answer <<"\n\n";

}

					
		
if (choice == 3) 

	{
		cout << "Great, please enter a number ... \n";
			cin >> a;
		cout << "Thanks, now enter a number to divide that by...\n";
			cin >> b;
		
		(answer = a / b);

		cout << " All done, the answer is " << answer <<"\n\n";

}

						

if (choice == 4) 

	{
		cout << "Great, please enter a number ... \n";
			cin >> a;
		cout << "Thanks, now enter a number to multiply that by...\n";
			cin >> b;
		
		(answer = a * b);

		cout << " All done, the answer is " << answer <<"\n\n";

}

						system ("PAUSE");




return 0;

				}

		}

}

Last edited on
i changed the varible type to int on choice and it works know.

i only use to system pause because i use the compiler dev c++ and if you dont use it the window will only stay up for a second.
Topic archived. No new replies allowed.