Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
strcmp在while循环中不工作_C_While Loop_Strcmp - Fatal编程技术网

strcmp在while循环中不工作

strcmp在while循环中不工作,c,while-loop,strcmp,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

我想在我的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;
     }
#包括
#包括
#包括
int main()
{
字符选择[80];
while(strcmp(选项一)!=0 | | strcmp(选项二)!=0){
scanf(“%s”,选择(&C);
getchar();
}
//如果用户输入一个或两个,则继续执行代码。。。
返回0;
}
您肯定想要

while(strcmp (choice,"one") != 0 && strcmp (choice,"two") != 0)
如果输入不满足不等于1或不等于2的条件,则当前版本将继续循环。如果它等于一,它就不等于二等等

您很少希望不等于子句一起或在一起…

使用AND(&&&)运算符而不是OR(| |)

如果使用OR,则当任一条件为true时,该语句为true;如果使用AND,则仅当两个条件均为true时,该语句为true。如果使用AND,则只要选项既不是“一”也不是“二”,就执行循环


另一方面,键盘输入读取行应该是
sscanf(“%s”,选项)

在逻辑公式中,否定按符号分布。所以
!(a | b)=(!a&&!b)

我在您的代码中看到了三个问题:

1. 此条件的计算结果永远不会为
false
。因为用户输入一个或两个或其他任何东西,所以任意一个或两个strcmp条件都返回true

2. 那里不需要
&
。要使用
%s
将字符串读取到字符数组,您不需要使用
&
(因为当您指定字符数组的名称时,地址是指向的)

就这样写吧:

scanf("%s", choice);
3. 这是合乎逻辑的。您刚刚声明了一个字符数组。在下一行中,您将在
strcmp
中使用它。当然,数组将具有垃圾值

我的解决方案: 实现您的代码,如:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
   char choice[80];
   while(1)
   {
        scanf("%s", choice);
        if(strcmp (choice,"one") == 0 || strcmp (choice,"two") == 0)
        {
           break;
        }
        getchar();
   }
   // if the user enters either one or two, continue executing code...
   return 0;
}
#包括
#包括
#包括
int main()
{
字符选择[80];
而(1)
{
scanf(“%s”,选项);
如果(strcmp(选项,“一”)=0 | | strcmp(选项,“二”)=0)
{
打破
}
getchar();
}
//如果用户输入一个或两个,则继续执行代码。。。
返回0;
}

D'oh<代码>扫描频率(“%s”,选项)挑剔的评论。堆栈在启动时可能有“o”、“n”、“e”和“0”,这将导致没有用户输入。您应该在使用选项之前初始化它。或者使用
do{}while
@Armin你是什么意思?@TomTanner我尝试了do/while循环,但没有成功。@user1431627这是因为你的条件错误。但是当您访问未初始化的内存时,代码仍然有未定义的行为。strcmp可能会崩溃,特别是当while子句的整个含义始终为真时(最多“选择是一”或“选择是二”中的一个为真,并且由于strcmp使用0表示相等…)@user1431627:很高兴:)祝您一切顺利:)但我应该将其声明为字符串吗?@user1431627:我不明白。将其声明为char choice[]@user1431627:动态的,从某种意义上说,您可以通过运行时来控制它。要在*ptr上存储字符串,需要使用malloc分配内存,比如:
char*ptr=(char*)malloc(SIZE)
这里SIZE是一个变量,如果SIZE的值是80,则可以存储0个字符,如果是120,则可以存储120个字符。因此,在您当前的环境中。不需要更改。现在就把它忘了。当您掌握了指针、malloc和free概念时,请看一下这一点
scanf("%s", choice);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
   char choice[80];
   while(1)
   {
        scanf("%s", choice);
        if(strcmp (choice,"one") == 0 || strcmp (choice,"two") == 0)
        {
           break;
        }
        getchar();
   }
   // if the user enters either one or two, continue executing code...
   return 0;
}