0

I try to make a program that the output will be the classification based on the input grade (by character). Here is my code:

main ()
{
    char choice,grade;
    printf("Enter a grade: \n");
    scanf("%c",&grade);
    do
    {
        if (grade == 'a' or grade == 'A')
        {
            printf("Excellent!\nDo you want to continue?\n");
        }
        else if (grade == 'b' or grade == 'B')
        {
            printf("Good!\n Do you want to continue?\n");
        }
        else if (grade == 'c' or grade == 'C')
        {
            printf("Fair!\n Do you want to continue?\n");
        }
        else if (grade == 'd' or grade == 'D')
        {
            printf("Average!\n Do you want to continue?\n");
        }
        else if (grade == 'f' or grade == 'F')
        {
            printf("Weak!\n Do you want to continue?\n");
        }
        else
        {
            printf("Invalid!\n Do you want to continue?\n");
        }
        scanf("%c",&choice);
    } while (choice == 'y');
}

And this is the outcome of the code: enter image description here

It doesn't allow me to type yes or no. It just ends.

It would be very great if someone explain to me where I'm wrong since this is the first coding language that I learn and I am trying so hard to understand it by myself.

  • Change `scanf("%c",&choice);` to be `scanf(" %c",&choice);` and same for `scanf("%c",&grade)`. That is, add space before `%c`. See duplicate post for more details. – kaylum Feb 16 '22 at 03:16
  • Thank you for your answers guys. The problem with my code is that I should left a space before the %c. Another thing is that in other to have a program that function properly as required, the ` printf("Enter a grade: \n");` and the ` scanf(" %c",&grade);` should also be brought into the DO part. – Trí Huỳnh Feb 16 '22 at 03:57

1 Answers1

0

Your scanf() inside the do-while is getting skipped because when you press enter after inputting a grade, scanf() accepts it as a return character. So, do this instead: scanf(" %c", &grade).

onapte
  • 217
  • 2
  • 8