I guess what your teacher meant is this:
if (choice == 1) {
spider();
But there is a better way to write this. You adventure game is actually a Finite State Machine. You could implement it with a simple loop:
#include <iostream>
struct state;
struct transition;
struct transition {
char *text;
struct state *next_state;
};
struct state {
char *text;
struct transition transitions[8];
};
extern struct state start;
extern struct transition start_transitions[];
extern struct state spider;
extern struct transition spider_transitions[];
struct state start = {
.text = "You are on a road that heads west and east of your position.\n"
"Which way will you go?",
.transitions = {
{"Go West", &spider},
{"Go East", NULL},
{"Wait for something to happen", NULL},
{ NULL }
}
};
struct state spider = {
.text = "You travel down the road, about only 100 metres and you encounter\n"
"a giant spider with vicious poison coated fangs.\n"
"Its hideous appearance causes your throat to dry and your knees to shake!\n"
"What on earth will you do?",
.transitions = {
{ "Attempt to attack the spider with you sword.", NULL },
{ "Throw your sword in the off chance it might kill it.", NULL },
{ "RUN FOR YOUR LIFE!", NULL },
{ NULL }
}
};
int main(void)
{
state *cur = &start;
while (cur) {
// Print text
std::cout << cur->text << std::endl;
// List available choices
unsigned trans = 0;
while (cur->transitions[trans].text) {
std::cout << trans << ". " << cur->transitions[trans].text << std::endl;
trans += 1;
}
// Read input
unsigned choice;
std::cin >> choice;
std::cin.ignore();
// Check input is valid
if (choice < trans) {
// Move to new state
cur = cur->transitions[choice].next_state;
}
}
return 0;
}
Of course, a more mature version of the game would read the states and transitions from a data file rather than include them directly in the code.