Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.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++;卡在while循环中 我在C++中做了一个非常简单的while循环,我甚至不知道为什么在输入正确的时候我会陷入其中。 string itemType = ""; while(!(itemType == "b") || !(itemType == "m") || !(itemType == "d") || !(itemType == "t") || !(itemType == "c")){ cout<<"Enter the item type-b,m,d,t,c:"<<endl; cin>>itemType; cout<<itemType<<endl; } cout<<itemType; string itemType=”“; 而(!(itemType==“b”)| |!(itemType==“m”)| |!(itemType==“d”)| |!(itemType==“t”)| |!(itemType==“c”)){ cout_C++ - Fatal编程技术网

C++;卡在while循环中 我在C++中做了一个非常简单的while循环,我甚至不知道为什么在输入正确的时候我会陷入其中。 string itemType = ""; while(!(itemType == "b") || !(itemType == "m") || !(itemType == "d") || !(itemType == "t") || !(itemType == "c")){ cout<<"Enter the item type-b,m,d,t,c:"<<endl; cin>>itemType; cout<<itemType<<endl; } cout<<itemType; string itemType=”“; 而(!(itemType==“b”)| |!(itemType==“m”)| |!(itemType==“d”)| |!(itemType==“t”)| |!(itemType==“c”)){ cout

C++;卡在while循环中 我在C++中做了一个非常简单的while循环,我甚至不知道为什么在输入正确的时候我会陷入其中。 string itemType = ""; while(!(itemType == "b") || !(itemType == "m") || !(itemType == "d") || !(itemType == "t") || !(itemType == "c")){ cout<<"Enter the item type-b,m,d,t,c:"<<endl; cin>>itemType; cout<<itemType<<endl; } cout<<itemType; string itemType=”“; 而(!(itemType==“b”)| |!(itemType==“m”)| |!(itemType==“d”)| |!(itemType==“t”)| |!(itemType==“c”)){ cout,c++,C++,您的问题在于您的逻辑。如果您查看while循环的条件,如果项目类型不是“b”或“m”或“d”等,循环将重复。这意味着如果您的项目类型是“b”,则显然不是“m”,因此它将重复。您希望使用&&而不是| |。退出循环的布尔表达式有缺陷。按照这种方式,要退出循环,itemType必须同时为所有这些字母。请尝试先将字母替换为|,然后对其求反: while(!(itemType==“b”| itemType==“m”| itemType==“d”| itemType==“t”| itemType==“c”)

您的问题在于您的逻辑。如果您查看while循环的条件,如果项目类型不是“b”或“m”或“d”等,循环将重复。这意味着如果您的项目类型是“b”,则显然不是“m”,因此它将重复。您希望使用&&而不是| |。

退出循环的布尔表达式有缺陷。按照这种方式,要退出循环,itemType必须同时为所有这些字母。请尝试先将字母替换为
|
,然后对其求反:

while(!(itemType==“b”| itemType==“m”| itemType==“d”| itemType==“t”| itemType==“c”)

试试这个

字符串itemType=“”

while(!(itemType==“b”| | itemType==“m”| | itemType==“d”| | itemType==“t”| | itemType==“c”)){

cout由于其他答案和注释写得正确,您的逻辑是错误的。使用
find()
可以简化您的任务:

std::string validCharacters( "bmdtc" );
while ( std::string::npos == validCharacters.find( itemType  ) )
{
    ...
}

此解决方案更为通用且更易于阅读。另请参阅Post a please的文档。还请逐行提供调试代码时所做的所有观察。将
|
更改为
&&
。考虑逻辑。尝试使用
!=
而不是
!(x==y)
。您的条件是“while itemType至少与这些字母中的一个不同”。你能想到一些不一样的吗?或者
while(!strchr(“bmdtc”,itemType[0])
std::string validCharacters( "bmdtc" );
while ( std::string::npos == validCharacters.find( itemType  ) )
{
    ...
}