#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)
答案 0 :(得分:1)
代码的cin>>initial
部分只接受用户输入,直到第一个空格
因此,当您输入LOOK LEFT时,初始字符串只包含LOOK,这就是您的代码未输入IF语句的原因。
尝试使用getline(初始)而不是cin&gt;&gt;
查看此link
答案 1 :(得分:0)
我发现这个程序存在一些问题。
如果你想要多个单词输入 - 使用getline(cin,str) - cin只会把第一个单词作为输入。
一旦发出EOF(或任何其他错误)信号,所有输入数据的尝试都将失败。在尝试进一步输入之前,必须首先清除流(cin.clear();)。因此,对于每个cin,使用cin.clear()来刷新输入缓冲区。
现在在程序中,如果你没有使用clear(),并输入多个单词输入,则第一个单词将被输入到第一个字符串,该输入的第二个单词将被输入第二个“cin&gt;&gt; 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");
}