Вопрос

I am trying to create a text based adventure game in c++ for my school project. The roblem i am having is that my gameover() function needs to be able to goto my begin() function. The problem is begin must be declared before before the gameover() function to allow it to go to begin(), only i have other functions that also need to access gameover(. In short i need a way to be able to tell my program to goto the function gameover() or begin(), and know that it exists and is declared. Thanks, Simon

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; }
 }
Это было полезно?

Решение 2

Add the header file containing the declaration of fucntion begin and gameover or add declaration by yourself by adding void begin(); and void gameover() before their first use.

Другие советы

C++ require you to describe the function before its calling statement. if you are going to add definition at top of main() then its call statement will work anywhere and second option is to declaration of function before calling. Its up to you that where you want that function to accessible.

Basically if you add some declaration in header file or in top of main then these functions will work anywhere

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

You should just add a function declaration like this in the top of the file (or above the gameover() function):

void begin();

The solution is to decouple the gameover and begin functions. Consider this:

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

This way, after gameover, the program control will exit the begin function and if continueGame is still true, the while loop will continue looping and begin will be called again. This way, you'll also be able to call the begin and gameover functions whenever you want, from the controllerFunction. It's just a matter of logic structuring. With this hint, you'll be able to come up with a logic that's smarter than what I posted.

Your code is unnecessarily recursive: begin() calling gameover() calling begin() calling...

It would be better to have an outer loop that calls the necessary functions, like

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();
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top