简体   繁体   中英

Simple C program using If statement stops running in VS Code

I made a simple C program to understand the working of the If-Else statement but in VS Code the program stops at second input without any error prompt. Please tell me what's the problem with my program? I'm a beginner in programming.

#include <stdio.h>

int main(){

    char math, sci;

    printf("Have you passed Mathematics test (y/n)\n");
    scanf("%c", &math);

    printf("Have you passed Science test (y/n)\n");
    scanf("%c", &sci);

    if ((math == 'y') && (sci == 'y'))
    {
        printf("You get a gift of worth Rs. 45.");
    }
    else if ((math == 'n') && (sci == 'y'))
    {
        printf("You get a gift of worth Rs. 15.");
    }
    else if ((math == 'y') && (sci == 'n'))
    {
        printf("You get a gift of worth Rs. 15.");
    }
    else if ((math == 'n') && (sci == 'n'))
    {
        printf("You don't get any gift.");
    }

    return 0;
}

The second scanf() reads the newline that was left pending in stdin by the first scanf() .

Use scanf(" %c", &sci); with an initial space in the conversion string to consume any newlines and initial white space in the input. Also test the return value of scanf() to detect premature end of file.

Here is modified version:

#include <stdio.h>

int main() {

    char math, sci;

    printf("Have you passed Mathematics test (y/n)\n");
    if (scanf(" %c", &math) != 1) {
        printf("Missing input\n");
        return 1;
    }

    printf("Have you passed Science test (y/n)\n");
    if (scanf(" %c", &sci) != 1) {
        printf("Missing input\n");
        return 1;
    }

    if ((math == 'y') && (sci == 'y')) {
        printf("You get a gift of worth Rs. 45.\n");
    } else
    if ((math == 'n') && (sci == 'y')) {
        printf("You get a gift of worth Rs. 15.\n");
    } else
    if ((math == 'y') && (sci == 'n')) {
        printf("You get a gift of worth Rs. 15.\n");
    } else
    if ((math == 'n') && (sci == 'n')) {
        printf("You don't get any gift.\n");
    } else {
        printf("Invalid input.\n");
    }
    return 0;
}

You just change

scanf("%c", &math) to scanf(" %c", &sci)

scanf("%c", &sci) to scanf(" %c", &math)

#include <stdio.h>

int main(){

    char math, sci;
    printf("Have you passed Mathematics test (y/n)\n");
    scanf(" %c", &math);
    printf("Have you passed Science test (y/n)\n");
    scanf(" %c", &sci);
    if ((math == 'y') && (sci == 'y'))
    {
         printf("You get a gift of worth Rs. 45.\n");
    }
    else if ((math == 'n') && (sci == 'y')||(math == 'y') && (sci == 'n'))
    {
         printf("You get a gift of worth Rs. 15.\n");
    }
    else if ((math == 'n') && (sci == 'n'))
    {
         printf("You don't get any gift.\n");
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM