Here's a source code I modified from a youtube tutorial. The purpose of the program is:
it ask your age. If you put 18 or more it ask your gender than print that you can enter dude or m'lady(depends on your gender). If you put your age less than 18 it prints "Nothing to see here!"
I want to make sure that if the input isn't exactly what I ask(only number or only character) it shows "Invalid input". In my program, the first part where the program ask for age, when the input for age is either alphabet or symbol it shows invalid input. But whenever I combine alphabet with number like 18a or 18/ it shows:
"Please enter you gender.(m/f)" "Invalid input."
also, if I input 18 or more and go to the part where the program ask about gender(m/f) it shows Invalid input if I put number or symbol.. But when I combine like m1 or m/ it shows:
"You may enter this website dude."
Can anyone help me about how can I remove this problem?
#include <stdio.h>
#include <conio.h>
int main(void) {
char gender;
int age=0;
printf("Please enter your age. \n");
if(scanf(" %d",&age)!=1) {
printf("invalid input. \n");
return 0;
}
if(age>=18) {
printf("Please enter your gender.(m/f) \n");
if(scanf(" %s", &gender) !=1) {
printf("Invalid input.\n");
return 0;
}
else if(gender== 'm') {
printf("You may enter this website dude.\n");
}
else if(gender== 'f') {
printf("You may enter this website m'lady.\n");
}
else {
printf("Invalid input. \n");
return 0;
}
}
else if(age<18) {
printf("Nothing to see here! \n\n");
return 0;
}
return 0;
}