PDA

View Full Version : char


Xiang
04-22-2003, 05:48 PM
Dear sir,

Refers to the below program. I want to fix one character on the Gender field. Should I used carriage return character to control user who enter more than one characters in Gender field? How can I do that??


Char Gender;

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


Best Regards,

Xiang

Josh Campbell
04-22-2003, 09:45 PM
Should probaly be using getchar if wanting only one character:

int getchar(void);

You could use a loop to ask the user for gender until they enter a valid one

Jason
04-22-2003, 10:43 PM
you could use a do while so that you can check the char to make sure it is M or F and if it isn't you ask again until it is either one and not more or les...


Jason

Phantom
04-26-2003, 12:44 AM
#include <stdio.h>

main() {
char gender[6];

printf("Enter your gender: ");
getchar(gender);

if((gender[0] == 'M') || (gender[0] == 'm')) {
printf("\nYou are a male\n");
} else if((gender[0] == 'F') || (gender[0] == 'f')) {
printf("\nYou are a female\n");
} else {
printf("Enter a valid gender!");
}
return 0;
}