boolian conditions and functions

Hey.
I am trying to get this simple math function to work. The function recieves an operator and two numbers and in theory, is supposed to change the value of 'res' and return a true or false value.
I can't really get it to work and I need some help with it.
Thanks in advance.
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
 #include <iostream>
#include <math.h>
using namespace std;

bool math(char op, float x, float y, float& res);

void main()
{
	char op;
	int val;
	float x, y, res=0;
	cout << " enter the desired operator: * + / s - , and two numbers: \n";
	cin >> op >> x >> y;
	bool math(char op, float x, float y, float &res);
	if (math == 0)
		cout << "unable to calculate result due to wrong parameters. please try again";
	else
		cout << "your result is " << res << endl;			
		
}

bool math(char op, float x, float y, float &res)
{
	if (op != '+', '*', '/', '-', 's')
		return false;
	else
		{
		switch (op)
			{
			case '+':
				res = x + y;
				return true;
				break;
			case '*':
				res = x * y;
				return true;
				break;
			case '/':
				if (y = 0)
				{
					return false;
					break;
				}
				else
				{
					res = x / y;
					return true;
					break;
				}
			case '-':
				res = x - y;
				return true;
				break;
			case 's':
				if (x < 0)
				{
					return false;
					break;
				}
				else
				{
					res = sqrt(x);
					return true;
					break;
				}
			}
		}

}
Last edited on
Line 7: main must always be type int

Line 14: This is a function declaration, not a function call.

 
  if (math(op, x, y, &res) == 0)


Line 24: C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y'))

('y') always evaluates to 1 (true), therefore the if statement is always true.

Topic archived. No new replies allowed.