Calculator Problems

Hey I just started learning C++ and I can't get this to work.

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
//Keeley's Project 3 - Ocotober 11th 2009
//Calculator Revision - 1

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{ 
	int Operator;
	
	double IntegerA1;
	double IntegerA2;

	double IntegerS1;
	double IntegerS2;

	double IntegerM1;
	double IntegerM2;

	double IntegerD1;
	double IntegerD2;



	cout << "Keeley's Calculator\n";
	cout << "Please choose what operation you want to do and press enter\n\n";
	cout << "1 = Addition   2 = Subtraction   3 = Multiplication   4 = Division\n\n";
	cin  >> Operator;
		
	
		if(Operator = 1) cout << "\nYou choose Addition\n";
						 cout << "Please type the number you want to add\n\n";
						 cin  >> IntegerA1;
						 cout << "\nYou typed in " << IntegerA1 << ", please type in the number you want to add to " << IntegerA1 <<"\n\n";
						 cin  >> IntegerA2;
						 cout << "\n\nYou typed in " << IntegerA2 << " and the answer is " << IntegerA1 + IntegerA2; 
						 cout <<_getch();

	    if(Operator = 2) cout << "\n\nYou choose Subtraction\n";
						 cout << "Please type the number you want to subtract\n\n";
						 cin  >> IntegerS1;
						 cout << "You typed in " << IntegerS1 << ", please type in the number you want to subtract from " << IntegerS1 <<"\n\n";
						 cin  >> IntegerS2;
						 cout << "\n\nYou typed in " << IntegerS2 << " and the answer is " << IntegerS1 - IntegerS2;
						 cout <<_getch();

	    if(Operator = 3) cout << "\n\nYou choose Multiplication\n";
						 cout << "Please type the number you want to multiply\n\n";
						 cin  >> IntegerM1;
						 cout << "You typed in " << IntegerM1 << ", please type in the number you want to multiply to " << IntegerM1 <<"\n\n";
						 cin  >> IntegerM2;
						 cout << "\n\nYou typed in " << IntegerM2 << " and the answer is " << IntegerM1 * IntegerM2;
						 cout <<_getch();

	    if(Operator = 4) cout << "\n\nYou choose Division\n";
						 cout << "Please type the number you want to divide\n\n";
						 cin  >> IntegerD1;
						 cout << "You typed in " << IntegerD1 << ", please type in the number you want to divide to " << IntegerD1 <<"\n\n";
						 cin  >> IntegerD2;
						 cout << "\n\nYou typed in " << IntegerD2 << " and the answer is " << IntegerD1 / IntegerD2;
						 cout <<_getch();

	     

	    return 0;

}


When I press 1,2,3,4, it will always go to Addition why?
Your if blocks have no braces. Your if conditions are using the assignment operator (=) instead of the equality operator (==).
would it not be easier using a switch with break; at the end of each case:
@helios Oh wow, I can't believe I used = and forgot the braces, thanks!

@thatGuy I never learnt how to do that yet, I'm following some tutorial and it never told me how to pause a program so I can't learn from the examples and a quick google gave me _getch(); :p
may be you can remove all your "_getch()" and add a new one before "return 0" after you have add braces, just a little suggestion ...i am a newbie too
Last edited on
Oh yeah I could do that thanx!

Though I'll build upon this Calculator and will probably need "_getch()"
Question, I'm not trying to correct Keeley, but I was wondering, wouldn't you use else if's for all the "if's" after the first one? Such as,

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
if(Operator == 1){ cout << "\nYou choose Addition\n";
						 cout << "Please type the number you want to add\n\n";
						 cin  >> IntegerA1;
						 cout << "\nYou typed in " << IntegerA1 << ", please type in the number you want to add to " << IntegerA1 <<"\n\n";
						 cin  >> IntegerA2;
						 cout << "\n\nYou typed in " << IntegerA2 << " and the answer is " << IntegerA1 + IntegerA2; 
						 cout <<_getch();}

	   else if(Operator == 2) {cout << "\n\nYou choose Subtraction\n";
						 cout << "Please type the number you want to subtract\n\n";
						 cin  >> IntegerS1;
						 cout << "You typed in " << IntegerS1 << ", please type in the number you want to subtract from " << IntegerS1 <<"\n\n";
						 cin  >> IntegerS2;
						 cout << "\n\nYou typed in " << IntegerS2 << " and the answer is " << IntegerS1 - IntegerS2;
						 cout <<_getch();}

	   else if(Operator == 3){ cout << "\n\nYou choose Multiplication\n";
						 cout << "Please type the number you want to multiply\n\n";
						 cin  >> IntegerM1;
						 cout << "You typed in " << IntegerM1 << ", please type in the number you want to multiply to " << IntegerM1 <<"\n\n";
						 cin  >> IntegerM2;
						 cout << "\n\nYou typed in " << IntegerM2 << " and the answer is " << IntegerM1 * IntegerM2;
						 cout <<_getch();}

	   else if(Operator == 4) {cout << "\n\nYou choose Division\n";
						 cout << "Please type the number you want to divide\n\n";
						 cin  >> IntegerD1;
						 cout << "You typed in " << IntegerD1 << ", please type in the number you want to divide to " << IntegerD1 <<"\n\n";
						 cin  >> IntegerD2;
						 cout << "\n\nYou typed in " << IntegerD2 << " and the answer is " << IntegerD1 / IntegerD2;
						 cout <<_getch();}
           
                   else {cout <<"\n\nBad Input. Quitting...";}
return 0;
}

The switch/case/break method would be best here
Topic archived. No new replies allowed.