Search code examples
c

if condition ignored in VSC


In VSC my if got skipped and it went straight for the else condition. Here's the code, thank you for helping ;-;

#include <stdio.h>
#include <ctype.h>
int main() 
{
    char questions[][100] = {"1. What is Nanook Path?", 
                             "2. Who fixed the Astral Express?", 
                             "3. What is Firefly's armor type?"};
    char options[][100] = {"A. Abundance", "B. The Hunt", "C. Destruction", "D. Preservation", 
                           "A. Himeko", "B. Welt", "C. Pom Pom", "D. Me obviously", 
                           "A. Type IX", "B. Type IV", "C. Type V", "D. PType VI"};
    int numberofquestions = sizeof(questions)/sizeof(questions[0]);
    char answers[] = {'C', 'A', 'B'};
    char guess;
    int score = numberofquestions;
    
    for (int i = 0; i < numberofquestions; i++)
    {
        printf("%s\n", questions[i]);

        for(int j = i * 4; j < i * 4 + 4; j++)
        {
            printf("%s\n", options[j]);
        }

        printf("Enter your guess:");
        scanf("%c", &guess);
        scanf("%c"); //clear \n character from input buffer

        guess = toupper(guess);

        if (guess == answers[i])
        {
            printf("Correct!!\n");
        }
        else
        { 
            printf("Wronggg!!!\n");
            score--;
        }
    }
    printf("%d", score);
    return 0;
}

I have tried the code on the online c compiler and it works just fine. I also tried reset VSC but still doesn't work.


Solution

  • Your code invokes undefined behaviour:

    scanf("%c");
    

    You must provide a matching argument for each format specifier. In this case you must provide the address of the character that is read.

    Failing to do so, invokes undefined behaviour and basically anything can happen. In this case the function will probably take whatever it finds in the place where the address should be stored and use it to save the data. That could be the variable you used last time when you called scanf, i.e. guess, or some neighboring variable or any other location. Or it could do something completely different.

    That said, any recent compiler is well aware of your error. And I would expect that you should get some warning about it.

    If you did not get a warning, you need to crank up warning level. For gcc or clang you should use options -Wall -Wextra -pedantic.

    If you got a warning, you should not have ignored it. The compiler normally knows what it is talking about.

    If I run your code through gcc, I get this output already without any extra options:

    $ gcc test.c -o test
    test.c: In function ‘main’:
    test.c:27:17: warning: format ‘%c’ expects a matching ‘char *’ argument [-Wformat=]
       27 |         scanf("%c"); //clear \n character from input buffer
          |                ~^
          |                 |
          |                 char *
    

    And if I run it (without fixing) I get Correct!! for the right answers. That is nature of undefined behaviour. It can differ from machine to machine.