0

I am writing a function which checks to see if a character inputted by the user matches the string "qwerty". I have to write the function getCharOption which essentially is a function that receives a string and sees if the user-entered character matches any of the characters in the string.

The problem I am facing is that the getCharOption function is unable to validate the characters entered. I am supposed to input 'R', 'p' then 'r' in order for the message on the screen to say PASSED. If the user enters a character that doesn't match the string, an error message should pop up and allow the user to enter it again.

This is the function which calls getCharOption:

   void test07_getCharOption(void)
{
    char charValue;

    printf("TEST #7 - Instructions:\n"
        "1) Enter the character 'R' [ENTER]\n"
        "2) Enter the character 'p' [ENTER]\n"
        "3) Enter the character 'r' [ENTER]\n"
        ":>");

    // You may want to comment the next line if you have not yet created the getInteger function:
    charValue = getCharOption("qwerty");

    printf("////////////////////////////////////////\n");
    printf("TEST #7 RESULT: ");
    if (charValue == 'r')
    {
        printf("*** PASS *** \n");
    }
    else
    {
        printf("### FAILED ###\n");
    }
    printf("////////////////////////////////////////\n\n");
}

This is what I have so far:

    char getCharOption(const char text[31]) {
    
        char input;
        int count = 0, i;
        scanf("%c", &input);;
        clearStandardInputBuffer();
        for (i = 0; text[i] != '\0'; i++) {
            if (text[i] == input) {
                count++;
            }
            if (count == 0) {
                printf("ERROR: Character must be one of [qwerty]:");
    
        }
return input;
    }

When I compile this program, this ends up being my output when I input an incorrect character. The program should allow me to re-enter another character with the error message shown.

TEST #7 - Instructions:
1) Enter the character 'R' [ENTER]
2) Enter the character 'p' [ENTER]
3) Enter the character 'r' [ENTER]
:>R
////////////////////////////////////////
TEST #7 RESULT: ### FAILED ###
////////////////////////////////////////

It won't even register a valid character inputted. This is the result when I put in the character 'r':

TEST #7 - Instructions:
1) Enter the character 'R' [ENTER]
2) Enter the character 'p' [ENTER]
3) Enter the character 'r' [ENTER]
:>r
////////////////////////////////////////
TEST #7 RESULT: ### FAILED ###
////////////////////////////////////////

0 Answers0