error check

I am calculating a full expression regardless of spaces. So I have a program that accepts a string ex: "56 + 67", and passes it by reference to a void function that first removes the spaces and sets it to correctly to "56+67",
it then returns to main and passes it to the void process function to correctly process it according to the specifications laid out in the lab details.

It runs a check:

1
2
3
4
5
6
7
8
9
10
string temp1, temp2;
  char side = 'l';
  char chcheck = 'g';

//int a = userInput.size() -1;
if(userInput == "q" || userInput == "Q"){
  exit = 'q';
  cout << "Exiting.";
  return;
  }


if the string is equal to q or Q it will return and exit, if not it will run another check and then a for look to check for any characters in the string that are not digits or +-*%/ operators. If it is not a digit, it checks if it a operator and if it isn't it couts and returns back to main.
1
2
3
4
5
6
7
8
9
10
11
12
13
  else if(userInput != "q" || userInput != "Q"){

  for(int i=0; i<userInput.size(); i++){
    if(!isdigit(userInput[i])){
      if(userInput[i] != '+' || userInput[i] != '%' || userInput[i] != '-' || userInput[i] != '*' || userInput[i] != '/'){
        cout << "Input cannot contain any characters that are neither digits,";
        cout << " an operator, or the quit command\n";
        return;
      }
      else
      continue;
    }
  }


for some reason I can't figure out it'll still cout the error message when the expression is "56+67"?? Any help please.
(userInput[i] != '+' || userInput[i] != '%' || userInput[i] != '-' || userInput[i] != '*' || userInput[i] != '/')

This expression is true. Always, always true.

There is no value of userInput[i] for which this is not true.

Pick a value of userInput[i]. Let's say the value is '+'.

So the expression becomes

('+' != '+' || '+' != '%' || '+' != '-' || '+' !='*' || '+' != '/')
which comes out as
(false || true || true || true || true)

False OR true OR true OR true OR true. Well, that's true.

There is no value of userInput[i] for which this expresison is not true.
ahh I see! I missed that. Just took it out and replaced it with
1
2
3
4
5
6
7
for(int i=0; i<userInput.size(); i++){
    if(isalpha(userInput[i])){
        cout << "Input cannot contain any characters that are neither digits,";
        cout << " an operator, or the quit command\n";
        return;
    }
  }


Thank you!
Topic archived. No new replies allowed.