Validating Data in Unix C

Hi all,
I posted this in the UNIX section but the last post was a month ago. Hopefully one of you brilliant minds can help :)
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

  • : Hi all,
    : I posted this in the UNIX section but the last post was a month ago. Hopefully one of you brilliant minds can help :)
    : 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
    :
    What you can do first of all is that have a tolower() function happening that converts your quiz answer to one case (lower/upper). Have a string variable like char valid_input = "abcd";

    for(i=0;i<strlen(valid_input);i++)
    if(answer != valid_input[i])
    printf("Invalid answer")

    Hope it helps
    code_monkey
    :
    :

  • : : Hi all,
    : : I posted this in the UNIX section but the last post was a month ago. Hopefully one of you brilliant minds can help :)
    : : 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
    : :
    : What you can do first of all is that have a tolower() function happening that converts your quiz answer to one case (lower/upper). Have a string variable like char valid_input = "abcd";
    :
    : for(i=0;i<strlen(valid_input);i++)
    : if(answer != valid_input[i])
    : printf("Invalid answer")
    :
    : Hope it helps
    : code_monkey
    : :
    : :
    :
    :

    Rather do like this, or the loop won't work:
    [code]
    for(...)
    {
    if(answer == valid_input[i])
    {
    printf("right answer")
    break;
    }
    }
    if(i==strlen(valid_input))
    printf("Invalid answer")
    [/code]
  • : Hi all,
    : I posted this in the UNIX section but the last post was a month ago. Hopefully one of you brilliant minds can help :)
    : 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
    :
    do you mean something like:
    [code]
    printf ("Enter Answer (a, b, c, d): ");
    scanf("%c", &ans);
    ans = tolower(ans);
    switch (ans)
    {
    case 'a':
    // do something based on answer
    break;
    case 'b':
    // do something based on answer
    break;
    case 'c':
    // do something based on answer
    break;
    case 'd':
    // do something based on answer
    break;
    default:
    printf ("Invalid seelction, try again
    ");
    break;
    }[/code]
Sign In or Register to comment.

Howdy, Stranger!

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

Categories