Problem with Calculator Program

Write a program, which will act as a simple four-function calculator. That is it will read a number, read an operator, read another number, then do the operation. The calculator works with integers and uses four functions: +, -, *, and /. After the first operation is completed, the program will read another operator and uses the result of the previous operation as the first value for the next operation. If the user enters a C the result is cleared and then the user starts entering a new number. If the user enters an X, the calculator is turned off. The various input values (i.e. numbers, operators, commands) will be followed by the ENTER key. Your program should prompt the user on what the user is to do. The commands C and X may be entered in place of an operator.

All numbers entered and displayed will be integer values.

NOTES:

The person using the calculator has a terrible time using a keyboard and may not always enter a valid operator. If this happens the program should ask for another operator.

Regardless of math operation attempted, the program must not die. If the math operation would cause a fatal error in your program you should check for it and issue an error message rather than attempting the operation.


Here is my code:

#include <iostream>

using namespace std;

#include <stdlib.h>

void main ()
{
bool bValidOP;
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;
bValidOP = 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':
bValidOP = false;
cout << "Clearing Calculator." << endl;
goto s;
break;
case 'x':
case 'X':
bValidOP = false;
cout << "Turning Off Calculator." << endl;
exit (0);
break;
default:
bValidOP = false;
cout << "You have entered an invalid operator." << endl;
cout << "Please enter +, -, *, /, 'c' or 'x' as operator. " << endl;
goto p;
break;
}

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

} while (!bValidOP);
}


The build works with no errors.

However, "After the first operation is completed, the program will read another operator and uses the result of the previous operation as the first value for the next operation" is not what is happening with mine. It keeps using the very first number put in as the first number over and over. Also, after I enter an invalid OP, c, or x, it asks me to "Please enter another number and press ENTER:", so I do, then it will complete the case.

Besides these problems, I am pretty sure I have done what the professor asked for this assignment. Thanks for any help.






Hello,

It keeps using the same number that is entered by the user in variable number1 because it doesn't know to use the answer in the next problem. You need to assign the answer into number1 . I would put it after you print the answer. Also the reason why it askes you to enter the second number is because the way you have everything set up. Once you enter 'c' or 'x' it still has to execute the next lines of code before it gets to the switch statement to decide what to do.

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

do 
{
p:
cout << "Please enter an operator, 'c' to clear, or 'x' to turn off and press ENTER: ";
cin >> Op;
bValidOP = 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':
bValidOP = false;
cout << "Clearing Calculator." << endl;
goto s;
break;
case 'x':
case 'X':
bValidOP = false;
cout << "Turning Off Calculator." << endl;
exit (0);
break;
default:
bValidOP = false;
cout << "You have entered an invalid operator." << endl;
cout << "Please enter +, -, *, /, 'c' or 'x' as operator. " << endl;
goto p;
break;
}

cout << "The result is: " << answer << endl;
number1 = answer; //I would put it here

} while (!bValidOP);
}


Hope I helped
Good Luck
D Bialecki
UAT Programming Student
Hey...sorry so long to get back. When I first tried a reply, system was down. I did try what you said, but it did not work there (fyi). I got help from the tutor and we put it after the p:.
It worked there! You had it right, just not in the right place. He also helped me on my other
problem mentioned.

Thanks though. It got me on the right track.

ps I have another assignment coming up that I have to modify this one and use functions.
I believe I can handle this one (will use a menu). If not, will post a new question. So, will just say this one is solved.

Thanks again.
Topic archived. No new replies allowed.