How to clear?

closed account (2A7X92yv)
solved .. thanks guys
Last edited on
The break on line 49 should be at line 51.
closed account (2A7X92yv)
solved thanks guys
Last edited on
closed account (2A7X92yv)
please help
closed account (2A7X92yv)
anyone?
Line 50: Are you sure that break is supposed to be there?

Also, it's probably a good idea to check (with an if statement) if the operator entered is a 'c' before asking for the different number, and then using continue to jump back to the top of your loop.

Similarly, if your operator is an 'x', then it'd probably be a better idea to check it before asking for a number and then breaking out of the loop rather than using exit(). Exit(), being a C function, isn't safe to use with C++ objects that have something called a destructor (which many if not most do).

Finally, void main() is evil. Please change it to int main(). :(

-Albatross
Last edited on
OP wrote:
I have a simple calculator. for c++
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
#include <iostream>

using namespace std;

#include <stdlib.h>

void main ()
{
bool	IsOpValid;
int	number1;
int	number2;
int	answer;
char	Op;
	
s:
number1 = 0;
number2 = 0;
answer = 0;

cout << "Please enter a number and press ENTER: ";
cin >> number1;

do	{
p:
cout << "Please enter an operator, 'c' to clear, or 'x' to turn off and press ENTER: ";
cin >> Op;
IsOpValid = false;

cout << "Please enter another number and press ENTER: ";
cin >> number2;

switch (Op)
{
case '+':
answer = number1 + number2;
break;
case '-': 
answer = number1 - number2;
break;
case '*': 
answer = number1 * number2;
break;
case '/': 
answer = number1 / number2;
break;
case 'c':
case 'C':
break;
IsOpValid = true;
cout << "Clearing Calculator." << endl;

case 'x':
case 'X':
IsOpValid = true;
cout << "Turning Off Calculator." << endl;
exit (0);
break;
default:
IsOpValid = true;
cout << "You have entered an invalid operator." << endl;
cout << "Please enter +, -, *, /, 'c' or 'x' as operator. " << endl;
break;
}

cout << "The result is: " << answer << endl;

} while (!IsOpValid);
	}
	{
return 0;

}


the only problem it has is when i do C or c which is to clear.. it doesnt clear.. it keeps on saying some messages

and one other problem it has is .. never mind i do a c or a x (exit) . it asks me once again then only i can exit out.. or clear (but clear goes on for ever)
Topic archived. No new replies allowed.