我正在创建一个程序,要求输入,然后根据输入拒绝它或接受它。这是代码:
while(1){
printf("Enter name: ");
scanf("%s", &name);
if(name[0] == '\0'){
printf("Input Cannot be empty\nExample: bobshmurda\n");
} else {
break;
}
}
printf("Enter age: ");
scanf("%d", &age);
while(!age>= 15){
printf("Age\n");
}
while(1){
printf("Enter MMN: ");
scanf("%d", &mmn);
if (!cvv >= 3){
printf("\nInvalid MMN... Try again\n");
} else {
break;
}
}
while(1){
printf("DOB: ");
scanf("%d", &dob);
if (!exp == 4){
printf("Invalid DOB detected... Format: 0123\n");
} else {
break;
}
}
我基本上想做的是,例如,如果年龄大于100或小于0,请执行此操作,等等。与字符串相同,我该怎么做?
答案 0 :(得分:1)
do-while循环可能是最简单的方法:
int is_valid = 0;
do {
// Get user input here
is_valid = validate_input(...);
} while (! is_valid);
// Continue with processing...