我找不到第一个用户输入后立即退出程序的原因
i cant find the reason the program quits right after the first user input
#include "stdafx.h"
#include <string>;
#include <iostream>;
#define newline 'n';
using namespace std;
string initial;
string a;
string b;
string c;
string object;
string sword;
string testswing;
int main()
{
cout << "you awake in a forest with no memory what do you do?";
cout << newline;
cin >> initial;
if (initial == "look left" || initial == "look forward" || initial == "look behind"){
cout << newline;
cout << "you see a dense forest";
cout << newline;
}
else (initial == "look right"); {
cout << newline;
cout << "you see a opening with a shape in the distance";
cout << newline;
}
cin >> a;
cout << newline;
if (a == "go right")
{
cout << newline;
cout << "you see a trap lucily you dident set it off. might want to SEARCH AREA before going into open areas";
cout << newline;
cin >> c;
cout << newline;
if (c == "go forward")
{
cout << newline;
cout << "you arrive at the strange object do you want to INSPECT OBJECT?";
cout << newline;
if (object == "inspect object")
{
cout << newline;
cout << "You see the object is a sword stuck in a stone";
cout << newline;
cin >> sword;
if (sword == "pull sword");
{
cout << newline;
cout << "the sword breaks free of the stone you have obtained the SHORT SWORD";
cout << newline;
cout << "use this item when appropriate by using the command SWING SWORD";
if (testswing == "swing sword");
{
cout << newline;
cout << "you swing the sword it hits the rock and breaks";
cout << newline;
cout << "we told you only to use the sword when appropriate now look what you've done your adventure is over already";
cout << newline;
system("pause");
return 0;
}
}
}
else
{
cout << newline;
cout << "the object draws you twards it seemingly by magic";
cout << newline;
}
}
else (c == "go left" || c == "go right"|| c == "go back");
{
cout << "there is nothing but empty feilds for miles you get lost and die";
cout << newline;
system("pause");
return 0;
}
}
else {
cout << "you cant go that way";
}
}
无论输入是否向左外观,请向后看,向后看或向右看,该程序在第一个用户输入之后就退出了,该程序在其之后就退出了,我找不到问题。它说该程序以代码0(0x0)
代码的cin>>initial
部分仅接入用户输入,直到第一个空空间
因此,当您输入时,当您的初始字符串包含外观时,这就是为什么您的代码未输入IF语句的原因。
尝试getline(初始)而不是cin>>
查看此链接
我发现了这个程序的某个问题。
如果您想要多个单词输入 - 使用getline(cin,str) - CIN只将第一个单词作为输入。
一旦发出EOF(或任何其他错误),所有输入数据的尝试都会失败。在尝试进一步输入之前,您必须首先清除流(cin.clear();)。因此,对于每个CIN,使用Cin.Clear()冲洗输入缓冲区。
现在在您的程序中,如果您不使用clear(),并且输入多个单词输入,那么第一个单词将输入第一个字符串,该输入的第二个单词将输入第二个" CIN>> A",因此将输入它不止于此,因为它获得了输入。
尝试以下操作: -
//#include "stdafx.h"
#include <string>;
#include <iostream>;
#define newline 'n';
using namespace std;
string initial;
string a;
string b;
string c;
string object;
string sword;
string testswing;
int main()
{
cout << "you awake in a forest with no memory what do you do?";
cout << newline;
getline(cin, initial);
if (initial == "look left" || initial == "look forward" || initial == "look behind"){
cout << newline;
cout << "you see a dense forest";
cout << newline;
}
else if(initial == "look right") {
cout << newline;
cout << "you see a opening with a shape in the distance";
cout << newline;
}
cin.clear();
getline(cin, a);
cout << newline;
if (a == "go right")
{
cout << newline;
cout << "you see a trap lucily you dident set it off. might want to SEARCH AREA before going into open areas";
cout << newline;
cin >> c;
cout << newline;
if (c == "go forward")
{
cout << newline;
cout << "you arrive at the strange object do you want to INSPECT OBJECT?";
cout << newline;
if (object == "inspect object")
{
cout << newline;
cout << "You see the object is a sword stuck in a stone";
cout << newline;
cin >> sword;
if (sword == "pull sword");
{
cout << newline;
cout << "the sword breaks free of the stone you have obtained the SHORT SWORD";
cout << newline;
cout << "use this item when appropriate by using the command SWING SWORD";
if (testswing == "swing sword");
{
cout << newline;
cout << "you swing the sword it hits the rock and breaks";
cout << newline;
cout << "we told you only to use the sword when appropriate now look what you've done your adventure is over already";
cout << newline;
system("pause");
return 0;
}
}
}
else
{
cout << newline;
cout << "the object draws you twards it seemingly by magic";
cout << newline;
}
}
else (c == "go left" || c == "go right"|| c == "go back");
{
cout << "there is nothing but empty feilds for miles you get lost and die";
cout << newline;
system("pause");
return 0;
}
}
else {
cout << "you cant go that way";
}
cout<<endl;
system("PAUSE");
}
相关文章:
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 将库链接到另一个应用程序时,CMAKE 找不到库
- 我在c ++矩阵计算器程序中找不到错误
- C ++程序编译错误,找不到/访问文件
- GTK C++:找不到信号处理程序 您是否使用 -rdynamic 进行了编译?
- Qt应用程序找不到第三方DLL并崩溃
- Qt视频流应用程序:找不到qt.mediaplayer的服务
- 程序找不到回车符或换行符。退货超出范围
- 我找不到第一个用户输入后立即退出程序的原因
- (Qt 5.6)切换到"Release"模式时程序找不到.txt文件
- 交叉编译用于ARM的MongoDB C++驱动程序.找不到Boost库
- SFML2 应用程序找不到共享对象
- LLVM JIT编译的程序找不到外部函数
- C++程序找不到提升
- 为什么应用程序找不到这个libSDL2_image-2.0.so.0库?-Ubuntu 14.04
- 服务加载程序找不到服务提供程序类,即使类与 META-INF/services 位于同一 JAR 文件中也是如此
- c++应用程序找不到com dll,因为编译器使用不正确的guid生成.tlh文件
- boost.log std::异常格式化程序找不到运算符<< 自己的命名空间中的重载
- SFML 程序找不到 libGLEW