Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.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
在C中使用getche在结构中输入字符?_C - Fatal编程技术网

在C中使用getche在结构中输入字符?

在C中使用getche在结构中输入字符?,c,C,我在C中输入字符时遇到问题。我希望用户只输入x或x。否则,用户必须再次输入。此外,我想用一个struct来实现它。这是我的代码: typedef struct chu{ char c; }; int main(){ chu input; char temp; do{ printf("\nInput: "); temp=getche(); if((temp!='x')||(temp!='X'))

我在C中输入字符时遇到问题。我希望用户只输入
x
x
。否则,用户必须再次输入。此外,我想用一个struct来实现它。这是我的代码:

typedef struct chu{
    char c;
};

int main(){
    chu input;
    char temp;
    do{
        printf("\nInput: ");
        temp=getche();
        if((temp!='x')||(temp!='X')) 
            printf("\nWrong input (only 'x' or 'X')");
        else
            input.c=temp;
    }while((temp!='x')||(temp!='X'));
}

当我输入
x
x
时,我不应再次输入。

如果条件错误,则应为:

if((temp!='x') && (temp!='X')) 
               ^  && if not both 
while(!((temp =='x') || (temp=='X')));
      ^
我用
&&
替换了
|
(因为任何一个都可以接受,所以如果temp不等于两者,则应打印错误消息)

第二个while条件应该是:

if((temp!='x') && (temp!='X')) 
               ^  && if not both 
while(!((temp =='x') || (temp=='X')));
      ^
正如您所说,当我输入x或x时,我不应该再次输入,因此如果
temp
x
x
,那么
(temp=='x')|(temp='x')
=
1
,因为
它在(0)和循环中断时提供

此外,结构定义应类似于:

typedef struct {
    char c;
}chu;

我更正了你的代码,你可以在你的机器上找到并试用

问题在于您的状况:

} while((temp!='x')||(temp!='X'));
这意味着:在不相等的“x”或“x”时重复。(这总是正确的,因为它只能是一个或另一个。 将其替换为:

} while(temp != 'x' && temp != 'X');
在不是“x”或“x”时重复。 同样的问题也存在于你的
if
中(我第一次似乎忽略了这一点,我的错)

您的代码可以这样重建:

int main(){
    chu input;
    char temp;
    while(true) {
        printf("\nInput: ");
        temp=getche();
        if(temp != 'x' && temp != 'X') 
            printf("\nWrong input (only 'x' or 'X')");
        else {
            input.c=temp;
            break;
        }
    }
}
此代码将永远循环(如
while(true)
)所示),但只要输入“x”或“x”,它就会将变量设置为您的结构,并
中断
使其脱离循环

关于语义的一些信息:

只要第一个条件返回
true
,由或(
|
)连接的任何内容都将停止计算。此时,无论发生什么情况,整个构造都将返回
true

相反,与AND(
&&
)连接的任何内容都将在第一个条件返回
false
时停止计算,因为它会使整个构造
为false

回到您的示例,这是您的编译器所做的,假设我们输入“X”

if(temp != 'x') {      //Okay, it's not 'x'. Let's try the next one.
    if(temp != 'X') {  //Oh wait, it IS X. I can't stop yet!
        //Stop looping
    }                  //This will be executed
}
//Loop me!
这就是它对
&

if(temp == 'x') {      //Hmm...not 'x'.
    if(temp == 'X') {  //Oh wait! It IS 'X'.
        //Stop looping //This gets executed.
    }
}
//Loop me!

您可能已经注意到,我在这里将
!=
更改为
=
。我可以做到这一点,因为二进制算术。NOT(x或x)等于(NOT x和NOT x).

@GrijeshChauhan据我理解,他希望程序在输入x | | | x后停止,对吗?不管这个程序的设置方式如何,它都会无限期地循环,不管你输入什么。很抱歉延迟响应:可以找到这是错误的原因
(temp!=“x”&&temp!=“x”)
HINT尝试两次temp=
X
和temp=
a
@GrijeshChauhan,恐怕我听不懂。如果你把
X
放入
temp
中,等式返回false,循环中断,如果你把
a
放入,那么它就不会出现错误(并且你会打印错误消息)