Help with input validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sonic
    New Member
    • Nov 2006
    • 40

    Help with input validation

    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:


    Code:
    / Definition of printError which determines if user has entered         *
    // acceptable input.                                                     *
    //**************************************************  **********************
    
    void printError(int& rows, char& characterSelect)
    {
    
    	if ((rows < 2) || (rows > 14) || (rows%2 != 0))
    	{
    		cout << "\nInvalid number of rows. Please retry.\n";
    		cout << "\n";
    		getInput(rows, 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!
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by sonic
    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:

    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.

    Code:
    (characterSelect != '*')
    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:

    Code:
    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!

    Comment

    • sonic
      New Member
      • Nov 2006
      • 40

      #3
      Thank you so much for your help. That seemed to clear everything up.

      Comment

      Working...