Array Loop Question

Here is the summary of what I am trying to do. I am building a calculator using functions. I am having trouble getting the array to be evaluated for a received input and return a value. I can not figure out why it will not return any values in the array after the first one. So '+' works but none of the others. Any assistance is greatly appreciated.

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
(definition of const char [] from main.cpp file)  
char Valid_Ops[] = "+-/*CcXx";

(code from functions.cpp file)
bool Read_Operator(const char Valid_Ops[], char & Op)
	{
		char a;
	
		cin >> a;
		for (int i = 0; i < Valid_Ops[i+1]; i++)
			if (a == Valid_Ops [i])
			{
					Op = a;
					return (true);
			}else
				return (false);
			
		if (Op == 'X' || Op == 'x')
				exit(0);
			else;
				if (Op == 'C' || Op == 'c')
					system("cls");
				else;
				return (Op);
	}
That looks rather inconsistent.


How about: http://www.cplusplus.com/reference/string/string/find/

1
2
3
4
5
6
7
8
9
10
11
12
13
const std::string Valid_Ops = "+-/*CcXx";

bool Read_Operator( const std::string & Ops, char & op )
{
  if ( std::cin >> op ) {
    auto found = Ops.find( op );
    return ( found != std::string::npos );
  }
  else {
    op = '\0';
    return false;
  }
}

Now the Read_Operator returns true, if op is a valid operator.

IMO the other two actions should be performed by the caller:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool valid = Read_Operator( Valid_Ops, operator );
if ( valid ) {
  // use operator
}
else {
  operator = tolower( operator );
  if ( 'x' ==operator ) {
    return; // calling exit() is too harsh
  }
  else if ( 'c' == operator ) {
    // clear screen in portable way, if possible, i.e. probably not
  }
}
Those make sense. I will try revising my code with these to see if it works out. What is the #include file that is needed to run the (auto, find) functions?
Topic archived. No new replies allowed.