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

C++ Starting up different files in if/else if

本文关键字:if 启动 文件 else C++      更新时间:2023-10-16
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;
};

那是我的简介.h文件

这是我的介绍.cpp文件。

 using namespace std;
 void Introduction::intro_Init () {
//Errors err;
cout << "****************************************" << endl;
cout << "*            Jay: Hey Kid?             *" << endl;
cout << "****************************************" << endl;
cout << "1. Yes? n2. Ignore" << endl;
string yesOrNo;
cin >> yesOrNo;
if (yesOrNo == "1") {
    //NEED TO INITIATE THE STARTGAME::GAME_INIT()
}
else if (yesOrNo == "2") {
   //NEED TO INITIATE ZERTH
//        Zerth zerthstart;
//        zerthstart.zerth_Init();
}
}

我想要它,所以如果用户输入 1,那么它会链接到"开始游戏.cpp"如果用户输入 2,则它链接到文件"Zerth.cpp"我已经尝试了一切,如果我运行为。如果我输入 1 或 2,它会自动将我链接到 StartGame.cpp。非常感谢。编辑我的简介 类介绍 { 公共: 简介(( { 重复 = 假; Items("/Users/ewokhugz/Documents/Apps/Space Adventure/Space Adventure/CargoItems.txt"); } ~简介(( {} 无效intro_Init((;

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

};

我的介绍.cpp

#include "Intro.h"
#include "StartGame.h"
#include "Zerth.h"
 using namespace std;
void Introduction::intro_Init () {

using std::cout;
using std::endl;
using std::string;
//Errors err;
cout << "****************************************" << endl;
cout << "*            Jay: Hey Kid?             *" << endl;
cout << "****************************************" << endl;
cout << "1. Yes? n2. Ignore" << endl;
string yesOrNo;
cin >> yesOrNo;
if (yesOrNo == "1") {
    //NEED TO INITIATE THE STARTGAME::GAME_INIT()
    StartGame gamestart;
    gamestart.game_Init();
}
else if (yesOrNo == "2") {
   //NEED TO INITIATE ZERTH
    Zerth zerthstart;
    zerthstart.zerth_Init();
}
}

泽特·

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;
};

泽斯.cpp

#include <iostream>
#include "Zerth.h"
#include "Intro.h"
using namespace std;
void Zerth::zerth_Init () {

cout <<"You are in Zerth" << endl;
repeat = true;
}

开始游戏.h

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;

};

开始游戏.cpp

#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 << "****************************************nn" << 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 << "****************************************nn" << 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;
}

忽略坏故事。但是,是的,这是代码。

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

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

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

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