Getting question in loop according to user input

Write your question here.

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

I would appreciate any help. Thank you.
Well you need to put all of this:

1
2
3
4
5
6
7
8
9
10
11
cin>>ch1;

    if (ch1=="Yes")
    {
        choice(5, name);

    }
    else
    {
        choice(4, name);
    }


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.
Thank you for your help. I have found a work around without using the "while" function.

I will paste the code here.

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
#include <iostream>
#include <string>
using namespace 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;
}
Topic archived. No new replies allowed.