A Simple Function Help, Please!

This is the Question and following is my program. Could someone please help with it. I'm not getting anywhere. It keep giving the 2065 error code -- "cout is undeclared Identifier at the functions".

Modify Lab Five such that you use functions for each math calculation (Add, Subtract, Multiply, and Divide). Pass in parameters to each function for the values to use and the functions will return the result. Use a function to read in the numbers involved. These numbers will be doubles. Also write a function that reads in the operator and returns a boolean – true if the operator is valid, false if not valid. This function will have two parameters. First is a string of characters containing the valid operators. The second is a reference parameter where the operator will be placed if the operator entered is valid.

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <stdlib.h>
#include <conio.h>

void getNum1(double);
void getNum2(double);
void getOperator(char &);
void Add(char &);
void Subtract(char &);
void Result(char &);



double Num1;
double Num2;
char OPR;
bool test1, test2;
bool test3;

int main()
{
	double Num1;
	double Num2;
	char OPR;

	getNum1(Num1);
	getOperator(OPR);
	getNum2(Num2);
	Result(OPR);

}

void getNum1(double Num1)
{
	cout << "Enter the First Number: " << endl;
	cin >> Num1;

	if((Num1 >= 0) || (Num1 <= 0))
		{
			test1 = true;
		}
	else
		{
			cout << "Not a Valid Number" << endl;
			goto: getNum1(Num1);
		}
}

void getNum2(double Num2)
{
	cout << "Enter the Second/Next number: ";
	cin >> Num2;

	if ((Num2 >= 0) || (Num2 <= 0))
		{
			test2 = true;
		}
	else
		{
			cout << "Not a Valid Number" << endl;
			goto: getNum2(Num2);
		}
}

void getOperator(char &OPR)
{
	cout << "Enter the Operator: ";
	cin >> OPR;

	if((OPR != 'C' && OPR != 'c') || (OPR != 'X' && OPR != 'x'))
		{
			switch(OPR)
				{
			case '+':
			case '-':
			case '-':
			case '/':
				getNum2(Num2);
				test3 = true;
				break;
			case 'C':
			case 'c':
				cout << "The Program is Cleared " << endl;
				test3 = true;
				break;
			case 'X':
			case 'x':
				cout << "\t--------TERMINATED---------\t" << endl;
				exit(0);

			default: 
				if((OPR != '+') && (OPR != '-') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
					{
						cout << "Must Be An Operator(+-*/)" << endl;
						cout << "Enter an Operator: ";
						cin >> OPR;
					}
				}
		}
	
	if ((OPR != '+') && (OPR != '-') && (OPR != '*') && (OPR != '/'))
		{
			goto getNum1(Num1);
		}
	else
		{
			goto getNum2(Num2);
		}
}

void Add(char &OPR)
{
	double Result;
	if((OPR != '-') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
		{
			Result = Num1 + Num2;
		}
}

void Subtract(char &OPR)
{
	double Result;
	if((OPR != '+') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
		{
			Result = Num1 + Num2;
		}
}

void Result(char &OPR)
{
	double Result;
	if ((OPR != '+') && (OPR != '-') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
		{
			switch (Opp)
				{
					case '+':
						Result = Add(OPR);
						cout << "The Result is " << Add(OPR) << "\n" << endl;
						break;
					case '-':
						Result = Subtract(OPR);
						cout << "The Result is " << Subtract(OPR) << "\n" << endl;
						break;
					//case '*':
					//	Result= Num1 * Num2;
					//	cout << "The Result is " << Result << "\n"   << endl;
					//	break;
					//case '/':
					//	Result = Num1 / Num2;
					//	if (Num2 <= 0 && Num2 >= 0)
					//		{
					//			cout << "Error, Cannot Divide By Zero, The Result will always be Nothing == " << Result << endl;
					//			exit(0);
					//		}	
					//	else 
					//		cout << "The Result is " << Result << "\n" << endl;
					//		break;

					case 'C':
					case 'c':
						cout << "The Program Cleared " << endl;
						test3 = true;
						break;			

					case 'X':
					case 'x':
						cout << "You're Terminated :P/n" << endl;
						exit (0);
						

			
					default: 
						cout << "**Must Be A Number**" << endl;
				}		
			Num1 = Result;
		}
}


It keeps giving the error code 2065. Also I haven't plugged in the Multiplication function ad Divide Function. I just want to make sure first i get add and subtract, right. Please Help.
Last edited on
You need to specify
using namespace std;
The Program runs ask for the Operator and the second number but for some minor mistake it doesn't add or subtract. and ask for second number twice before giving the result. And when Press 'C' it clears and doesn't ask for operator.
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;

void getNum1(double);
void getNum2(double);
void getOperator(char &);
void Add(char &);
void Subtract(char &);
void Result(char);



double Num1, Num2;
char OPR;
bool test1, test2;
bool test3;
double Result1;
double Result2;

int main()
{
	getNum1(Num1);
	
	do
		{	
			
			getOperator(OPR);
			getNum2(Num2);
			Result(OPR);
		}
		while(OPR);
}

void getNum1(double One)
{
	One = Num1;
	cout << "Enter the First Number: ";
	cin >> Num1;

	if((Num1 >= 0) || (Num1 <= 0))
		{
			test1 = true;
		}
	else
		{
			cout << "Not a Valid Number" << endl;
			getNum1(Num1);
		}
}

void getNum2(double Num2)
{
	cout << "Enter the Second/Next number: ";
	cin >> Num2;

	if ((Num2 >= 0) || (Num2 <= 0))
		{
			test2 = true;
		}
	else
		{
			cout << "Not a Valid Number" << endl;
			getNum2(Num2);
		}
}

void getOperator(char &OPR)
{
	cout << "Enter the Operator: ";
	cin >> OPR;

	if((OPR != 'C' && OPR != 'c') || (OPR != 'X' && OPR != 'x'))
		{
			switch(OPR)
				{
			case '+':
			case '-':
			case '*':
			case '/':
				test3 = true;
				break;
			case 'C':
			case 'c':
				cout << "The Program is Cleared " << endl;
				test3 = true;
				break;
			case 'X':
			case 'x':
				cout << "\t--------TERMINATED---------\t" << endl;
				exit(0);

			default: 
				if((OPR != '+') && (OPR != '-') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
					{
						cout << "Must Be An Operator(+-*/)" << endl;
						cout << "Enter an Operator: ";
						cin >> OPR;
					}
				}
		}
	
	if ((OPR != '+') && (OPR != '-') && (OPR != '*') && (OPR != '/'))
		{
			getNum1(Num1);
		}
	else
		if((OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
		{
			getNum2(Num2);
		}
}

void Add(char &OPR)
{
	if((OPR != '-') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
		{
			Result1 = Num1 + Num2;
		}
}

void Subtract(char &OPR)
{
	if((OPR != '+') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
		{
			Result2 = Num1 + Num2;
		}
}

void Result(char OPR)
{
	if ((OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
		{
			switch (OPR)
				{
					case '+':
						Add(OPR);
						cout << "The Result is " << Result1 << endl;
						break;
					case '-':
						Subtract(OPR);
						cout << "The Result is " << Result2 << endl;
						break;
					//case '*':
					//	Result= Num1 * Num2;
					//	cout << "The Result is " << Result << "\n"   << endl;
					//	break;
					//case '/':
					//	Result = Num1 / Num2;
					//	if (Num2 <= 0 && Num2 >= 0)
					//		{
					//			cout << "Error, Cannot Divide By Zero, The Result will always be Nothing == " << Result << endl;
					//			exit(0);
					//		}	
					//	else 
					//		cout << "The Result is " << Result << "\n" << endl;
					//		break;

					case 'C':
					case 'c':
						cout << "The Program Cleared " << endl;
						test3 = true;
						break;			

					case 'X':
					case 'x':
						cout << "You're Terminated :P/n" << endl;
						exit (0);
						

			
					default: 
						cout << "**Must Be A Number**" << endl;
				}		
			if(OPR = '+')
				{
					Num1 = Result1;
				}
			else
				{
					Num1 = Result2;
				}
		}
}



@clarkd

I personally hate dislike constructs like line 125. They are ugly & non-scalable, and best handled with a switch. They are especially not needed just prior a switch which does the same thing.

The code would benefit from a IsOperator function that returns a bool, rather repeating these tests throughout the code.

This is the psuedocode for a basic calculator:

1
2
3
4
//get number
//get operator
//get number
//calc & print answer 


Looking at this, 185 LOC is too much, and it only does + and - so far. Remember to check for division by zero when you do that function. Be careful doing that with doubles - you really want to check whether the number is less than some arbitrary precision like 0.001 say.

You also have global variables, put them all in main() and send them to functions that need them as references.

You can also make use of the toupper function (which transforms a char to upper case) so you don't have to test variables twice.

Hope all goes well.
Topic archived. No new replies allowed.