Calculator loop question

Oct 26, 2009 at 8:53pm
Hello guys!

I wrote a calculator program a while ago - just curious as to how i could loop it with a while loop.
Ive tried a few ways, but i keep getting bare errors.

Would someone be able to show me where i could start / end the loop?

Thanks!

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;

				}

		}

}

Oct 26, 2009 at 8:59pm
Start the loop before line 11 and end it before line 96.
You should indent your code in a better way
Oct 26, 2009 at 10:27pm
Cheers Bazzy! I'll give it a go. Whats up with the way i indent? I think its quite snazzy!
Oct 26, 2009 at 10:42pm
The point of indentation is to make structure more clear. Yours is all over the place. Some statements have two levels of indentation, others have three levels, none of the matching braces are at the same level.
Oh, and normally you don't wrap in parentheses an expression that's the only expression in the statement. It's not wrong, but it's just... unnecessary. I think it could even confuse me if it caught me off-guard

http://en.wikipedia.org/wiki/Indent_style
Pick one of those (preferably K&R or Allman) and use it consistently.
Last edited on Oct 26, 2009 at 10:44pm
Topic archived. No new replies allowed.