C++ 在 if/else if 中启动不同的文件
C++ Starting up different files in if/else if
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 中的生成命令(,这将生成可以运行的二进制可执行文件。此时,您的所有文件都将被解析并编译为实际执行的代码。
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 如何将enable-if与模板参数和参数包一起使用
- 如何创建一个空的全局类并在启动时实例化它
- 无论条件是否为true,if总是在c++中执行
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- if数组上的随机数
- 即使我读取了所有内容,在FIFO上打开的QSocketNotifier也会一直启动
- 将按位if条件转换为普通if条件
- If语句在c++中被忽略
- C++如何重新启动if语句
- 如何启动具有许多布尔参数的函数模板,而不使用 2^n if 语句
- C++ 在 if/else if 中启动不同的文件