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!?
#include <iostream>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
usingnamespace 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.
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/
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.
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;