PDA

View Full Version : get char


Xiang
05-24-2003, 04:11 PM
Dear sir,

Refers to the below coding. If user enters FF or MM, the condition was turn to the true. But, I would like to turn this wrong answer to the false condition, how can I do???

printf("Enter your gender [F/M] : ");
scanf("%c", &gender);

if (gender == 'F');
printf('You are Female");

else if (gender == 'M')
printf("You are Male");

else
printf("Invalid gender"); //false condition


Thanks,

Xiang

allison
05-25-2003, 07:03 AM
not sure if I understand what you mean

are you're trying to reverse the logic of the boolean condition.?

if so, all you need to do is negate the condition
by using !

if (!(gender == 'F'))
{
//code

}

there is always the !=
which stands for "not equals "

allison

Jason
05-27-2003, 09:56 PM
you are using the char function to get a char from the user, but can't check if there are multiple chars...try using the string instead so that if the user enters multiple chars the prog will catch the errors....



Jason

Phantom
05-28-2003, 12:30 AM
Which, by the way, is gets()

rflberg
06-03-2003, 05:47 PM
you have a ; at the end of
if (gender == 'F');:mad:

it should read
if (gender == 'F') :)
otherwise it will close the if true block