gpt4 book ai didi

c++ - 我找不到程序在第一次用户输入后立即退出的原因

转载 作者:行者123 更新时间:2023-11-28 05:24:14 24 4
gpt4 key购买 nike

#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 部分只接受用户输入,直到第一个空白处

因此,当您输入 LOOK LEFT 时,初始字符串仅包含 LOOK,这就是您的代码未输入 IF 语句的原因。

尝试使用 getline(initial) 而不是 cin>>

看看这个link

关于c++ - 我找不到程序在第一次用户输入后立即退出的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40879308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com