Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Validating Data

Hi all,
I need to write a program in Unix using C. It's basically a quiz with multiple choice answers. I'm having trouble validating input. I did one in perl using something like this
if ($answer1=~/[a,A,b,B,c,C,d,D]/)
yada yada yada

Any help you could give me would be great.
Thanks
Take care

Comments

  • ajh_ptrajh_ptr Member Posts: 2
    : Hi all,
    : I need to write a program in Unix using C. It's basically a quiz with multiple choice answers. I'm having trouble validating input. I did one in perl using something like this
    : if ($answer1=~/[a,A,b,B,c,C,d,D]/)
    : yada yada yada
    :
    : Any help you could give me would be great.
    : Thanks
    : Take care
    :
    :

    You could use a function like this:

    int is_valid(char * buf)
    {
    char valid[9] = {'a','A','b','B','c','C','d','D',''};
    if(strlen(buf)==1 && strstr(valid,buf)!=NULL)
    return 1;
    return 0;
    }


    And then do a:

    if(is_valid(buf))
    ; /* something */


    I tested this by using fgets to fill buf and giving
    the size of buf as fgets second parameter as 2.
  • sumitxshahsumitxshah Member Posts: 20
    : Hi all,
    : I need to write a program in Unix using C. It's basically a quiz with multiple choice answers. I'm having trouble validating input. I did one in perl using something like this
    : if ($answer1=~/[a,A,b,B,c,C,d,D]/)
    : yada yada yada
    :
    : Any help you could give me would be great.
    : Thanks
    : Take care
    :
    :
    You can also "Switch...break...Case...default " statments to validate your multipule choice questions.
    Make a seprate function for this very purpose and just return 1 or 0
    bye.
    A winner is not the one who never fails , but the one who never quits.
    Sumit Shah.

Sign In or Register to comment.