problem with text based games

If you runn my program, you can see that once the display() function is called, all variables are 0. help!? also, how could i go about getting rid of all these global variables before they begin to add up!?

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
166
167
  
#include <iostream>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

// GLOBAL VARIABLES
string charname;
int navatk;
int navdef;
int navspd;
int navhpo;
int navluk;
int navepo;

//GLOBAL VARIABLES
void display();
void display(){
    cout<<"attack: "<< navatk <<endl;
    cout<<"defense: "<< navdef <<endl;
    cout<<"speed: "<< navspd <<endl;
    cout<<"health points: "<< navhpo <<endl;
    cout<<"luck: "<< navluk <<endl;
    cout<<"energy points: "<< navepo <<endl;
};
//INTRO FUNCTION
int main();
int main(){
    cout<<"what's your name again?"<<endl; 
    cin>>charname; 
    cout<<"hello, "<<charname<<endl;
    sleep(2);
    cout<<"welcome to the pentagon"<<endl;
    sleep(1);
    cout<<"year:4056 C.E." << endl;
    sleep(1);
    cout<<"you have been nominated to control a 'navigator.' a digital fighting warrior that we use to fight the rebellion."<<endl;
    sleep(1);
    cout<<"..."<<endl;
    sleep(1);
    cout<<"what's this? you've never heard of the rebellion before?"<<endl;
    sleep(1);
    cout<<"the year was 2015, when the citizens of the united states of america rose up against the government. little did they know, the governmet had installed a top-secret project called 'SEVEN' (or s.ecretly e.liminate v.iolent e.lectronic n.ews) into the world wide web . they used this successfully to break up riots by intercepting internet-bound messages. soon after, the rebellion's top scientists figured out the programs weak-spots, and created viruses 'bugs' to destroy said program."<<endl;
    sleep(12);
    cout<<"that's where you come in." << endl;
    sleep(1);
    cout<<"we've created an elite squad of web 'navigators' to destroy any viruses we encounter." <<endl;
    sleep(1);
    cout<<"you will be tasked with controlling one of these 'navigators' to end the rebellion once and for all."<<endl;
    sleep(1);
    cout<<"let me show you them"<<endl;
    sleep(1);
    cout<<"..."<<endl;
    sleep(1);
    cout<<"bladenav: a well balanced navigator that can wield a multitude of de-bugging weapons"<<endl;
    cout<<"arrownav: a speed-oriented navigator that can attack stealthily from far range"<<endl;    
    cout<<"fistsnav: an attack-heavy navigator with low speed but with a variety of high-power weapons"<<endl;
    cout<<"shellnav: an extremely high defense navigator with low speed and attack "<<endl;
    cout<<"golemnav: a high attack, high defense navigator with low speed, and a narrow range of attacks "<< endl;
    cout<<"flamenav: a navigator with flame-based attacks with relatively large luck"<<endl;
    cout<<"aqueonav: a navigator with aqua-based attacks with relatively large luck"<<endl;    
    cout<<"plantnav: a navigator with plant-based attacks with relatively large luck"<<endl;
    cout<<"psychnav: a high hit points navigator with indirect attacking abilities"<<endl;
    cout<<"chicknav: a navigator with terrible stats all around and no redeeming qualities whatsoever except for it's 100% luck stat."<<endl;
    sleep(3);
    cout<<"you may choose one and only one to control. pick wisely"<<endl;
    string navchoice; 
    cin>> navchoice;
//STAT SETTING
    if (navchoice == "bladenav")
    {
        int navatk = 50;
        int navdef = 50;
        int navspd = 50;
        int navhpo = 100;
        int navluk = 10;
        int navepo = 40;
    };
    if (navchoice =="arrownav")
    {
        int navatk = 55;
        int navdef = 30;
        int navspd = 65;
        int navhpo = 100;
        int navluk = 10;
        int navepo = 40;
    };
    if (navchoice =="fistsnav")
    {
        int navatk = 80;
        int navdef = 55;
        int navspd = 10;
        int navhpo = 115;
        int navluk = 10;
        int navepo = 30;
    };   
    if (navchoice =="shellnav")
    {
        int navatk = 25;
        int navdef = 80;
        int navspd = 25;
        int navhpo = 120;
        int navluk = 10; 
        int navepo = 40;
    };
    if (navchoice =="golemnav")
    {
        int navatk = 70;
        int navdef = 70;
        int navspd = 10;
        int navhpo = 100;
        int navluk = 10;
        int navepo = 40;
    };    
    if (navchoice =="flamenav")
    {
        int navatk = 50;
        int navdef = 50;
        int navspd = 40;
        int navhpo = 100;
        int navluk = 15;
        int navepo = 45;
    };  
    if (navchoice =="aqueonav")
    {
        int navatk = 50;
        int navdef = 50;
        int navspd = 40;
        int navhpo = 100;
        int navluk = 15;
        int navepo = 45;
    };
    if (navchoice =="plantnav")
    {
        int navatk = 50;
        int navdef = 50;
        int navspd = 40;
        int navhpo = 100;
        int navluk = 15;
        int navepo = 45;    
    };
    if (navchoice =="psychnav")
    {
        int navatk = 20;
        int navdef = 30;
        int navspd = 30;
        int navhpo = 150;
        int navluk = 10;
        int navepo = 50;
    };
    if (navchoice =="chicknav")
    {
        int navatk = 20;
        int navdef = 20;
        int navspd = 20;
        int navhpo = 100;
        int navluk = 100;
        int navepo = 40;
    };
    sleep(1);
    display();
    sleep(1);
    cout<<"is this you're final selection, "<< charname <<"?"<<endl;
    return 0;
}
Oops. You're declaring local variables in your function and setting those instead of your global variables. You'll want to get rid of all those nasty prefixed ints in that function

Try to stray away from global variables in the future.

-Albatross
thanks so much!
sorry, im self taught...

and how excactly would I go about this program without declaring so many global variables?

-ASCII14
You could declare them in main and pass them as arguments to functions, preferably via reference. That would work fine, at least until you learn about more elegant solutions.
http://www.cplusplus.com/doc/tutorial/functions2/

-Albatross
Last edited on
Just so you know, the function prototypes on lines 19 and 29 are unnecessary, especially the one on line 29, because you'll never call main() explicitly.
Also, the semi-colons on the closing braces of your ifs aren't necessary either. They wont affect compilation or anything, but they don't do anything.
yes! thank you albatross, now i've gotten rid of the global variables by doing exactly what you suggested.

[code]
#include <iostream>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

// GLOBAL VARIABLES
string charname;

void display(int navatk, int navdef, int navspd, int navhpo, int navluk, int navepo);
void display(int navatk, int navdef, int navspd, int navhpo, int navluk, int navepo){
cout<<"attack: "<< navatk <<endl;
cout<<"defense: "<< navdef <<endl;
cout<<"speed: "<< navspd <<endl;
cout<<"health points: "<< navhpo <<endl;
cout<<"luck: "<< navluk <<endl;
cout<<"energy points: "<< navepo <<endl;
};
//INTRO FUNCTION
int main();
int main(){
cout<<"what's your name again?"<<endl;
cin>>charname;
cout<<"hello, "<<charname<<endl;
sleep(2);
cout<<"welcome to the pentagon"<<endl;
sleep(1);
cout<<"year:4056 C.E." << endl;
sleep(1);
cout<<"you have been nominated to control a 'navigator.' a digital fighting warrior that we use to fight the rebellion."<<endl;
sleep(1);
cout<<"..."<<endl;
sleep(1);
cout<<"what's this? you've never heard of the rebellion before?"<<endl;
sleep(1);
cout<<"the year was 2015, when the citizens of the united states of america rose up against the government. little did they know, the governmet had installed a top-secret project called 'SEVEN' (or s.ecretly e.liminate v.iolent e.lectronic n.ews) into the world wide web . they used this successfully to break up riots by intercepting internet-bound messages. soon after, the rebellion's top scientists figured out the programs weak-spots, and created viruses 'bugs' to destroy said program."<<endl;
sleep(12);
cout<<"that's where you come in." << endl;
sleep(1);
cout<<"we've created an elite squad of web 'navigators' to destroy any viruses we encounter." <<endl;
sleep(1);
cout<<"you will be tasked with controlling one of these 'navigators' to end the rebellion once and for all."<<endl;
sleep(1);
cout<<"let me show you them"<<endl;
choice:
sleep(1);
cout<<"..."<<endl;
sleep(1);
cout<<"bladenav: a well balanced navigator that can wield a multitude of de-bugging weapons"<<endl;
cout<<"arrownav: a speed-oriented navigator that can attack stealthily from far range"<<endl;
cout<<"fistsnav: an attack-heavy navigator with low speed but with a variety of high-power weapons"<<endl;
cout<<"shellnav: an extremely high defense navigator with low speed and attack "<<endl;
cout<<"golemnav: a high attack, high defense navigator with low speed, and a narrow range of attacks "<< endl;
cout<<"flamenav: a navigator with flame-based attacks with relatively large luck"<<endl;
cout<<"aqueonav: a navigator with aqua-based attacks with relatively large luck"<<endl;
cout<<"plantnav: a navigator with plant-based attacks with relatively large luck"<<endl;
cout<<"psychnav: a high hit points navigator with indirect attacking abilities"<<endl;
cout<<"chicknav: a navigator with terrible stats all around and no redeeming qualities whatsoever except for it's 100% luck stat."<<endl;
sleep(3);
cout<<"you may choose one and only one to control. pick wisely"<<endl;
string navchoice;
cin>> navchoice;
//STAT SETTING
int navatk;
int navdef;
int navspd;
int navhpo;
int navluk;
int navepo;

if (navchoice == "bladenav")
{
navatk = 50;
navdef = 50;
navspd = 50;
navhpo = 100;
navluk = 10;
navepo = 40;
};
if (navchoice =="arrownav")
{
navatk = 55;
navdef = 30;
navspd = 65;
navhpo = 100;
navluk = 10;
navepo = 40;
};
if (navchoice =="fistsnav")
{
navatk = 80;
navdef = 55;
navspd = 10;
navhpo = 115;
navluk = 10;
navepo = 30;
};
if (navchoice =="shellnav")
{
navatk = 25;
navdef = 80;
navspd = 25;
navhpo = 120;
navluk = 10;
navepo = 40;
};
if (navchoice =="golemnav")
{
navatk = 70;
navdef = 70;
navspd = 10;
navhpo = 100;
navluk = 10;
navepo = 40;
};
if (navchoice =="flamenav")
{
navatk = 50;
navdef = 50;
navspd = 40;
navhpo = 100;
navluk = 15;
navepo = 45;
};
if (navchoice =="aqueonav")
{
navatk = 50;
navdef = 50;
navspd = 40;
navhpo = 100;
navluk = 15;
navepo = 45;
};
if (navchoice =="plantnav")
{
navatk = 50;
navdef = 50;
navspd = 40;
navhpo = 100;
navluk = 15;
navepo = 45;
};
if (navchoice =="psychnav")
{
navatk = 20;
navdef = 30;
navspd = 30;
navhpo = 150;
navluk = 10;
navepo = 50;
};
if (navchoice =="chicknav")
{
navatk = 20;
navdef = 20;
navspd = 20;
navhpo = 100;
navluk = 100;
navepo = 40;
};
sleep(1);
display( navatk, navdef, navspd, navhpo, navluk, navepo);
sleep(1);
cout<<"is this you're final selection, "<< charname <<"?"<<endl;
string yesno;
cin >> yesno;
if(yesno == "no"){
cout<< "let me show you them again" << endl;
goto choice;
}
return 0;
}/code]
Topic archived. No new replies allowed.