samedi 27 février 2016

C programming, skipped a scanf function unexpectedly

I'm coding on Visual Studio 2015. When I execute below C program, during the execution program skips else part's character scanf function and displays "You entered an invalid character." directly. What is the problem?

    double grade1, grade2, avarage;
    char yes_no;            //character variable for (Y/N) statement
    int std;            //index for students
    int std_num;        //number of students
    printf("Enter how many students will be determined: ");
    scanf("%d", &std_num);
    for (std = 1; std <= std_num; std++);       //loop statement
    {//curly brace for loop

        printf("Enter two grades: ");       //display for inputs
        scanf("%lf %lf", &grade1, &grade2);     //input statement

        avarage = (grade1 + grade2) / 2;

        if (avarage >= 50)
            printf("Successful.\n");
        else
        {
            printf("Did the student take this exam before?(Y/N): ");
            scanf("%c", &yes_no);

            if (yes_no == 'Y' || yes_no == 'y')
                printf("Unsuccessful, and has no other chance.\n");
            else if (yes_no == 'N' || yes_no == 'n')
                printf("Unsuccessful, but has one more chance.\n");
            else
                printf("You entered an invalid character.\n");
        }
    }//curly brace for end of the loop
return 0;
}

Aucun commentaire:

Enregistrer un commentaire