Need Help Correcting the Function!!!!!!!

I'm suppose to write a calculator program with functions. 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;
				}
		}
}




Last edited on
Hi, for your function, getNum1(double One), getNum2(double Num2)..., you have to add '&' before the One and Num2...
Duplicate thread.

http://www.cplusplus.com/forum/beginner/96539/


@clarkd

I gave you a whole lot advice in the other thread, - if you have more questions, continue the original thread don't start a new one. Especially when you haven't taken on board all the advice given.

It's annoying from osgwsy's point of view because he spent some time to give some advice, only to find lots of the same advice has been given elsewhere on the same topic.

Edit: changed some wording

Last edited on
To add on, I go through your code, I find some of the lines are a bit weird.

For example, Line 42 and Line 58, if((Num1 >= 0) || (Num1 <= 0)) . I find this condition a bit redundant? What do you mean by the number is either less or equal zero or more than or equal zero?

In addition, Line 74, you added a if else condition before the switch. For the if else condition, you state no 'c'/'C' or 'x'/'X', however, you add two cases in the switch for 'c'/'C' and 'x'/'X', which does not make sense to me. You might want to check this part too.

About asking the second number twice, the reason is due to Line 111. In Line 111, it ask for getNum2(), then Line 30 ask for the second number again.

I think you mean want to draft what are the functions you might need, and organize your codes?
Topic archived. No new replies allowed.