C++;我无法访问所需的功能 我试图在C++中为我的学校项目创建一个基于文本的冒险游戏。我遇到的问题是gameover()函数需要能够转到begin()函数。问题是必须在gameover()函数之前声明begin,才能让它转到begin(),只有我有其他函数也需要访问gameover(.简言之,我需要一种方法来告诉我的程序转到gameover()或begin()函数,并知道它存在并已声明。 谢谢,西蒙 void begin() { int name; int choice1; system("cls"); cout << "To start your adventure, please enter your name." << endl; cin >> name; cin.ignore(); system("cls"); cout << "Hello " << name << " Your adventure now begins.... Who knows what's in store for you!" << endl; system("pause"); system("cls"); cout << "You find yourself in a dark, cramp library. " << endl; cout << "You don't know how you got here, or where you are." << endl; cout << "Luckily there is a sword laying on the ground next to you \nand an open door in front.\n" << endl; cout << "What will you do?" << endl; cout << "1. Pick up the sword" << endl; cout << "2. Walk forward through the door" << endl; cout << "3. Use the sword to end your miserable existence!" << endl; cin >> choice1; cin.ignore(); system("cls"); if (choice1 == 1) { cout << "You quickly pick up the sword and run through the door." << endl; system("pause"); road(); } else if (choice1 == 2) { cout << "While you make you way to the door...." << endl; cout << "You somehow managed to trip on the sword." << endl; cout << "You fall on the hilt smashing your neck, and end your painfully short life. " << endl; system("pause"); gameover(); } else { cout << "That wasn't an option....." << endl; cout << "You have now broken the game. Good day Sir!!!" << endl; } } void gameover() { int choice_b; cout << " Oops! You died.... Try Again." << endl; cout << "\n1. Start Again!" << endl; cout << "2. Exit" << endl; cin >> choice_b; cin.ignore(); system("cls"); if (choice_b == 1) { begin(); } else if (choice_b == 2) { std::exit; } } void begin(){ int名称; 综合选择1; 系统(“cls”); 姓名; cin.ignore(); 系统(“cls”); cout

C++;我无法访问所需的功能 我试图在C++中为我的学校项目创建一个基于文本的冒险游戏。我遇到的问题是gameover()函数需要能够转到begin()函数。问题是必须在gameover()函数之前声明begin,才能让它转到begin(),只有我有其他函数也需要访问gameover(.简言之,我需要一种方法来告诉我的程序转到gameover()或begin()函数,并知道它存在并已声明。 谢谢,西蒙 void begin() { int name; int choice1; system("cls"); cout << "To start your adventure, please enter your name." << endl; cin >> name; cin.ignore(); system("cls"); cout << "Hello " << name << " Your adventure now begins.... Who knows what's in store for you!" << endl; system("pause"); system("cls"); cout << "You find yourself in a dark, cramp library. " << endl; cout << "You don't know how you got here, or where you are." << endl; cout << "Luckily there is a sword laying on the ground next to you \nand an open door in front.\n" << endl; cout << "What will you do?" << endl; cout << "1. Pick up the sword" << endl; cout << "2. Walk forward through the door" << endl; cout << "3. Use the sword to end your miserable existence!" << endl; cin >> choice1; cin.ignore(); system("cls"); if (choice1 == 1) { cout << "You quickly pick up the sword and run through the door." << endl; system("pause"); road(); } else if (choice1 == 2) { cout << "While you make you way to the door...." << endl; cout << "You somehow managed to trip on the sword." << endl; cout << "You fall on the hilt smashing your neck, and end your painfully short life. " << endl; system("pause"); gameover(); } else { cout << "That wasn't an option....." << endl; cout << "You have now broken the game. Good day Sir!!!" << endl; } } void gameover() { int choice_b; cout << " Oops! You died.... Try Again." << endl; cout << "\n1. Start Again!" << endl; cout << "2. Exit" << endl; cin >> choice_b; cin.ignore(); system("cls"); if (choice_b == 1) { begin(); } else if (choice_b == 2) { std::exit; } } void begin(){ int名称; 综合选择1; 系统(“cls”); 姓名; cin.ignore(); 系统(“cls”); cout,c++,visual-studio-2010,cmd,text-based,C++,Visual Studio 2010,Cmd,Text Based,您只需在文件顶部(或gameover()函数上方)添加如下函数声明: 添加包含功能声明的头文件begin和gameover,或通过添加void begin();和void gameover()自行添加声明在第一次使用之前。C++要求您在函数调用语句之前描述函数。如果您要在main()的顶部添加定义,那么它的调用语句将在任何地方工作,第二个选项是在调用之前声明函数。您希望在何处访问该函数取决于您 基本上,如果您在头文件或main的顶部添加一些声明,那么这些函数将在任何地方工作 #include h

您只需在文件顶部(或gameover()函数上方)添加如下函数声明:


添加包含功能声明的头文件
begin
gameover
,或通过添加
void begin();
void gameover()自行添加声明
在第一次使用之前。

C++要求您在函数调用语句之前描述函数。如果您要在main()的顶部添加定义,那么它的调用语句将在任何地方工作,第二个选项是在调用之前声明函数。您希望在何处访问该函数取决于您

基本上,如果您在头文件或main的顶部添加一些声明,那么这些函数将在任何地方工作

#include headerfiles....
void begin(); //
void end(); // Function prototypes 
int main()
{
   .....
    begin(); // Will work here
}

解决方案是去掉<代码> GAMOVER 和<代码>开始<代码>函数。考虑如下:

bool continueGame = true;

void gameover()
{
 //your gameover code
 if (userChoosesToExit) continueGame = false;
}


void begin()
{
 //your begin code
 if (playerDies) gameover();
}

void controllerFunction()
{
 while (continueGame) 
 {
  begin();
 }//while

}//controller

这样,在
gameover
之后,程序控制将退出
begin
函数,如果
continueGame
仍然为true,while循环将继续循环,并再次调用
begin
。这样,您还可以随时调用
begin
gameover
函数,从
controllerFunction
。这只是一个逻辑结构的问题。有了这个提示,你就能想出一个比我发布的更聪明的逻辑。

你的代码是不必要的递归:
begin()
调用
gameover()
调用
begin()
调用

最好有一个调用必要函数的外部循环,如

int gameover()
{   // ...
    // if (choice_b == 1) {
    // begin();
    // }
    // else if (choice_b == 2) { std::exit; }
    return choice_b;
}

int i = 0;
while (i != 2)
{   begin();
 // ...
    i = gameover();
}

谢谢你这么快的回复,工作就像一个魅力!)直截了当地说,你需要学习C++。如果其中一个答案帮助你,请接受它。
int gameover()
{   // ...
    // if (choice_b == 1) {
    // begin();
    // }
    // else if (choice_b == 2) { std::exit; }
    return choice_b;
}

int i = 0;
while (i != 2)
{   begin();
 // ...
    i = gameover();
}