如果语句不输出任何C++
我正在做一个简单的游戏,我对if语句有一个问题。我有正确的语句格式,但没有输出 我试图输出一个cout文本,然后要求用户输入另一个操作 我试图通过打印用于记录操作输入的字符串来隔离问题如果语句不输出任何C++,c++,C++,我正在做一个简单的游戏,我对if语句有一个问题。我有正确的语句格式,但没有输出 我试图输出一个cout文本,然后要求用户输入另一个操作 我试图通过打印用于记录操作输入的字符串来隔离问题 #include <iostream> #include <iomanip> #include <conio.h> #include <string> using namespace std; int main() { int charisma1 = 50
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string>
using namespace std;
int main() {
int charisma1 = 50;
string action, choice;
cout << "Hello There!\n";
cout << "\nBefore we start you need to learn the basics commands of theis game\n";
cout << "\nGo North -- To Move North\n";
cout << "Go South -- To Move South\n";
cout << "Go West -- To Move West\n";
cout << "Go East-- To Move East\n";
cout << "\nIF YOU WANT TO SKIP THE TUTORIAL THEN TYPE \"Yes\": ";
cin >> choice;
if (choice != "yes") {
cout << "\nAttack -- To Attack your enemy (You will have to however have a weapon equiped)\n";
}
cout << "\nEast of House\n";
cout << "\nYou are standing outside in a clearing west of a east house with a boarded front door\n";
_getch();
cout << "You see a mailbox there\n";
cin >> action;
if (action == "open mailbox"){
cout << "You see a letter inside\n";
}
return 0;
}
你看到一个邮箱了吗
输入:打开邮箱
你看到里面有一封信吗
输入:读字母
当您使用
cin >> action;
if (action == "open mailbox"){
cout << "You see a letter inside\n";
}
然而,这将导致另一个问题。上次从cin读取的调用
将在流中保留换行符。在使用std::getline之前,请确保已读取并丢弃换行符以及换行符之前的任何内容。你可以用它
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(cin, action);
加
在C++中,可以使用STD::NoimiCix.E.P/>P>,CIN是空白分隔的。这意味着一旦函数遇到空格、制表符、换行符等,它将停止读取中的字符。要读取整行,即按Enter键划界,请改用getline函数并将目标字符串传递给它。
是否尝试使用调试器单步执行代码?请注意,include and _getch;不会编译大多数C++编译器。为了便于携带,你应该扔掉那些东西。你要求输入Yes,但你检查Yes。尝试将答案转换为小写,然后检查它。。。。通过尝试打印出我用来记录操作输入的字符串,您在哪里这样做?我在你发布的代码中找不到它。你得到了什么作为输出?也是为了你的输入检查cin >> choice;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(cin, action);
#include <limits>