need some instruction!

My hw is: .....................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.
Operators List: + :add
- :subtract
* :multiply
/ :divide
c,C:clear data
x,X:exit program
This is what I got so far:
Main.cpp

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
#include <iostream>
#include "ReadNumbers.h"
;using namespace std;



void main ()
	{
	[output][/output]
	char Op;
	char OperatorList [] = "+-*/XxCc";

	cout<< "Enter a operator: ";


	if (GetValidOp (OperatorList, Op))
		switch (Op)
			{case '+':
				case '-':
				case '*':
				case '/':
					cout<< "Operator was"<<endl;
					break;
				case 'c':
				case 'C':
					system ("cls");
					break;
				case 'x':
				case 'X':
					exit(0);
					break;
			}
	
	
	}




Functions.cpp




1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool GetValidOp (char OperatorList[], char & Op)
{
	
	
	cin>>Op;
	for(int n = 0; n < 8;n++)
		if (Op == OperatorList[n])
		{	
			return true;
		}
		else
		{
			return false;
		}




Just dont know where to put Switch to make it right, in Main.cpp or Function.cpp?


1. There's a stray ; in front of using in line 3.
2. The return type of main is incorrect, it must be int.
3. You should pass the length of the array to GetValidOp - or better yet, the operator list should only exist inside that function.
4. You're exiting the GetValidOp early if the first operator is not a valid one. The loop will never go beyond n=0.
Topic archived. No new replies allowed.