use*_*627 0 c while-loop strcmp
我想在C代码中做的是检查用户输入并验证他们只能输入"一个"或"两个".我做了一个while循环,检查用户输入值strcmp,但它不起作用.while循环似乎忽略了getchar();并且进行了无限循环.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char choice[80];
while(strcmp (choice,"one") != 0 || strcmp (choice,"two") != 0){
scanf("%s", &choice);
getchar();
}
// if the user enters either one or two, continue executing code...
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Bri*_*new 10
当然你想要的
while(strcmp (choice,"one") != 0 && strcmp (choice,"two") != 0)
Run Code Online (Sandbox Code Playgroud)
如果输入不满足不等于1或不等于2的条件,则当前版本将继续循环.如果它等于一,它将不等于两个等....
很少你想要不等于平等的 OR OR在一起......