I am having problem with my printError function. It handles incorrect input from user dealing with the numerical range. However, on the second condition being evaluated (dealing with character selection for the pattern) I always recieve "Invalid character.", even if the selection is of an eligible type. Here's my code:
- void printError(int& rows, char& characterSelect)
-
{
-
//...
-
else if ((characterSelect != '*') || (characterSelect != '+') || (characterSelect != '#') || (characterSelect != '$'))
-
{
-
cout << "\nInvalid character. Please retry using one of the four characters given.\n";
-
cout << "\n";
-
getInput(rows, characterSelect);
-
}
-
}
Thanks for any help on this others may have!
The problem is in your else if portion. Let's look at the logic determining whether or not your input is bad.
and statements like it are fine - the block should not execute if the character is *, or + or whatever. However, in between the statements, you have || - OR. This means that you are in effect saying, "If the character is not * OR the character is not + OR...then do this." But let's assume your characterSelect is actually '*'. The first condition would evaluate to false (because characterSelect == '*'), but the second condition would evaluate to true (because characterSelect != '+'). At this point, the entire condition has evaluated to true, because of the ||.
Instead, use && - AND. Looking at the same example, the first condition would evaluate to false - therefore, the entire condition is false, because &&s need everything to be true before they execute.
Your new else if will look like this:
- else if ((characterSelect != '*') && (characterSelect != '+') && (characterSelect != '#') && (characterSelect != '$'))
-
{
-
cout << "\nInvalid character. Please retry using one of the four characters given.\n";
-
cout << "\n";
-
getInput(rows, characterSelect);
-
}
Hope that helps!