C++ C++;在if/else if中启动不同的文件

C++ C++;在if/else if中启动不同的文件,c++,C++,这是我的简介.h文件 这是我的Introduction.cpp文件 class Introduction { public: Introduction() { repeat = false; // Items("/Users/ewokhugz/Documents/Apps/Space Adventure/Space Adventure/CargoItems.txt"); } ~Introduction() {} void intro_Init(); private: std::

这是我的简介.h文件

这是我的Introduction.cpp文件

class Introduction
{
public:
Introduction() 
{
    repeat = false;
  //  Items("/Users/ewokhugz/Documents/Apps/Space Adventure/Space Adventure/CargoItems.txt");
}
~Introduction() {}
void intro_Init();

private:
std::string anyInput();
void invalid();
bool repeat;
};
})

我的简介

private:
std::string anyInput();
void invalid();
bool repeat;
Zerth.cpp

class Zerth
{
public:
Zerth() 
{
    repeat = false;
    //  Items("/Users/ewokhugz/Documents/Apps/Space Adventure/Space Adventure/CargoItems.txt");
}
~Zerth() {}
void zerth_Init();

private:
std::string anyInput();
void invalid();
bool repeat;
};
})

StartGame.cpp

class StartGame
{
public:
StartGame() 
{
    repeat = false;
   // Items("/Users/ewokhugz/Documents/Apps/Space Adventure/Space   Adventure/CargoItems.txt");
}
~StartGame() {}
void game_Init();

private:
std::string anyInput();
void invalid();
bool repeat;
#包括“StartGame.h”
#包括“简介h”
使用名称空间std;
void startName::game_Init(){

cout您的文件不会在运行时执行:您的函数会执行。您的各种文件只是用来组织您的游戏将具有的各种逻辑

要根据不同的用户输入执行不同的操作,请为要执行的每个操作创建单独的函数,然后为相应的用户选择调用正确的函数

在这种情况下,您可能需要一个InitGame()函数和一个StartZerth()函数来处理用户的是/否选择


编写完代码后,将运行编译器和链接器(可能通过IDE中的build命令)这将生成一个可运行的二进制可执行文件。此时,您的所有文件都将被解析并编译为实际执行的代码。

您的文件不会在运行时执行:您的函数会执行。您的各种文件仅用于组织您的游戏将具有的各种逻辑

要根据不同的用户输入执行不同的操作,请为要执行的每个操作创建单独的函数,然后为相应的用户选择调用正确的函数

在这种情况下,您可能需要一个InitGame()函数和一个StartZerth()函数来处理用户的是/否选择


编写完代码后,将运行编译器和链接器(可能通过IDE中的build命令)这将生成一个可运行的二进制可执行文件。此时,您的所有文件都将被解析并编译为实际执行的代码。

您没有正确地考虑它。请尝试从“调用函数”而不是“链接文件”的角度来考虑.所以我会将StartName和Zerth设置为函数?这似乎是一个很有希望的方向,是的。你没有正确地考虑它。试着从“调用函数”而不是“链接文件”的角度来考虑。所以我会将StartName和Zerth设置为函数?这似乎是一个很有希望的方向,是的。~Zerth(){}void Zerth_Init();像那样?我在Zerth.h中创建了它。然后在Zerth.cpp中,我取消了Zerth::Zerth_Init(){coutYes,类似的。这样想:如果两个用户玩你的游戏,用户A可能会选择
yes
,用户B可能会选择
no
。因此,对于一个用户,一部分代码将执行,而对于另一个用户,另一部分代码将运行。这两部分代码都需要编译到你的程序中,以便两部分都可以运行t根据用户的选择,任何时候都可以。这正是我要做的。现在如果用户点击了一个,那么我会尝试执行StartGame StartGame;StartGame.game_Init();(我已经在该文件中声明了该函数,并包括了该函数),否则如果用户输入了两个,则Zerth zerthstart;zerthstart.Zerth_Init().但不管它将转到StartGame.cpp,而是运行StartGame函数~Zerth(){}void Zerth_Init();像这样?我在Zerth.h中创建了它。然后在Zerth.cpp中我void Zerth::Zerth_Init(){coutYes,类似的。这样想:如果两个用户玩你的游戏,用户A可能会选择
yes
,用户B可能会选择
no
。因此,对于一个用户,一部分代码将执行,而对于另一个用户,另一部分代码将运行。这两部分代码都需要编译到你的程序中,以便两部分都可以运行t根据用户的选择,任何时候都可以。这正是我要做的。现在如果用户点击了一个,那么我会尝试执行StartGame StartGame;StartGame.game_Init();(我已经在该文件中声明了该函数,并包括了该函数),否则如果用户输入了两个,则Zerth zerthstart;zerthstart.Zerth_Init();但是不管发生什么,它都会转到StartGame.cpp而不是运行StartGame函数
class Zerth
{
public:
Zerth() 
{
    repeat = false;
    //  Items("/Users/ewokhugz/Documents/Apps/Space Adventure/Space Adventure/CargoItems.txt");
}
~Zerth() {}
void zerth_Init();

private:
std::string anyInput();
void invalid();
bool repeat;
};
#include <iostream>
#include "Zerth.h"
#include "Intro.h"
using namespace std;
void Zerth::zerth_Init () {


cout <<"You are in Zerth" << endl;
repeat = true;
}
class StartGame
{
public:
StartGame() 
{
    repeat = false;
   // Items("/Users/ewokhugz/Documents/Apps/Space Adventure/Space   Adventure/CargoItems.txt");
}
~StartGame() {}
void game_Init();

private:
std::string anyInput();
void invalid();
bool repeat;
#include "StartGame.h"
#include "Intro.h"
using namespace std;
void StartGame::game_Init() {
cout << "* Well you've come to the right place. *" << endl;
cout << "* I have a cargo filled with that needs*" << endl;
cout << "* to be shipped.                       *" << endl;
cout << "* Keep in mind that this isn't exactly *" << endl;
cout << "* ... legal bussiness. So keep it quiet*" << endl;
cout << "*   *" << endl;
cout << "* to tell you exactly how I found you. *" << endl;
cout << "* Simply put, I found you under a pile *" << endl;
cout << "* of debris. You were unconcious for   *" << endl;
cout << "* seven or eight weeks after I found   *" << endl;
cout << "* you. Oh! How rude of me, I never     *" << endl;
cout << "* introduced myself. My name is Samuel.*" << endl;
cout << "* This, is my laboratory. I assume that*" << endl;
cout << "* you may have been an assistant of the*" << endl;
cout << "* previous professors? I'll have to    *" << endl;
cout << "* inquire about the accident later. I  *" << endl;
cout << "* didn't quite catch your name, by the *" << endl;
cout << "* way. Also, just to clear some things *" << endl;
cout << "* up... I never quite caught your sex, *" << endl;
cout << "* considering your face is caked in a  *" << endl;
cout << "* layer of dust. No offense, I'd rather*" << endl;
cout << "* know however, if you'd be so kind.   *" << endl;
cout << "****************************************\n\n" << endl;

std::string gender;
repeat = true;
while (repeat == true)
{
    cout << "Gender: " << endl;
    cout << "1. Boy. " << endl;
    cout << "2. Girl. " << endl;

    gender = anyInput();

    if (gender == "1")
    {
        gender = "BOY";
        repeat = false;
        break;
    }
    if (gender == "2" or "girl")
    {
        gender = "GIRL";
        repeat = false;
        break;
    }
    if (gender == "?")
    {
        gender = "HERM";
        repeat = false;
        break;
    }
    if (gender != "?" && gender != "2" && gender != "1")
    {
        invalid();
    }
}

cout << "****************************************" << endl;
cout << "* So you're a " << gender << "!" << endl;
cout << "* So, what was your name?              *" << endl;
cout << "****************************************\n\n" << endl;
std::string name;

cout << "What is your name?" << endl;
name = anyInput();

cout << "****************************************" << endl;
cout << "* Ahh! So your name is " << name << "!" << endl;
cout << "****************************************\n" << endl;

cout << "****************************************" << endl;
cout << "* Let me explain what happened in the  *" << endl;
cout << "* lab while you were out. First and    *" << endl;
cout << "* foremost, I have taken over. I am    *" << endl;
cout << "* your new boss. I'd like for you to   *" << endl;
cout << "* know that the task I have for you is *" << endl;
cout << "* completely unrelated to the reason   *" << endl;
cout << "* that you signed up for your current  *" << endl;
cout << "* position. In the accident, a rare,   *" << endl;
cout << "* unstable compound combined with an   *" << endl;
cout << "* unknown element. In the process of   *" << endl;
cout << "* destroying this entire laboratory,   *" << endl;
cout << "* it has done some interesting things. *" << endl;
cout << "* Come see me tomorrow and I will      *" << endl;
cout << "* explain some more to you.            *" << endl;
cout << "****************************************" << endl;
repeat = false;
//    Player P(name, gender);
//    Pallet_Town pallet;
//    pallet.buildings(true);
}


std::string StartGame::anyInput()
{
using std::string;
using std::cin;

string input;
cin >> input;
return input;

}

void StartGame::invalid()
{
using std::cout;
using std::endl;
cout << "****************************************" << endl;
cout << "*You did not input an acceptable value.*" << endl;
cout << "****************************************" << endl;
repeat = true;
}