I have written various programs using the if condition, and they always seem to work only when I am using integer or float constants. Whenever i use char constants, the if condition completely ignores it. Here is an example:
#include <stdio.h>
int main(void)
{
int age;
char status,gender;
printf("Enter age, marital status and gender");
scanf("%d%c%c",&age,&status,&gender);
if((status=='m')||(status=='u' && gender=='m' && age>35) ||
(status=='u' && gender=='f' && age>25)
)
printf("Driver is insured" );
else
printf("Driver is not insured");
}
For instance, if I enter status as u, gender as m and age as 38, it yet says that driver is not insured. Here is the program from cmd:
Enter age, marital status and gender 39 u m Driver is nt insured Press any key to continue . . .
Aucun commentaire:
Enregistrer un commentaire