-1 c++ if-statement
好吧,所以我(尝试)做一个猜词游戏,同时也试图学习编码.我的问题是,程序跳过真正的if语句并执行false语句.这是一节:
#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 << "\n\nDifficulty does not yet exist!";
}
else
{
cout << "\n\nDifficulty does not exist";
}
/*These two execute, when I enter
"Very Easy" for some reason*/
}
}
Run Code Online (Sandbox Code Playgroud)
注意:我看看是否有人有同样的问题,但我没有找到任何人,虽然我发现了一个关于执行错误陈述的问题,这对我没有帮助.另外,我不知道这是否仅在c ++中发生,没有使用c或c#.
这种情况
cin >> Sel_Diff;
if (Sel_Diff == "Very Easy")
Run Code Online (Sandbox Code Playgroud)
永远不会触发,因为operator>>一次只读一个字.所以它停止了Very.
如果你想阅读更多内容,比如整行输入,你可以使用 getline(cin, Sel_Diff);
| 归档时间: |
|
| 查看次数: |
81 次 |
| 最近记录: |