我找不到第一个用户输入后立即退出程序的原因

i cant find the reason the program quits right after the first user input

本文关键字:退出程序 找不到 第一个 用户 输入      更新时间:2023-10-16
#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>>

查看此链接

我发现了这个程序的某个问题。

  1. 如果您想要多个单词输入 - 使用getline(cin,str) - CIN只将第一个单词作为输入。

  2. 一旦发出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");
}