Text adventure with functions

I have an assignment for my class. we are to make a choose your own adventure game. I don't want anyone to help with coding, I can handle that. My question is more understanding how to implement it.
The directions are to use a function for each "page" in the adventure.

I am going to go with if statements, so would that mean that each if statement would call a function??

such as
if (this)
function a
else (that)
function b

I can most certainly handle that.
My problem is where would I put all of these functions to be called?? Would they go above int main ()?
That seems like a whole lot of code since I have to code the actual text output as well into each "page" function.

Then pretty much my int main will be the original function and that's it, since it would flow through each if statement till the end correct??

After coding just a little, I have noticed that the functions put above my main have to be in reverse order otherwise I get errors of "undeclared first use". Does that seem right?

other than that I think I've got a good idea, just have to keep it all together!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>

using namespace std;

int doorChoice = 0;
int figureChoice = 0;

int leftDoor () {
    cout << "\n\nThe handle turns slightly and instantly you are pushed inside.\n";
    cout << "The door slams behind you, and you notice a small light from the ceiling.\n";
    cout << "As your eyes adjust to the darkness again, you notice a small figure.\n";
    cout << "The figure approaches and reaches toward you.\n\n";
    cout << "You notice a small passage behind the figure, just big enough for you.\n\n";
    cout << "Go to page 4 to push the figure and dive for the passage.\n";
    cout << "Go to page 5 to stay and see what the figure wants.\n\n";
    cout << "Which page would you like?\n";
    cin >> figureChoice;
    if (figureChoice == 4) {
       cout << "Thanks!\n";  }
    else {
       cout << "Yoo-hoo!\n"; }  
}

int rightDoor () {
    cout << "you chose the right door!\n";  }

int pickDoor () {
    cout << "Which page would you like?\n";
    cin >> doorChoice;
    if ( doorChoice == 2) {
       leftDoor(); }
    else {
       rightDoor (); }    
}

           

    


int main () {
    cout << "========================The Journey========================\n\n\n";
    cout << "You strain your eyes against the flood of light.\n";
    cout << "Your legs are weak and your memory hazy.\n\n";
    cout << "'How did I get here?' you say.\n";
    cout << "You notice a small key hanging form the center of the room.\n";
    cout << "Attached is a note...it reads:\n\n";
    cout << "  Greetings Traveler!  Your Journey Begins Here. Choose Left Or Right!\n\n";
    cout << "The room has two doors, one labeled 'Left' and one 'Right'.\n";
    cout << "Which door would you like to go through?\n\n";
    cout << "\nPick Page 2 for the left door.\n";
    cout << "Pick Page 3 for the right door.\n\n";
    pickDoor();
    cout << "Goodbye!\n";
    cout << "test\n";

return 0;
}    
    
            
       
A function only has to be declared before it is called. As long as you can see the protoype of a function before it's called, the actual definitions of the function can be anywhere, even in a different file. So, if at the top of the file you have function prototypes, you'll be ok:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int leftDoor ();
int rightDoor ();
int pickDoor ();

int main()
{
    /* function definition here */
    pickDoor();
}

int leftDoor()
{
    /* function definition here */
}

int pickDoor()
{
    /* function definition here */
    leftDoor();
    ...
    rightDoor();
}

int rightDoor()
{
    /* function definition here */
}


Note: I mixed up the order of the function definitions to make a point. You can put the definitions in any order you want to.

When one function calls another, it needs to know what parameters to pass and what return type to expect. That's why you need a function prototype. A full function definition would work, too, but a prototype is sufficient.

You could also put the function prototypes into a header file and include the header file. You will need to do that when your code starts getting really big and you need to organize functions into separate files. But you'll get to that eventually.
This looks like to be a state machine. Each page is a state of the machine. When the player makes a decision, it transfers to a different state.

I am now getting an error "linker error undefined reference to openBox () and stepBalcony()"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>

using namespace std;

int doorChoice = 0;
int figureChoice = 0;
int gravityChoice = 0;
int boxChoice = 0;
int leftDoor ();
int rightDoor ();
int pickDoor ();  
int divePast ();
int stayStill ();
int spaceSwitch ();
int stepBalcony ();
int openBox ();    


int main () {   // Main Function //
    cout << "========================The Journey========================\n\n\n";
    cout << "You strain your eyes against the flood of light.\n";
    cout << "Your legs are weak and your memory hazy.\n\n";
    cout << "'How did I get here?' you say.\n";
    cout << "You notice a small key hanging form the center of the room.\n";
    cout << "Attached is a note...it reads:\n\n";
    cout << "  Greetings Traveler!  Your Journey Begins Here. Choose Left Or Right!\n\n";
    cout << "The room has two doors, one labeled 'Left' and one 'Right'.\n";
    cout << "Which door would you like to go through?\n\n";
    cout << "\nPick Page 2 for the left door.\n";
    cout << "Pick Page 3 for the right door.\n\n";
    pickDoor();
    cout << "The End!\n";
    cout << "test\n";

return 0;
}    
// Start page functions //

int pickDoor() {
    cout << "Which page would you like?\n";
    cin >> doorChoice;
    if ( doorChoice == 2) {
       leftDoor(); }
    else {
       rightDoor(); }    
}

int leftDoor() {
    cout << "\n\nThe handle turns slightly and instantly you are pushed inside.\n";
    cout << "The door slams behind you, and you notice a small light from the ceiling.\n";
    cout << "As your eyes adjust to the darkness again, you notice a small figure.\n";
    cout << "The figure approaches and reaches toward you.\n\n";
    cout << "You notice a small passage behind the figure, just big enough for you.\n\n";
    cout << "Go to page 4 to push the figure and dive for the passage.\n";
    cout << "Go to page 5 to stay and see what the figure wants.\n\n";
    cout << "Which page would you like?\n";
    cin >> figureChoice;
    if (figureChoice == 4) {
       divePast();  }
    else {
       cout << "Yoo-hoo!\n"; } 
}                 

int rightDoor() {
    cout << "As you approach the door you hear a faint buzzing sound.\n";
    cout << "The handle turns and the door creaks open slowly.\n";
    cout << "You step in and the door slams forcefully behind you.\n";
    cout << "Inside is a small box pulsing with electricity.\n";
    cout << "You spot a key inside the box to unlock the door behind you.\n";
    cout << "There is also a small balcony to step out onto.\n";
    cout << "Go to page 8 to reach for the key.\n";
    cout << "Go to page 9 to step onto the balcony.\n";
    cout << "Which page would you like?\n";
    cin >> boxChoice;
    if (boxChoice == 8) {
       openBox(); }
    else if (boxChoice == 9) {
       stepBalcony(); }                
}

int divePast() {
    cout << "You shove the figure to the floor, and dive for the passage!\n";
    cout << "Your body slams to the ground and you slide through to a well lit room.\n";
    cout << "You hear a shriek and the passage crumbles behind you.\n";
    cout << "Immediately you try to stand and notice that you are being forced to the ground.\n";
    cout << "The gravity in this room is 3 times what it should be.\n";
    cout << "You notice a hole in the ceiling and in the floor.\n";
    cout << "You can see a room through the ceiling as if you were looking down on it.\n";
    cout << "There is a switch on the wall across the room but you must stand to push it\n";
    cout << "Do you slide to the hole in the floor and fall through?\n";
    cout << "Or do you struggle for the switch?\n\n";
    cout << "Go to page 6 to fall through the floor.\n";
    cout << "Go to page 7 to try for the switch.\n\n";
    cout << "Which page would you like?\n";
    cin >> gravityChoice;
    if (gravityChoice == 7) {
      spaceSwitch(); }
    else if (gravityChoice == 6) {
      cout << "Work on it!";   }  
}    
    
int spaceSwitch () {
    cout << "You slowly slide over to the switch and struggle to reach up.\n";
    cout << "With all your strength you manage to just barely flip the switch.\n";
    cout << "Immediately a huge suction forces you to the hole in the ceiling!\n";
    cout << "You smash through the fake room projection and are ejected into space.\n";
    cout << "Within seconds you are forzen solid and only able to reach for the room.\n";
    cout << "Your frozen body is forever suspended, drifting away into space.\n";
}           
       
 
Nevermind, I figured it out. I hadn't actually written those functions yet!
here is my finished code. Didn't get any errors, and works as expected.
Any suggestions are welcome (I know very basic programming, nothing advanced)
Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>

using namespace std;

int page = 0;     //Page Choices
int choice = 0;

int leftDoor ();         //Page function decleration
int rightDoor ();
int pickDoor ();  
int divePast ();
int stayStill ();
int spaceSwitch ();
int stepBalcony ();
int openBox ();    
int theRoom ();
int wakeUp ();
int floorFall ();
int originalRoom ();

int main () {   // Main Function //
    cout << "========================The Journey========================\n\n\n";
    cout << "You strain your eyes against the flood of light.\n";
    cout << "Your legs are weak and your memory hazy.\n\n";
    cout << "'How did I get here?' you say.\n";
    cout << "You notice a small key hanging form the center of the room.\n";
    cout << "Attached is a note...it reads:\n\n";
    cout << "  Greetings Traveler!  Your Journey Begins Here. Choose Left Or Right!\n\n";
    cout << "The room has two doors, one labeled 'Left' and one 'Right'.\n";
    cout << "Which door would you like to go through?\n\n";
    cout << "\nPick Page 2 for the left door.\n";
    cout << "Pick Page 3 for the right door.\n\n";
    pickDoor();
    cout << "\n\n========================The End!=============================\n";
    cout << "test\n"; 

return 0;
}    
// Start page functions //

int pickDoor() {
    cout << "Which page would you like?\n";
    cin >> page;
    if ( page == 2) {
       leftDoor(); }
    else if (page == 3) {
       rightDoor(); }    
}

int leftDoor() {
    cout << "\n\nThe handle turns slightly and instantly you are pushed inside.\n";
    cout << "The door slams behind you, and you notice a small light from the ceiling.\n";
    cout << "As your eyes adjust to the darkness again, you notice a small figure.\n";
    cout << "The figure approaches and reaches toward you.\n\n";
    cout << "You notice a small passage behind the figure, just big enough for you.\n\n";
    cout << "Go to page 4 to push the figure and dive for the passage.\n";
    cout << "Go to page 5 to stay and see what the figure wants.\n\n";
    cout << "Which page would you like?\n";
    cin >> page;
    if (page == 4) {
       divePast();  }
    else if (page == 5) {
       stayStill();  } 
}                 

int rightDoor() {
    cout << "\n\nAs you approach the door you hear a faint buzzing sound.\n";
    cout << "The handle turns and the door creaks open slowly.\n";
    cout << "You step in and the door slams forcefully behind you.\n";
    cout << "Inside is a small box pulsing with electricity.\n";
    cout << "You spot a key inside the box to unlock the door behind you.\n";
    cout << "There is also a small balcony to step out onto.\n";
    cout << "Go to page 8 to reach for the key.\n";
    cout << "Go to page 9 to step onto the balcony.\n\n";
    cout << "Which page would you like?\n";
    cin >> page;
    if (page == 8) {
       openBox(); }
    else if (page == 9) {
       stepBalcony(); }                
}

int divePast() {
    cout << "\nYou shove the figure to the floor, and dive for the passage!\n";
    cout << "Your body slams to the ground and you slide through to a well lit room.\n";
    cout << "You hear a shriek and the passage crumbles behind you.\n";
    cout << "Immediately you try to stand and notice that you are being forced to the ground.\n";
    cout << "The gravity in this room is 3 times what it should be.\n";
    cout << "You notice a hole in the ceiling and in the floor.\n";
    cout << "You can see a room through the ceiling as if you were looking down on it.\n";
    cout << "There is a switch on the wall across the room but you must stand to push it\n";
    cout << "Do you slide to the hole in the floor and fall through?\n";
    cout << "Or do you struggle for the switch?\n\n";
    cout << "Go to page 6 to fall through the floor.\n";
    cout << "Go to page 7 to try for the switch.\n\n";
    cout << "Which page would you like?\n";
    cin >> page;
    if (page == 7) {
      spaceSwitch(); }
    else if (page == 6) {
      floorFall();   }  
}    
    
int stayStill() {
    cout << "\nYou stand frozen, hoping the figure is not hostile.\n";
    cout << "It slowly glides to you, getting taller as it moves.\n";
    cout << "It stop just short of you and strechtes its long dark hand out.\n";
    cout << "In it is a square ornate box, cold to the touch.\n";
    cout << "You take it and as you do the figure turns and disappears.\n";
    cout << "Behind in the smoke, a door forms into the wall.\n\n";
    cout << "Go to page 10 to go through the door.\n";
    cin >> page;
    if (page == 10) {
      theRoom();  }
}

int theRoom() {
    cout << "\nAs you enter the room a box emergences from the floor in the center.\n";
    cout << "An insert to place the ornate box is depressed into the middle.\n";
    cout << "You place the box down and immediately a horrible siren pierces your ears.\n";
    cout << "You struggle to find the source as your ears beging to vibrate with pain.\n";
    cout << "You fall to the ground and clasp your hands to your ears!\n";
    cout << "You are barley able to make out a voice in the distance!\n";
    cout << "The voice is calling your name....\n\n";
    cout << "Go to page 24 to see who it is.\n";
    cin >> page;
    if (page == 24) {
        wakeUp ();  }
}                    

int spaceSwitch() {
    cout << "\nYou slowly slide over to the switch and struggle to reach up.\n";
    cout << "With all your strength you manage to just barely flip the switch.\n";
    cout << "Immediately a huge suction forces you to the hole in the ceiling!\n";
    cout << "You smash through the fake room projection and are ejected into space.\n";
    cout << "Within seconds you are forzen solid and only able to reach for the room.\n";
    cout << "Your frozen body is forever suspended, drifting away into space.\n\n";
}           
       
int openBox() {
    cout << "\nThe box pulses with electricity!\n";
    cout << "You can feel it as you get close when the hairs on your body raise.\n";
    cout << "Inside shines the key to the door.\n";
    cout << "You steady your hand and time the pulses...\n";
    cout << "One....Two....Three!  You grab!!  You feel the jolt through your body.\n";
    cout << "Inside your hand you feel the hard metal of the key.\n";
    cout << "Afer you recover from the shock, you insert they key and slowly twist.\n";
    cout << "You hear a faint click as the door unlocks and you slowly open it.\n\n";
    cout << "Go to page 21 to get back into the room.\n";
    cin >> page;
    if (page == 21) {
       originalRoom ();  }
}

int stepBalcony() {
    cout << "\nThe air is cold and sharp as you step onto the balcony,\n";
    cout << "As you breath in the breeze you can smell the faint smell of smoke!\n";
    cout << "As you turn around you realize the room behind you is engulfed in flames!\n";
    cout << "You panic as there is nowhere to go beside out onto the roof!\n";
    cout << "As you swing your leg over the rail, you can see the ground is close.\n";
    cout << "Close enough to reach with your feet.\n";
    cout << "You simply climb over and walk away.\n";
    cout << "You can't help but think that for some reason...\n";
    cout << "You didn't think the gas would ignite that fast.......\n";
Topic archived. No new replies allowed.