【发布时间】:2015-02-20 04:17:59
【问题描述】:
下面是我的代码,基本上我试图让我的 do-while 循环运行,只要 if 或 else if 语句被评估为真。但是每次我运行它时,它都会起作用,只是它总是打印 2 else if 语句“您输入的值小于 1”,而不管用户输入如何。我完全不知道为什么它错了。
int get_input(void){
int Value_1;
int status;
do{
//Ask user to enter an odd number between 1 and 9
printf("Enter any odd number between 1 and 9 inclusive> \n");
//Store entered number in Value_1
status = scanf(" %d", &Value_1);
is_valid(Value_1);
}
while (is_valid()!=1);
return(Value_1);
}
int is_valid(int status){
if(status==2 || status==4 || status==6|| status==8){
printf("The value you entered is not odd>\n");
return(0);
}
else if(status > 9){
printf("the value you entered is greater than 9> \n");
return(0);
}
else if(status < 1){
printf("the value you entered is less than 1> \n");
return(0);
}
else{
return(1);
}
}
int main(void){
int Value_1;
int status;
Value_1=get_input();
printf("%d", Value_1);
return(0);
}
【问题讨论】:
-
您确定您的代码甚至可以编译吗?您正在调用一个带有 no 参数的函数。
标签: c if-statement do-while