Hello guys!
First I'd like to say that I started studying C++ just a few days ago with the HUGE help of Alex Allain's guide.
Now, I've learned some basics (I'm currently at lesson 16 of Alex's guide) and I started to write a small text game of my own. I got through the beginning well, but then a problem arose.
I'll link the code first and point out the part I have problem with.
So here's the code (scroll below the code to see my problem):
Code:
#include <iostream>#include <cstring>
using namespace std;
int input;
char string[256];
void line()
{
cout<<"-------------------";
}
void game1()
{
cout<<"Welcome to the world of whatever (this is just and example for the forums)"<<string<<"!"<<endl;
}
void game0()
{
char string[256];
cin.getline(string, 256, '\n');
cout<<"Please enter your name: ";
cin.getline(string, 256, '\n');
cout<<endl;
cout<<"Confirm name: "<<string<<"? (yes/no): ";
cin.getline(string, 256, '\n');
cout<<endl;
if (strcmp(string, "yes")==0)
{
game1();
}
else if(strcmp(string, "no")==0)
{
cout<<"Please re-enter your name correctly: ";
cin.getline(string, 256, '\n');
game1();
}
}
void instructions()
{
cout<<endl;
cout<<"Instructions:"<<endl;
cout<<"-------------"<<endl;
cout<<endl;
cout<<"Always press ENTER to progress."<<endl;
cout<<endl;
cout<<"If you are prompted to answer either yes or no"<<endl;
cout<<"make sure your grammar is correct. Only use lower-"<<endl;
cout<<"case letters in these cases or the game will crash."<<endl;
cout<<endl;
cout<<"Everything else is pretty self-explanatory."<<endl;
cout<<endl;
cout<<"Press ENTER to move back to Main Menu."<<endl;
cout<<endl;
cin.ignore();
cin.get();
cout<<"Test game!"<<endl;
cout<<endl;
int input;
cout<<"1.Start game"<<endl;
cout<<"2.Exit"<<endl;
cout<<"3.Instructions"<<endl;
cout<<endl;
cout<<"---------"<<endl;
cout<<"Selection: ";
cin>>input;
cout<<"---------"<<endl;
cout<<endl;
switch (input)
{
case 1:
game0();
break;
case 2:
cout<<"Exiting program."<<endl;
cin.ignore();
cin.get();
break;
case 3:
instructions();
break;
}
}
int main()
{
int input;
cout<<"Test game."<<endl;
cout<<endl;
cout<<"1.Start game"<<endl;
cout<<"2.Exit"<<endl;
cout<<"3.Instructions"<<endl;
cout<<"---------"<<endl;
cout<<"Selection: ";
cin>>input;
cout<<"---------"<<endl;
cout<<endl;
switch (input)
{
case 1:
game0();
break;
case 2:
cout<<"Exiting program."<<endl;
cin.ignore();
cin.get();
break;
case 3:
instructions();
break;
}
}
First of all, I know I'm using a lot of useless stuff in there but this is my first project so all that matters to me is that it works as intented.
Anyway the problem is that I want to use the name defined in void game0() later on in the game by using the string's definition but I can't because it's defined in another void.
So my question is: is there any way I could use the string defined in void game0() in other voids through some coding?
Thanks in advance. And I'm sorry if this question has already been answered in another thread. I tried looking through the forums, but couldn't find an answer.