validation

This is a discussion on validation within the C Programming forums, part of the General Programming Boards category; Hi, can someone tell me what's wrong with this code? Code: while ((num <= 1) && (num >= 9999)) { ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    6

    validation

    Hi, can someone tell me what's wrong with this code?

    Code:
            while ((num <= 1) && (num >= 9999)) {
            printf("\n");
            printf("\tinventory number: ");
            scanf("%d", &num);
            if (num <= 1)
            printf("Invalid entry\n");
            printf("Please enter a value greater than or equal to 1\n");
            if (num >= 9999)
            printf("Invalid entry\n");
            printf("Please enter a value less than or equal to 9999");
            }
    I want it to ask for an inventory number until the user enters a number between 1-9999 and give an error message if the number is outside of that range.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    can someone tell me what's wrong with this code?
    The most glaring thing is the lack of indentation
    Second is that you need a OR and not an AND
    Third instead of having two if statements change it to one
    Code:
    while ( (num <= 1) || (num >= 9999) )
    {
      printf("\n");
      printf("\tinventory number: ");
      scanf("%d", &num);
      if (num <= 1 || num >= 9999)
      {
        printf("Invalid entry\n");
        printf("Please enter a value between 1 and 9999\n");
      }
    }

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    6
    Thanks man. Can you take a look at this beast?
    Code:
            char ch1 = toupper(cat1);
                    char ch2 = toupper(cat2);
                    while (toupper(char1, != 'F') && (ch1 != 'N') && (ch2 != 'D') && (ch2 != 'M') && (ch2 != 'N') && (ch2 != 'P') && (ch2 != 'S') && (ch2 != 'A') && (ch2 != 'H'))
                    {
                            printf("\tcategory\t: ");
                            while (getchar() != '\n');
                                    scanf("%c%c", &cat1, &cat2);
    
                                    if (ch1 == 'F')
                                    {
                                            if ((ch2 != 'D') && (ch2 != 'M') && (ch2 != 'N') && (ch2 != 'P') && (ch2 != 'S'))
                                            {
                                                    printf("Invalid entry\n");
                                                    printf("Second character must be D,M,N,P,or S\n");
                                            }
                                    }
                                    else if (ch1 == 'N')
                                    {
                                            if ((ch2 != 'A') && (ch2 != 'H') && (ch2 != 'M') && (ch2 != 'P') && (ch2 != 'S'))
                                            {
                                                    printf("Invalid entry\n");
                                                    printf("Second character must be A,H,M,P,or S\n");
                                            }
                                    }
                                    else {
                                            printf("Invalid entry");
                                            printf("Category must be F or N\n");
                                    }
                    }
    The first character must be either F or N. If the first character is F, then the second character must be D,M,N,P,or S. If the first character is N, then the second character must be A,H,M,P,or S.
    Last edited by blanny; 03-05-2004 at 04:08 PM.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Another method:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void validate(int ch1, int ch2)
    {
      switch (ch1)
      {
        case 'F':
          if (strchr("DMNPS", ch2))
          {
            puts("Valid by first choice");
          }
          else
          {
            puts("Invalid by first choice");
          }
    
          break;
    
        case 'N':
          if (strchr("DMNPS", ch2))
          {
            puts("Valid by second choice");
          }
          else
          {
            puts("Invalid by second choice");
          }
    
          break;
    
        default:
          puts("Not even close");
          break;
      }
    }
    
    int main(void)
    {
      validate('F', 'Z');
      validate('N', 'M');
      validate('Z', 'Z');
      return(0);
    }
    There are many ways to do this...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50

    Re: validation

    Code:
            while ((num <= 1) && (num >= 9999)) {
            printf("\n");
            printf("\tinventory number: ");
            scanf("%d", &num);
            if (num <= 1)
            printf("Invalid entry\n");
            printf("Please enter a value greater than or equal to 1\n");
            if (num >= 9999)
            printf("Invalid entry\n");
            printf("Please enter a value less than or equal to 9999");
            }
    The evaluation in your while loops wants number less then 1 or a number greater then 9999, but your second if statement will givea validation error if the user selected number is in fact greater than 9999. Isn't this contradicting?

    I think you want:

    Code:
    while (num >= 1 && num <= 9999) {

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    6
    Hammer, can you briefly explain what your code is doing here?

    Code:
    int main(void)
    {
      validate('F', 'Z');
      validate('N', 'M');
      validate('Z', 'Z');
      return(0);
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by blanny
    Hammer, can you briefly explain what your code is doing here?

    Code:
    int main(void)
    {
      validate('F', 'Z');
      validate('N', 'M');
      validate('Z', 'Z');
      return(0);
    }
    The code is calling the validate function 3 times, to test different scenarios. Normally a validation function would also return a value representing success or failure, but my example is left to simply write out to the screen.

    What I provided is a compilable program, have a go at running it yourself, and making alterations to the parameters in the validate() function calls and observe the results.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    6
    Here's how I'm trying to implement it:

    Code:
    while (valid != 1)
                    {
                            printf("\tcategory\t: ");
                            while (getchar() != '\n');
                                    scanf("%c%c", &cat1, &cat2);
    
                            void validate(int ch1, int ch2)
                            {
                                    switch (ch1)
                                    {
                                            case 'F':
                                            if (strchr("DMNPS", ch2))
                                    {
                                            valid = 1;
                                    }
                                    else
                                    {
                                            puts("Second character must be D,M,N,P,or S");
                                    }
                                            break;
    
                                    case 'N':
                                    if (strchr("AHMPS", ch2))
                                    {
                                            valid = 1;
                                    }
                                    else
                                    {
                                            puts("Second character must be A,H,M,P,or S");
                                    }
                                            break;
    
                                    default:
                                            puts("First character must be F or N");
    
                                            break;
                            }
    
                            validate(ch1, ch2);
                    }

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Posted by OOPboredom
    I think you want:
    Code:
    while (num >= 1 && num <= 9999) {
    If you re-read the error messages inside the loop you will see that he wants numbers in the range from 1 to 9999 inclusive. Or you can just re-read his first post[qoute]I want it to ask for an inventory number until the user enters a number between 1-9999[/quote]
    Now I can make one little change to your loop to make it valid though
    Code:
    while ( !(num >= 1 && num <= 9999) ) {

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Here's how I'm trying to implement it:
    Your layout is wrong, validate() is a complete function and must live outside all other functions. Look at the layout of this summarised version:
    Code:
    int validate(char ch1, char ch2)
    {
      int valid = 0;
      /*
       * Do validation here
       */
      return valid;
    }
    
    int main(void)
    {
      char cat1, cat2;
      
      /*
       * read user input here 
       */
      
      /*
       * Now call validate()
       */
      if (validate(cat1, cat2) == 1)
      {
        puts ("Yes, its valid");
      }
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation of TextBoxes!?
    By c9dw2rm8 in forum C# Programming
    Replies: 1
    Last Post: 04-01-2008, 06:19 PM
  2. set validation
    By jmarsh56 in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2005, 07:49 AM
  3. DATE <TIME.H> data validation
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 08-07-2005, 01:10 AM
  4. validation routine help plz
    By rajesh23 in forum C Programming
    Replies: 8
    Last Post: 09-23-2003, 07:21 AM
  5. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 11:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21