Can someone help me with my calculator?

1. Description
1.1. Goal - The purpose of this program is to use the control structure concepts learned in chapters 4 and 5.
1.2. Requirements – Design and implement a simple calculator.
 Prompts the user to Enter Expression:
 The user enters an expression of the following format;
o operand1 operator operand2
o examples;
 12 * 3 (where12 is operand1, * is the operator, and 3 is operand2)
 45.0 / 15.5 (where 45 is operand1, / is the operator, and 15.5 is operand2)
 -12.5 + 7 (where -12.5 is operand1, + is operator, and 7 is operand2)
 35.5 – 20.2 (where 35.5 is operand1, - is operator, and 20.2 is operand2)
 The program displays the results as illustrated below;
o Result: operand1 operator operand2 = result
o examples;
 Result: 12 * 3 = 36
 Result: 45.0 / 15.5 = 2.90323
 Result: -12.5 + 7 = -5.5
 Result: 35.5 – 20.2 = 15.3
 After the expression is evaluated, results computed and displayed, the user is prompted to Enter another expression <Y/N> ?
o If the user enters Y, then they are prompted to “Enter Expression”
o If the user enters N, then the program displays “Goodbye” and terminates.
o The user is prompted to “Enter another expression <Y/N> ?” until the user enters either a Y or N character.

my calculator is supposed to meet these requirements

Heres my program but I keep getting errors

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
// Simple Calculator.cpp : main project file.
// Text-printing program
#include <iostream>
#include <StdAfx.h>
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;
number1 = answer; 

} while (!bValidOP);
}


These are the errors I get
1>------ Build started: Project: Simple Calculator, Configuration: Debug Win32 ------
1> Simple Calculator.cpp
1>Simple Calculator.cpp(3): warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>Simple Calculator.cpp(22): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(23): error C2065: 'cin' : undeclared identifier
1>Simple Calculator.cpp(27): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(28): error C2065: 'cin' : undeclared identifier
1>Simple Calculator.cpp(31): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(32): error C2065: 'cin' : undeclared identifier
1>Simple Calculator.cpp(52): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(52): error C2065: 'endl' : undeclared identifier
1>Simple Calculator.cpp(58): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(58): error C2065: 'endl' : undeclared identifier
1>Simple Calculator.cpp(63): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(63): error C2065: 'endl' : undeclared identifier
1>Simple Calculator.cpp(64): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(64): error C2065: 'endl' : undeclared identifier
1>Simple Calculator.cpp(69): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(69): error C2065: 'endl' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I dont know what I did wrong...
How about change <iostream> to <iostream.h>?
I still get these errors

1>------ Build started: Project: Simple Calculator, Configuration: Debug Win32 ------
1> Simple Calculator.cpp
1>Simple Calculator.cpp(3): warning C4627: '#include <iostream.h>': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>Simple Calculator.cpp(22): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(23): error C2065: 'cin' : undeclared identifier
1>Simple Calculator.cpp(27): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(28): error C2065: 'cin' : undeclared identifier
1>Simple Calculator.cpp(31): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(32): error C2065: 'cin' : undeclared identifier
1>Simple Calculator.cpp(52): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(52): error C2065: 'endl' : undeclared identifier
1>Simple Calculator.cpp(58): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(58): error C2065: 'endl' : undeclared identifier
1>Simple Calculator.cpp(63): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(63): error C2065: 'endl' : undeclared identifier
1>Simple Calculator.cpp(64): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(64): error C2065: 'endl' : undeclared identifier
1>Simple Calculator.cpp(69): error C2065: 'cout' : undeclared identifier
1>Simple Calculator.cpp(69): error C2065: 'endl' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
the top of your code should be
1
2
3
4
5
6
#include <iostream>
#include <cstdlib>

using namespace std; 
int main ()
......


you shouldnt have your include after your using statement.
Last edited on
use iostream. don't allow space between include and <iostream>
Im still getting running errors.... and also does anyone know how to make it loop and add the y/n option from my requirements?
@mzadriann

This works, but you would still need to add better checking.
1 - If user enters a 'C', don't ask for second number. Start at the beginning again.
2 - Same with 'x'. Quit program before asking for a second number.

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
// Simple Calculator.cpp : main project file.
// Text-printing program

#include "stdafx.h" // This MUST be first include, and in quotes
#include <iostream>
#include <stdlib.h>

using namespace std;

int main ()
{
	float number1; // Don't use int for variables. User may not enter whole numbers 
	float number2;
	float answer; // answer may not come out as a whole number, so use float
	char Op, YN = 'Y';

	do 
	{
		number1 = 0.0;
		number2 = 0.0;
		answer = 0.0;
		Op = ' ';

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

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

		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':
			cout << "Clearing Calculator." << endl;
			number1 = 0;
			number2 = 0;
			break;
		case 'x':
		case 'X':
			cout << "Turning Off Calculator." << endl;
			exit (0);
			break;
		default:
			cout << "You have entered an invalid operator." << endl;
			cout << "Please enter +, -, *, /, 'c' or 'x' as operator. " << endl;
			break;
		}
		if (Op != 'c')
			cout << "The result is : " << number1 << " " << Op << " " << number2 << " = " << answer << endl;
		cout << "Do you wish to enter another calculation? (Y or N )" << endl;
		cin >> YN;
	} while ((YN != 'n' && YN != 'N') || Op !='c' );

	cout << "Program has ended." << endl << endl;
}
Topic archived. No new replies allowed.