Hi there relatively new C++ Programmer and during the construction of a Text-Based adventure games I decided to handle movement by hard coding a bunch of commands inside of each "Hall" function.
Once the command is fulfilled it compares it to all of the strings at run time, then returns the desired "Hall" function(Hall1(), Hall2()..etc.)but once a function is actually returned the console shuts down...without error.
Now I do not want this to happen, I want it to continue(I guess you could say 'restart') the function and allow the player to input commands to help them progress further(If the function called is the same) or transfer into the next hall if such a function is called.
Everything works properly except for the console abruptly shutting down on me once a function is returned...
#include<iostream>
#include<string>
usingnamespace std;
int Start();
int Inventory();
int Hall1();
int Hall2();
int Hall3();
string Choice;
int (*foo)();
string HALL1STORY = "STORY1\n";
string HALL2STORY = "STORY2\n";
string HALL3STORY = "STORY3\n";
constint MAX_ITEMS = 10;
string inv[MAX_ITEMS];
int numItems = 0;
int main()
{
int choice;
foo = Hall1;
cout << "\t\t++++++++++++++++++Weclome--------------------\n";
cout << "To the great text adventure, this small adventure has you twisting and turning through the bleak corridors of the strange and seemingly ever changing metal building.";
cout<< "Apon a hazey walking you quickly notice that you have been kidnapped, the small cube like room taunts you as questions continue to fill your mind\n...Why was I taken, Who took me, Will I get out?\n\n";
cout << "List of Commands: \n" ;
cout << "MOVE -\n";
cout << "EAST\n";
cout << "WEST\n";
cout << "NORTH\n";
cout << "SOUTH\n\n";
cout <<"------------\n";
cout << "PICKUP [Item]\n";
cout << "ATTACK\n";
cout << "INVENTORY\n";
cout << "USE [Item]\n";
cout << "Use these words to navigate and interact with various parts of the story\n\n";
cout <<"What would you like to do\n";
cout<< "1) Play game\n";
cout << "2) Quit\n";
cin >> choice;
switch(choice)
{
case 1:
return Start();
break;
case 2:
cin.ignore(10, '\n');
cout << "PRESS ENTER TO QUIT";
cin.ignore(10,'\n');
cout << "GOODBYE";
return 0;
}
}
int Start()
{
string Choice;
cout << HALL1STORY;
getline(cin, Choice);
return Hall1();
}
//THE STARTING HALL FUNCTION
int Hall1()
{
char command[100];
char item[100];
cout << "How would you like to procced?:\n";
//MOVEMENT
while(Choice.compare("MOVE SOUTH") != 0 && Choice.compare("MOVE EAST") != 0 && Choice.compare("MOVE NORTH") != 0 && Choice.compare("MOVE WEST") != 0 && Choice.compare("PICKUP") != 0 && Choice.compare("ATTACK") != 0 && Choice.compare("USE") != 0 && Choice.compare("PICKUP") && Choice.compare("USE DETONATOR") != 0 && Choice.compare("USE C4") != 0 && Choice.compare("USE SERVICE ELEVATOR") != 0 && Choice.compare("USE SECURITY DOOR") != 0 && Choice.compare("USE SECURITY CONSOLE") != 0 && Choice.compare("USE SECURITY PASS") != 0 && Choice.compare("PICKUP") != 0 && Choice.compare("PICKUP SECURITY PASS") != 0 && Choice.compare("PICKUP C4") != 0 && Choice.compare("PICKUP DETONATOR") != 0 && Choice.compare("INVENTORY") != 0 && Choice.compare("1") != 0)
{
getline(cin,Choice);
if(Choice.compare("MOVE SOUTH") != 0 && Choice.compare("MOVE EAST") != 0 && Choice.compare("MOVE NORTH") != 0 && Choice.compare("MOVE WEST") != 0 && Choice.compare("PICKUP") != 0 && Choice.compare("ATTACK") != 0 && Choice.compare("USE") != 0 && Choice.compare("PICKUP") && Choice.compare("USE DETONATOR") != 0 && Choice.compare("USE C4") != 0 && Choice.compare("USE SERVICE ELEVATOR") != 0 && Choice.compare("USE SECURITY DOOR") != 0 && Choice.compare("USE SECURITY CONSOLE") != 0 && Choice.compare("USE SECURITY PASS") != 0 && Choice.compare("PICKUP") != 0 && Choice.compare("PICKUP SECURITY PASS") != 0 && Choice.compare("PICKUP C4") != 0 && Choice.compare("PICKUP DETONATOR") != 0 && Choice.compare("INVENTORY") != 0 && Choice.compare("1") != 0)
{
cout << "That is not a vaild command, please pick from the list : \n MOVE\nNORTH\nEAST\nSOUTH\nWEST\nPICKUP\nATTACK\nUSE\n";
}
if(Choice.compare("MOVE NORTH") == 0 )
{
cout << "A Solid steel wall blocks your path, you aren't going anywhere in that direction...\n";
Hall1();
}
if(Choice.compare("MOVE WEST") == 0)
{
cout << "A Solid steel wall blocks your path, you aren't going anywhere in that direction...\n";
Hall1();
}
if(Choice.compare("MOVE EAST") == 0)
{
cout << HALL2STORY;
return Hall2();
}
if(Choice.compare("MOVE SOUTH") == 0)
{
cout << HALL3STORY;
return Hall3();
}
//ITEMS
//USE
if(Choice.compare("USE") ==0 )
{
cout << "Pick an item or enviorment object to interact with.\n";
Hall1();
}
//PICK UP
if(Choice.compare("PICKUP") ==0)
{
cout << "Choose an item to pick up.\n";
Hall1();
}
//USE ITEMS
if(Choice.compare("USE DETONATOR") ==0 )
{
cout << "ITEM NOT AQUIRED\n";
Hall1();
}
if(Choice.compare("USE C4") ==0 )
{
cout << "ITEM NOT AQUIRED\n";
Hall1();
}
if(Choice.compare("USE SECURITY CARD") ==0 )
{
cout << "ITEM NOT AQUIRED\n";
Hall1();
}
//PICK UP ITEMS
if(Choice.compare("PICKUP DETONATOR") ==0 )
{
cout << "ITEM NOT FOUND\n";
Hall1();
}
if(Choice.compare("PICKUP C4") ==0 )
{
cout << "ITEM NOT FOUND\n";
Hall1();
}
if(Choice.compare("PICKUP SECURITY CARD") ==0 )
{
cout << "ITEM NOT FOUND\n";
Hall1();
}
//ENVIROMENT OBJECTS
if(Choice.compare("USE SECURITY CONSOLE") ==0 )
{
cout << "No console is around.\n";
Hall1();
}
if(Choice.compare("USE SECURITY DOOR") ==0 )
{
cout << "No Door is around.";
Hall1();
}
if(Choice.compare("USE SERIVCE ELEVATOR") ==0 )
{
cout << "No Elevators are around...";
Hall1();
}
if(Choice.compare("INVENTORY") ==0 )
{
return Inventory();
}
}
}
int Hall2()
{
// Hall2() Code(Identical template to Hall1());
}
int Hall3()
{
return 0;
}
int Inventory()
{
int choice;
cout << "Inventory:\n";
for(int i = 0; i < numItems; ++i)
{
cout << inv[i] << endl << "\n";
}
cout << "1) Return to Game";
cin >> choice;
switch(choice)
{
case 1:
return foo();
}
}
The only function that does not instantly close on me is my "Inventory" function(I added a function pointer to track the hallways so the Inventory function could return to the previous hall when closed) but once the previous hallway function is called the console shuts down...
You are using functions as if they were gotos. This is a bad idea.
Remember that when a function exits, program flow will continue at the point where the function was called:
1 2 3 4 5 6 7 8 9 10 11
void DoSomething()
{
// do something here
}
int main()
{
DoSomething(); // once DoSomething exits...
cout << "...code continues to execute here";
}
You should not have Hall1 call Hall2 which calls Hall1 which calls Hall2 etc etc. The computer has to remember each of those calls so it can return back once the function exits. So you suck up more and more stack space, and if it's long enough, you can run out and crash the program. Not to mention it makes for sloppy and hard to follow code.
A simpler approach to this problem is the "switch on current location" technique:
enum Location
{
Hall1,
Hall2,
Hall3,
RestartGame
ExitGame
};
Location DoHall1()
{
// do Hall1 stuff here
// return a "Location" based on what action you want to take.
// for example if the user moves to hall 2, you'd do:
return Hall2;
}
// make similar functions for Hall2, etc
void RunGame()
{
Location loc = RestartGame;
while(loc != ExitGame) // as long as the game is still going...
{
if(loc == RestartGame)
{
// restart the game here
}
switch(loc)
{
case Hall1: loc = DoHall1(); break;
case Hall2: loc = DoHall2(); break;
// etc
}
}
}
With this system, flow always returns immediately to RunGame, which calls the appropriate hall function.
There are of course, much better ways to do this, but in the interest of keeping this simple the above should work.