How to control user input for char type in C programming??
Problem: If user give 'Male' as first input then program did not ask to give 'age'
Output
Enter gender(M/F):Male
Enter age:
Your Gender is Male(M)
Your Age:71 //garbage value
#include <stdio.h>
int main()
{
char gender;
int age;
printf("\nEnter gender(M/F):");
scanf("%c", &gender);
printf("\nEnter age:");
scanf("%d", &age);
if(gender=='M'){
printf("\nYour Gender is Male(%c)",gender);
}else{
printf("\nYour Gender is not Male(%c)",gender);
}
printf("\nYour Age a:%d",age);
return 0;
}
Expected Output: if user give input 'Male'/'Female'/'M'/'F' then hit enter then console will ask to give age
Enter gender(M/F):Male
Enter age:23
Your Gender is Male(M) //base on &gender
Your Age:23