john k john k - 2 months ago 12
C Question

If else statement doesn't work in C

#include<stdio.h>

void main()
{
//Declaration
char grade[1];

//Data Input
enter code here
printf("Enter a grade{A,B,C,D,F]: ");
scanf("%c",&grade);

//If Statement

if(grade=='a'=='A')
printf("The score for the grade 'A' is 4.00");
else
if(grade=='b '=='B')
printf("The score for the grade 'B' is 3.00");
else
if(grade=='c'=='C')
printf("The score for the grade 'C' is 2.00");
else
if(grade=='d'=='D')
printf("The score for the grade 'D' is 1.00");


I try it so many times but the if else statement just didn't pop up
I trying to make the program recognize both upper case and lower case of the grade A-F or a-f. but it didn't seem to work

Enter a grade{A,B,C,D,F]: a

Process returned 1 (0x1) execution time : 2.599 s
Press any key to continue.

gives me this

Answer

It doesn't fail. Your comparison fails. You are saying:

If grade is equal to 'a' AND 'a' is equal to 'A'....

Which can never be true. Try:

if(grade == 'a' || grade == 'A')...