Turning calculator into functions

This is what I have so far.

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
#include <iostream>

using namespace std;

double val1;
double val2;
double r;
char ValidOperators [] = "+-*/cCxX";


double readnum1 (double val1)
	{

	cout << "Please enter your first number: ";
		cin >> val1;
	return val1;

	}

double readnum2 (double val2)
	{

	cout << "Please enter your second number: ";
		cin >> val2;
	return val2;
	
	}



double addnum (double val1, double val2)
	{

	return r = val1 + val2;

	}

double subtnum (double val1, double val2)
	{
	
	return r = val1 - val2;
	
	}

double multiply (double val1, double val2)
	{
	
	return r = val1 * val2;
	
	}

double divide (double val1, double val2)
	{

	return r = val1 / val2;

	} 

bool GetOperator (const char ValidOps [], char & Op);

void main ()

	{
	char	C;
	char	ValidOperators [] = "+-*/cCxX";

	GetOperator (ValidOperators, C);

	}

bool GetOperator (const char ValidOps [], char & Op)
	{
	return true;
	}
	



I have setup the basic functions for all the add/sub/mult/divide.


But now my assignment is telling me I need to create a bool handling whether whatever is entered is valid or not, which I'm having trouble with.


"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."

The teacher also gave us this to get started.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

void main ()
	{
	char	C;
	char	ValidOperators [] = "+-*/cCxX";

	GetOperator (ValidOperators, C);
	}

bool GetOperator (const char ValidOps [], char & Op)
	{
	return true;
	}


Any help on how to get started would be useful.
Last edited on
You mean you want to check if Op is in ValidOperators?
Why not just loop?
1
2
3
4
5
for(int i = 0; i < number_of_valid_operators; i++){//sizeof(ValidOperators) won't do here
//arrays are passed as pointers. you will have to use a constant
    if(ValidOperators[i] == Op) return true;
}
return false;
Topic archived. No new replies allowed.