如果是,否则代码只解析为else条件

Cam*_*ron 4 c++

我正在努力做一个非常基本的文本冒险来测试我的基本技能.基本运动提示用户输入,如果它匹配某些字符串,则更改其坐标.我知道,这很愚蠢.但是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.
Run Code Online (Sandbox Code Playgroud)

当我输入"go east"或"go north"时,它会打印关于匕首和驴子的提示字符串,只有在我输入其他内容时才会打印.我究竟做错了什么?为了澄清,我在OS X 10.10.3上使用Xcode.

Wer*_*kov 7

action从输入读取的值只能是"go",它在第一个空白字符上停止.见行为operator>>.


sep*_*p2k 6

cin >> string一次读一个字,而不是一行.因此,如果输入包含"go north",则第一个>>将显示为"go",第二个将显示为"north",两者都不等于"go north".

使用getline阅读一整行.