what is the bug that is preventing this C code from determining the right answer?

Viewed 69

I'm new to C and I'm building a quiz game, every time you answer right your score gets higher, however there is a bug that i was not able to find which is even if you answer correctly the code will tell you that you are wrong and moves to the next question in the end you will always get a score of 0/3. my question is what is the reason that is preventing my code from determining that the answer is right, why does it always go to the else statement which is wrong.

this is the code

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    char questions[][100] = {
        "\n1. what company was the first to create a foldable phone? ",
        "\n2. what company was the first to create a foldable laptop? ",
        "\n3. what company was the first to create a full electric car? "
    };

    char options[][100] = {
        "\nA. apple, B. oppo, C. samsung, D. motorolla",
        "\nA. Asus, B. MSI, C. Microsoft, D. Gygabyte",
        "\nA. mazda, B. chevrollet, C. toyota, D. Tesla"
    };

    char answers[] = {'C', 'A', 'D'};

    int score = 0;
    char ans;

    for (int i = 0; i < sizeof(questions) / sizeof(questions[0]); i++)
    {
        printf("%s", questions[i]);
        printf("%s", options[i]);

        printf("\n\nwhat is your answer? ");
        scanf("%c", &ans);
        scanf("%c");

        ans = toupper(ans);

        if (ans == answers[i])
        {
            score += 1;
            printf("\nthat's right!");
            printf("\nnext question\n");
        }
        else
        {
            printf("\nwrong!");
            printf("\nnext question\n");
        }
    }

    printf("\n\nthe correct answers are:  ");
    for (int i = 0; i < sizeof(questions) / sizeof(questions[0]); i++)
    {
        printf("%c  ", answers[i]);
    }

    printf("\nyour score is: %d/3", score);
}

ans is the user guess. if anyone have the solution for this i would really appreciate it. thanks in advance.

1 Answers

ok , so you have only one problem in your code and a warning .

in these 2 lines :

scanf("%c", &ans);
scanf("%c");

when the user enter a char like a and press enter , the first scanf will get a successfully but the second scanf("%c"); is incorrect as you are telling the function to get a character from the input and store it but where ? , it gave me undefined behavior .

note that , in order to clear the buffer after reading one char , you can simply write instead of the above 2 lines , these lines :

scanf("%c", &ans);
fgets(dummy, sizeof(dummy), stdin);

where fgets(dummy, 20, stdin); will read any chars there in the buffer so as to clear it.

also there is a warning in this line ans = toupper(ans); as the warning in my compiler says :

Clang-Tidy: Narrowing conversion from 'int' to signed type 'char' is implementation-defined

as the function called toupper takes int and returns int but you are storing the result into a char , so either cast the result as ans = (char)toupper(ans); or you can use _toupper which takes char and returns char like ans = _toupper(ans);

and this is the full code with these only 2 edits :

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    char questions[][100] = {"\n1. what company was the first to create a foldable phone? ",
                             "\n2. what company was the first to create a foldable laptop? ",
                             "\n3. what company was the first to create a full electric car? "};

    char options[][100] = {"\nA. apple, B. oppo, C. samsung, D. motorolla",
                           "\nA. Asus, B. MSI, C. Microsoft, D. Gygabyte",
                           "\nA. mazda, B. chevrollet, C. toyota, D. Tesla"};

    char answers[] = {'C', 'A', 'D'};

    int score = 0;
    char ans, dummy[20];

    for (int i = 0; i < sizeof(questions) / sizeof(questions[0]); i++)
    {
        printf("%s", questions[i]);
        printf("%s", options[i]);

        printf("\n\nwhat is your answer? ");
        scanf("%c", &ans);
        fgets(dummy, sizeof(dummy), stdin);

        ans = _toupper(ans);

        if (ans == answers[i])
        {
            score += 1;
            printf("\nthat's right!");
            printf("\nnext question\n");
        }
        else
        {
            printf("\nwrong!");
            printf("\nnext question\n");
        }
    }

    printf("\n\nthe correct answers are:  ");
    for (int i = 0; i < sizeof(questions) / sizeof(questions[0]); i++)
    {
        printf("%c  ", answers[i]);
    }
    printf("\nyour score is: %d/3", score);
}

and this is the output :

1. what company was the first to create a foldable phone?
A. apple, B. oppo, C. samsung, D. motorolla

what is your answer?c

that's right!
next question

2. what company was the first to create a foldable laptop?
A. Asus, B. MSI, C. Microsoft, D. Gygabyte

what is your answer?a

that's right!
next question

3. what company was the first to create a full electric car?
A. mazda, B. chevrollet, C. toyota, D. Tesla

what is your answer?d

that's right!
next question


the correct answers are:  C  A  D
your score is: 3/3
Process finished with exit code 0
Related