Calculator program

So I have to write a program that serves as a simple 4 function calculator. Looking up an example gave me the code I have below, but it won't build successfully and I have no idea where to go from here. Sorry if it's messy.

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
 #include <iostream>


using namespace std;

#include <ctype.h>
#include <stdlib.h>


int main()
{

				//Declaring variables
s:
char OP;

char Answer;

int Num1;

int Num2;

				// Start loop2
cout << "Enter a number >   " << endl;
cin >> Num1;							// Read the first number


do	{					//		Start loop1
p:		
	    cout << "OP>  ";	
		cin >> OP;	     //	Read an operator
		
		cout << "Enter a second number >    " << endl;
		cin >> Num2;
		
		
		switch (OP)				//	Is operator valid
			{
			case 'X':
			case 'x':
						do	{
							cout << "Are you sure? [Y/N] ";
							cin >> Answer;
							if (toupper (Answer) == 'Y')	// toupper says to treat Answer as if it were upper case
									{
									cout << "Have a nice day" << endl;
									exit (0);	// terminate the program right now (turn off the calculator
									}
								else;
							} while (toupper (Answer) != 'N');
						break;
			case 'C':
            case 'c':
						do {
							cout << "Clearing Calculator." << endl;
							goto s;
						}while (OP = 'C','c');
							break;

			case '+':  Num1 + Num2;
				break;

			case '-':  Num1 - Num2;
				      
				break;

			case '*':  Num1 * Num2;
				break;

			case '/':  Num1 / Num2;   
						if (Num2 == 0)
							cout << "Please enter another number. Cannot divide by 0." << endl;
						goto p;

				break;

			default: cout << "You have entered an invalid operator." << endl;    
			         cout << "Please enter + - * / C or X" << endl;
					 goto p;
					 break;
						
              cout << "The result is  " << Answer << endl;
						
		
             }while (true);

}
Last edited on
Please copy and paste the exact error message(s) you are getting and indicate which line(s) they occur on.

Use of goto is considered bad practice, you should try to rework your code flow without it.
I reworked a bit while waiting and I got it to compile successfully, but now it won't give me the answer. I imagine there's something I screwed up towards the end.



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
int main()
{

				//Declaring variables
s:
char OP;

char Answer;

int Num1;

int Num2;

				// Start loop2
cout << "Enter a number >   " << endl;
cin >> Num1;							// Read the first number


do	{					//		Start loop1
p:		
	    cout << "OP>  ";	
		cin >> OP;	     //	Read an operator
		
		cout << "Enter a second number >    " << endl;
		cin >> Num2;
		
		
		switch (OP)				//	Is operator valid
			{
			case 'X':
			case 'x':
						do	{
							cout << "Are you sure? [Y/N] ";
							cin >> Answer;
							if (toupper (Answer) == 'Y')	// toupper says to treat Answer as if it were upper case
									{
									cout << "Have a nice day" << endl;
									exit (0);	// terminate the program right now (turn off the calculator
									}
								else;
							} while (toupper (Answer) != 'N');
						break;
			case 'C':
            case 'c':
						do {
							cout << "Clearing Calculator." << endl;
							goto s;
						}while (OP = 'C','c');
							break;

			case '+':  Answer=Num1+Num2;
				break;

			case '-':  Answer=Num1-Num2;
				      
				break;

			case '*':  Answer=Num1*Num2;
				break;

			case '/':  Answer=Num1/Num2;   
						if (Num2 == 0)
							cout << "Please enter another number. Cannot divide by 0." << endl;
						goto p;

				break;

			default: cout << "You have entered an invalid operator." << endl;    
			         cout << "Please enter + - * / C or X" << endl;
					 goto p;
					 break;
						
             
						
		
             }while (true);


}while (OP = 'X', 'x', 'C', 'c', '+', '-', '*', '/');


 cout << "The result is  " << Answer << endl;
}
Line 76 of your most recent post - that is an infinite loop (it is not related to a do statement).

Line 79 of your most recent post - that does not do what you expect. Instead of comparing for equality between OP and all those character literals, it simply assigns '/' to OP and results in an infinite do-while loop. Consider instead:
79
80
81
82
83
84
85
86
    } while(OP == 'X'
      ||    OP == 'x'
      ||    OP == 'C'
      ||    OP == 'c'
      ||    OP == '+'
      ||    OP == '-'
      ||    OP == '*'
      ||    OP == '/');
Last edited on
I deleted line 76 and I now have no clue why it keeps expecting a while at the end. It's back to not compiling.

error C2059: syntax error : '}'
Last edited on
If you fix your indentation, the issue will become clear. This is your most recent code, with proper indentation:
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
int main()
{
	// Declaring variables
s: // (LB) you could start a while loop here instead
	char OP;
	char Answer;
	int Num1;
	int Num2;

	// Start loop2
	cout << "Enter a number >   " << endl;
	cin >> Num1; // Read the first number

	do
	{ // Start loop1
	p: // (LB) this is useless because of the 'continue' statement
		cout << "OP>  ";	
		cin >> OP; // Read an operator

		cout << "Enter a second number >    " << endl;
		cin >> Num2;

		switch (OP) //Is operator valid
		{
		case 'X':
		case 'x':
			do
			{
				cout << "Are you sure? [Y/N] ";
				cin >> Answer;
				if(toupper (Answer) == 'Y') // toupper says to treat Answer as if it were upper case
				{
					cout << "Have a nice day" << endl;
					exit (0); // terminate the program right now (turn off the calculator
				}
				else; // (LB) this line doesn't do anything
			} while (toupper(Answer) != 'N');
			break;
		case 'C':
		case 'c':
			do
			{
				cout << "Clearing Calculator." << endl;
				goto s; // (LB) why is this in a loop? The loop is pointless with this line
			} while (OP = 'C','c'); // (LB) this is an infinite loop (but the goto prevents it)
			break;
		case '+':
			Answer=Num1+Num2;
			break;
		case '-':
			Answer=Num1-Num2;
			break;
		case '*':
			Answer=Num1*Num2;
			break;
		case '/':
			Answer=Num1/Num2;
			if (Num2 == 0) // (LB) this condition is too late, the program will have already crashed
				cout << "Please enter another number. Cannot divide by 0." << endl;
			goto p; // (LB) this goto is unconditional
			break;
		default:
			cout << "You have entered an invalid operator." << endl; // (LB) you tell them this AFTER they enter the second number?
			cout << "Please enter + - * / C or X" << endl;
			goto p; // (LB) this could be replaced with a 'continue'
			break;
		} while (true); // (LB) this is an infinite loop no attached to a 'do'
	} while (OP = 'X', 'x', 'C', 'c', '+', '-', '*', '/'); // (LB) this is an infinite loop
	cout << "The result is  " << Answer << endl;
}
Last edited on
Thanks for the corrections, but it seems like I'll have to start the whole thing from scratch. I used a sort of template that my professor did in class and I seem to be misunderstanding what he did in it. I applied your corrections and am still getting the same error as above.

I removed the infinite while towards the end, applied your correction of the last while statement, removed the goto's, moved the second number input prompt below the switch, etc. I don't think do while loops are working out for me very well.


I wouldn't want you to write it for me, so I appreciate the help up to this point.



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
int main()
{
	// Declaring variables
    // (LB) you could start a while loop here instead
	char OP;
	char Answer;
	int Num1;
	int Num2;

	// Start loop2
	cout << "Enter a number >   " << endl;
	cin >> Num1; // Read the first number

	do
	{ // Start loop1
	 
		cout << "OP>  ";	
		cin >> OP; // Read an operator

		

		switch (OP) //Is operator valid
		{
		case 'X':
		case 'x':
			do
			{
				cout << "Are you sure? [Y/N] ";
				cin >> Answer;
				if(toupper (Answer) == 'Y') // toupper says to treat Answer as if it were upper case
				{
					cout << "Have a nice day" << endl;
					exit (0); // terminate the program right now (turn off the calculator
				}
				 
			} while (toupper(Answer) != 'N');
			break;
		case 'C':
		case 'c':
			do
			{
				cout << "Clearing Calculator." << endl;
				 
			} while (OP == 'C'
				   ||OP == 'c'); 
			break;
		case '+':
			Answer=Num1+Num2;
			break;
		case '-':
			Answer=Num1-Num2;
			break;
		case '*':
			Answer=Num1*Num2;
			break;
		case '/':
			Answer=Num1/Num2;
			if (Num2 == 0) // (LB) this condition is too late, the program will have already crashed
				cout << "Please enter another number. Cannot divide by 0." << endl;
			 
			break;
		default:
			cout << "You have entered an invalid operator." << endl; // (LB) you tell them this AFTER they enter the second number?
			cout << "Please enter + - * / C or X" << endl;
			continue; // (LB) this could be replaced with a 'continue'
			break;
		 
	} while(OP == 'X'
      ||    OP == 'x'
      ||    OP == 'C'
      ||    OP == 'c'
      ||    OP == '+'
      ||    OP == '-'
      ||    OP == '*'
      ||    OP == '/');

	cout << "Enter a second number >    " << endl;
		cin >> Num2;

	cout << "The result is  " << Answer << endl;
}

}

Could you copy and paste the exact error message and indicate which line it is talking about?
lab5.cpp(94): error C2059: syntax error : '}'

line 94 it seems. The very last one. That would be line 83 in the code above.
Have you tried deleting that line? That looks like an extra closing brace.
Topic archived. No new replies allowed.