程序跳过真if语句,但执行假if语句
Program skipping true if-statements, but executing false ones
好的,所以我(试着)做一个猜字游戏,同时也试着学习编码。我的问题是,程序跳过了真if语句,执行了假if语句。这里有一个section:
#include <iostream>
#include <stdlib.h>
/*Some more #include...*/
using namespace std;
int main()
{
int Lives = 5, Sel_Word, i;
string Sel_Diff;
/*Some more variables*/
while(Lives >= 1)
{
cout << "Difficulty...";
cin >> Sel_Diff;
if (Sel_Diff == "Very Easy")
{
/*Executes game for that level*/
}
/*Gets skipped, when I enter "Very Easy" for
some reason*/
if (Sel_Diff == "Exit")
{
break;
}
/*Works fine*/
if (Sel_Diff == "Easy" || Sel_Diff == "Medium")
/*Et cetera...*/
{
cout << "nnDifficulty does not yet exist!";
}
else
{
cout << "nnDifficulty does not exist";
}
/*These two execute, when I enter
"Very Easy" for some reason*/
}
}
指出:我看了看是否有人有同样的问题,但我没有找到任何人,尽管我发现了一个关于虚假陈述执行的问题,这对我没有帮助。此外,我不知道这是否只发生在c++中,我没有用过c或c#。
此条件
cin >> Sel_Diff;
if (Sel_Diff == "Very Easy")
将永远不会触发,因为operator>>
一次只读取一个单词。所以它在Very
后停止。
如果你想阅读更多内容,比如整行输入,你可以使用getline(cin, Sel_Diff);
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 我似乎对if/else的基本语句有问题:/
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果中的多个语句是否与多个 if 相同?
- C++ If/Else 语句被跳过
- 使用语句if时逻辑的问题
- 如何添加语句if-else
- 如何将语句if(isalpha(c))从c++转换为MIPS ?
- 在if语句if C++中创建对象时出现作用域问题
- if语句-if/else和参数放置C++