我试图做一个非常基本的文字冒险来测试我的基本技能。基本运动提示用户输入,如果它匹配某些字符串,则更改其坐标。我知道,这很愚蠢。但是if
,else if
,else
用于匹配其响应始终返回else
,即使您输入其中一个匹配字符串也是如此。
string action;
string west = "go west";
string east = "go east";
string north = "go north";
string south = "go south";
string prompt = "Don't just stand around with your dagger in your ass! Do something! ";
//i wrote a bunch of setup story here, it's irrelevant text output
int vertical = 25;
int horizon = 20;
//action begins
start:
{
cout << "What do you do?" << endl;
cin >> action;
if (action == south)
{
vertical = vertical - 5;
goto descriptions;
}
else if (action == east)
{
horizon = horizon + 5;
goto descriptions;
}
else if (action == west)
{
horizon = horizon - 5;
goto descriptions;
}
else if (action == north)
{
vertical = vertical + 5;
goto descriptions;
}
else
{
cout << prompt << "(Try typing \"go\" followed by a direction)" << endl;
goto start;
}
description:
//i put a bunch of if, else if, else stuff about coordinates describing the area down here.
当我输入&#34;去东部&#34;或者&#34;去北方#34;,它打印关于匕首和驴子的提示字符串,只有在我输入其他内容时才会打印。 我究竟做错了什么?为了澄清,我在OS X 10.10.3上使用Xcode。
答案 0 :(得分:7)
从输入中读取的action
的值只能是"go"
,它会在第一个空白字符上停止。请参阅operator>>
的{{3}}。
答案 1 :(得分:6)
cin >> string
一次读一个字,而不是一行。因此,如果输入包含&#34; go north&#34;,则第一个>>
将读取&#34; go&#34;而第二个&#34; north&#34;,两者都不等于&#34;去北方#34;。
使用getline
阅读整行。