#include <iostream>
#include <string>
usingnamespace std;
void choice(int area, string name)
{
switch (area)
{
case 1:
cout<<"You appear in a dark tigtened area, you can't see anything and the only"<<endl;
cout<<"Source of light is a console asking for input."<<endl;
cout<<"it says: Please insert your name: ";
break;
case 2:
cout<<"analysing body for internal damage."<<endl;
cout<<"temporary brain loss detected."<<endl;
break;
case 3:
cout<<"Trying to open the pod "<<endl;
break;
case 4:
cout<<"user confirmation required...\nYes/No"<<endl;
break;
case 5:
cout<<"You hear cracking sounds from the damaged pod door. The doors are openning...."<<endl;
}
}
int main()
{
string name, ch1;
choice(1, name);
cin>>name;
choice(2, name);
choice(3, name);
choice(4, name);
cin>>ch1;
if (ch1=="Yes")
{
choice(5, name);
}
else
{
choice(4, name);
}
return 0;
}
So basically. I am trying to make this little text based game just for the fun of it. I would like to give players some "freedom" so basically I am making an option to return to the previous choice so he can explore multiple paths and get items in order to proceed further in the story. Unfortunately I can not come up with a way how to get the question in a loop.
As seen in the code, if player answers the first question with No it should request his input again, unfortunately it doesn't do that and ends the program instead.
in a while loop, and if you ever want to get out of the loop use break. I do recommend that you research the concept of state machines and how they are implemented in C++. Designing your game as a state machine will make it much easier to maintain and expand.
#include <iostream>
#include <string>
usingnamespace std;
void choice(int area)
{
string crap, ch1;
switch (area)
{
case 1:
cout<<"You appear in a dark tigtened area, you can't see anything and the only"<<endl;
cout<<"Source of light is a console asking for input."<<endl;
cout<<"it says: Please insert your name: ";
cin>>crap;
break;
case 2:
cout<<"analysing body for internal damage."<<endl;
cout<<"temporary memory loss detected."<<endl;
break;
case 3:
cout<<"Trying to open the pod "<<endl;
break;
case 4:
cout<<"user confirmation required...\nYes/No"<<endl;
cin>>ch1;
if (ch1=="Yes")
{
choice(5);
}
else
{
choice(4);
}
break;
case 5:
cout<<"You hear cracking sounds from the damaged pod door. The doors are openning...."<<endl;
}
}
int main()
{
choice(1);
choice(2);
choice(3);
choice(4);
return 0;
}