Thread: user input verification

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    62

    user input verification

    So, I'm seeing if i can (re)learn programming as a more useful way to spend my time then watch tv.
    That said I'm trying to make a card game but I have problem with my input verification function:
    Code:
    int checknames(char *str,int op) {
    /*in progress...*/
      int i,j,test,resp=0;
      char faces[]="aAkKqQjJtT23456789";
      char suits[]="sShHcCdD";
      for (i=0;i<strsize(str)-op;i++) {
          test=0;
          if (i%3 == 0)
              for (j=0;faces[j];j++) 
                  if (*(str+i) == faces[j]) {
                      test=1;
                      break;
                    }
          if (i%3 == 1)
              for (j=0;suits[j];j++)
                  if (*(str+i) == suits[j]) {
                      test=1;
                      break;
                    }
          if (i%3 == 2)
              if (*(str+i) == ' ') {
                  test=1;
                  break;
                }
          if (!test) {
              resp=1;
              break;
            }
          }
      return resp;
    }
    The idea to verify that the input is in the correct form (that being 'As 5h 6c' for example) and return 1 if the verification fails (the op int is there to tell the function to ignore (or not) the last char of the string and will be either 0 or 1).

    So, what I was trying to accomplish was: check if the 1st char is a "face", check if the 2nd char is a "suit", check if there is a ' ' next, repeat until the end of the string (except \0 and sometimes the 1 before), return 1 if it fails or 0 if it passes.

    If you could detect what i did wrong (or give any advise in general) i would appreciate it.
    Last edited by killme; 04-27-2011 at 02:31 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Umm...try using strchr to check if a face or suit designator is valid, something like
    Code:
    strchr(faces, str[0])  // returns NULL if str[0] is not in faces
    Your valid card is any string that is exactly 2 characters long, with str[0] in faces and str[1] in suits. You could make this a one-line function.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So I seemed to have missed the part about the space-separated list of cards. You can split that up into individual cards using something like strtok or sscanf to extract individual cards from the string. You can pass those into the function I described in my previous post. Separating them might not be a bad idea, since you may want to check the validity of an individual card elsewhere in your program too.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    62
    done

    you were right, it is cleaner to separate them with sscanf. thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need help with getting input from the user.
    By nyekknyakk in forum C Programming
    Replies: 7
    Last Post: 08-17-2010, 10:22 PM
  2. C User input
    By Martin_T in forum C Programming
    Replies: 13
    Last Post: 11-19-2009, 07:09 AM
  3. End of user input must be 999
    By naspek in forum C Programming
    Replies: 23
    Last Post: 07-22-2009, 11:04 PM
  4. using user input as a var.
    By nubi in forum C++ Programming
    Replies: 13
    Last Post: 05-06-2003, 12:27 PM
  5. user input
    By hen in forum C Programming
    Replies: 16
    Last Post: 06-27-2002, 11:09 PM