String Validation (Newbie)

This is a discussion on String Validation (Newbie) within the C Programming forums, part of the General Programming Boards category; Hey Guys, Im working on an exersise that requires the user enter a string(ID) and it must be of 4 ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    33

    String Validation (Newbie)

    Hey Guys,

    Im working on an exersise that requires the user enter a string(ID) and it must be of 4 chars long the first digit must be a char and the following 3 digits must be numeric. EG: w123

    I know what I want to say but just cant get it into C.

    if the user enters the string and its captured by the Var "ID"

    I want to say something like:

    Code:
    if (ID(0)!== "a string" && ID(1)!== "a number" && ID(2)!== "a number" && ID(3)!== "a number" )
    printf("The code you entered is in an incorrect format");
    
    else
    
    Carry on............
    I realise thats probably complete jibberish heehee but I am stuck.

    Thanks again in advance for any assistance.

    Regards as ever
    C

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Hmm... do you even know how strings in C work? You might want to back up a little bit, and learn about those before you learn how to validate them.

    If you do know how C strings work, then take a look at the functions isdigit() and isalpha() which are declared in ctype.h

  3. #3
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    PHP!!! colinuk you are awesome! I love PHP, you are my friend! Try something like this:
    Code:
    #include <ctype.h>
    #include <stdio.h>
    
    int main(void)
    {
        char ID[5] = "E333";
        //get ID here
    
        // Validate ID
        if (!isalpha(ID[0]) || !isdigit(ID[1]) || !isdigit(ID[2]) || !isdigit(ID[3])) {
            printf("Bad format, bad!\n");
            return 1;
        }
    
        return 0;
    }
    You were close!

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Hey Kleid-0

    I tried what you suggested and generate an error .
    The code i have is
    Code:
    while(check_reg==1)
    {
       // Validate Reg
    
       if (!isalpha(record_id[0]) || !isdigit(record_id[1]) || !isdigit (record_id[2]) || !isdigit(record_id[3]))
    {
       printf("Bad format, bad!\n");
       printf("Please enter an Reg No eg: w123 \n");
       scanf("%s",&record_id);
       fflush(stdin);
       check_reg = 1;
     }
    
       check_reg= 0;
    }
    The error says:
    "Invalid indirection in function"

    It errors out on this line:
    Code:
    if (!isalpha(record_id[0]) || !isdigit(record_id[1]) || !isdigit (record_id[2]) || !isdigit(record_id[3]))
    Any thoughts are welcome.

    Thanks again
    C

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by colinuk
    The error says:
    "Invalid indirection in function"

    It errors out on this line:
    Code:
    if (!isalpha(record_id[0]) || !isdigit(record_id[1]) || !isdigit (record_id[2]) || !isdigit(record_id[3]))
    Any thoughts are welcome.
    The most important piece of information is your declaration of record_id. [edit]It seems like you may have declared it as a single char rather than an array.

    But these two lines are also prone to error:
    Code:
       scanf("%s",&record_id);
       fflush(stdin);
    Of interest may be these FAQs:
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Jez
    Jez is offline
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    colinuk: in your original post you say "!==", I think you mean "!=".

  7. #7
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    In PHP he would be comparing variable types.
    Code:
    int !== int  False
    char !== int  True
    double !== int   True
    char !== char False
    But that's just the thing, he's not sure how to do the same thing in C, but s/he shall learn the ways!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

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