How do you declare a global variable?

Hey Guys

I am trying to declare a global variable (Please don't say not to... I just wanna know how too) but I don't know where to put it or how to! Here is my code so far and the global variable I want to add but how can I do it?

Global Variable:
int playerLocation = 0;

My code so far:

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
165
#include <iostream>
#include <string>


using namespace std;

bool running = 1;

int userInput = 0;

int playerInfo[2];

void titleFunc();

int main()
{
    void titleFunc(); {
    cout << "\t\t\t\t---Xeon---\n\n\n";
    cout << "\t\t\t\t 1: Play\n";

    cin >> userInput;
    if(userInput == 1) {
            void newGameFunc(); {
            cout << "Welcome to Xeon!\nAn unexlored world full of adventurous paths and dangerous creatures!\n\n\n";
            cout << "I am Zolo... The DragonBorn!\n\n\n";
            cout << "Since you are a new comer to Xeon...\nCould you please tell me a little about yourself?\n\n";
            cout << "Are you a boy or a girl?\n 1: Boy\n 2: Girl\n";
            cin >> userInput;
            playerInfo[0] = userInput;

            cout << "And what kind of person are you?\n 1: Warrior\n 2: Archer\n 3: Thief\n 4: Scout\n 5: Mist\n";

    cin >> userInput;

    playerInfo[1] = userInput;

    cout << "You suddenly feel a gentle breeze pushing you but you ignore it\n\n\n";


    cout << "I thought I senced that in you! So now... Wait... Where are you going... What is happening!!\n\n\n";


    cout << "You suddenly feel the breeze turning into a violent wind.\nYou look down at your hands but they start disapearing...\nEverything around you is becoming blury...\nYou suddenly feel very dizzy and your vision starts to fade...\nAnd then... Everything turns Black!\n\n\n";

    playerLocation = 1;


            }

    }
    else {
        running = 0;
    }
     return 0;
}

    while (running) {
            if(playerLocation == 1) {

    cout << "You look around and you are in a wild jungle.\n You walk for a bit and you find yourself at the edge of a cliff.\n Eveything looks amazing and tropical around you!\n You look down and there is a sky blue lake.\n";
    cout << "Do you jump down or turn back?\n 1: Jump\n 2: Turn around and walk away\n";

    cin >> userInput;

    if(userInput == 1) playerLocation = 2;

    else if(userInput == 2) playerLocation = 3;

}
  if(playerLocation == 2) {

    cout << "You jumped down and landed with a big splash in the crystal water\n While you are in the water you see a small cave\n";
    cout << "Do you enter the cave or do rise to the top of the water and walk away?\n 1: Enter the cave\n 2: Rise out of the water and walk away\n";

    cin >> userInput;

    if(userInput == 1) playerLocation = 4;

    else if(userInput == 2) playerLocation = 5;

}
  if(playerLocation == 3) {

    cout << "You walk away from the cliff and find yourself walking down a hill.\nYou suddenly stop and look behind you where you see a lion starting to chase you\nTo your right there is a shallow hole you could jump down that the lion couldnt fit\n OR you could run straight ahead to some vines hanging down infront of another cliff\n\n";
    cout << "Do you go and hide in the hole or do you jump and swing on the vines?";

    cin >> userInput;

    if(userInput == 1) playerLocation = 6;

    else if(userInput == 2) playerLocation = 5;

}
  if(playerLocation == 4) {

    cout << "You have swam through a cave and found yourself on dry land... stil underground\n";
    cout << "1: Walk forward\n";

    cin >> userInput;

    if(userInput == 1) playerLocation = 7;


}
  if(playerLocation == 5) {

    cout << "You find yourself at the edge of a cliff...\nYou jump off and grab hold of some vines and start swinging.\nYou grab onto another vine but it breaks and you fall to the ground with a hard thump!\n";
    cout << "After laying on the ground for what felt like hours you finally find the strength to stand\n 1: Walk foward\n";

    cin >> userInput;

    if(userInput == 1) playerLocation = 12;

}
  if(playerLocation == 6) {

    cout << "You make it to the hole just in time before the lion swipes at you\nThere is a small hole to your right\n You manage to just fit through it and below you there is a drop into a cave\n";
    cout << "1: Drop down\n";

    cin >> userInput;

    if(userInput == 1) playerLocation = 8;


}
  if(playerLocation == 7) {

    cout << "You have made your way through the cave and you see a fiery pit.\nYou search around a bit and you find a small opening\n";
    cout << "Are you going to go through the small opening or be a badass and go across the fire\n 1: Small opening\n 2: Fiery pit\n";

    cin >> userInput;

    if(userInput == 1) playerLocation = 52;

    else if(userInput == 2) playerLocation = 9;

}
  if(playerLocation == 8) {

    cout << "You jump down through the hole and find yourself in a cave full of smoke\n";
    cout << "You believe you are about to get overwealmed by the smoke so you start running\n 1: Run ahead\n";

    cin >> userInput;

    if(userInput == 1) playerLocation = 9;


}
  if(playerLocation == 9) {

    cout << "After adventuring forward for a while you find yourself with two paths.\n One is straight ahead and one is to your right.\n";
    cout << "Do you turn right or keep going forward?\n 1: Turn right\n 2: Walk forward\n";

    cin >> userInput;

    if(userInput == 1) playerLocation = 53;

    else if(userInput == 2) playerLocation = 10;

}


    }
    return 0;
}

I havnt done the last half of the places or and mobs/weapons!

PLEASE HELP ME!
Wait ok so in my code up the top here is what I have done to declare it but IT WONT WORK!

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
#include <iostream>
#include <string>


using namespace std;

bool running = 1;

int userInput = 0;

int playerInfo[2];

void titleFunc();

int playerLocation = 0;

int main()
{
    void titleFunc(); {
    cout << "\t\t\t\t---Xeon---\n\n\n";
    cout << "\t\t\t\t 1: Play\n";

    cin >> userInput;
    if(userInput == 1) {
            void newGameFunc(); {
            cout << "Welcome to Xeon!\nAn unexlored world full of adventurous paths and dangerous creatures!\n\n\n";
            cout << "I am Zolo... The DragonBorn!\n\n\n";
            cout << "Since you are a new comer to Xeon...\nCould you please tell me a little about yourself?\n\n";
            cout << "Are you a boy or a girl?\n 1: Boy\n 2: Girl\n";
            cin >> userInput;
            playerInfo[0] = userInput;
.....

So How will I make this work?
Any variables declared outside of a function (main included) or class is considered a global variable. You already declared 4 of them.


Topic archived. No new replies allowed.