Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
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]:");

    }
}


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 ###
////////////////////////////////////////


What I have tried:

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]:");

    }
}
Posted
Updated 10-Dec-21 20:08pm

1 solution

Your function getCharOption is supposed to return a character, and your calling code test the return value to see if the check worked:
charValue = getCharOption("qwerty");

printf("////////////////////////////////////////\n");
printf("TEST #7 RESULT: ");
if (charValue == 'r')
{
    printf("*** PASS *** \n");
}
else
{
    printf("### FAILED ###\n");
}
printf("////////////////////////////////////////\n\n");
But there is no return statement in the getCharOption function at all:
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]:");
    }
}
So whatever it tested, it isn't valid data at all!
You should be returning 'r' for success, and something else for an error.

I'd also strongly suggest you read the instructions for your assignment again: you seem to have confused what you are supposed to do rather a bit - "qwerty" does not contain a "p", so at least one of your tests should fail!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900