How to make a choose your own path game...

Pages: 12
I want to know how to make a choose your own path game. I am relatively new to c++. HTML got boring so I am here now. I want to know how to make it so you can press 1 and enter for the first choice/path. Or press 2 and enter for the second path/choice without the program just closing or having to link 100 other programs to it. I just want it to be in command prompt. No GUI. Not that good yet.
closed account (3TXyhbRD)
The following code reads and prints the 3 numbers that it previously read.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main(){
    int a[3];
    for (int i = 0; i < 3; i++){
        cout << "Endter a number" << endl;
        cin>>a[i];
    }

    cout << "Your numbers are:" << endl;
    for (int i = 0; i < 3; i++)
        cout << a[i] << endl;
}

Is something like this that you want?
I will have to try it out. I am not currently at a computer but I will be around 9:00. I will test it and tell you how it goes. Also one more question. How do I get it to go to the next page for different options?
ok.

example:

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
#include <iostream>
using namespace std;

int main ()
{
  int choice;

  cout << "<your question here> 1 = first 2= second";
  cin >> choice;

 if (choice == 1)

 {

  cout << "<your message here>";
  
  }

 if (choice == 2)

 {

  cout << "<your message here>";

  }

 if (choice > 2)

 {

   cout << "<your message here>";

  }

return 0;

}


that would do it
i think this i what you want.
Last edited on
closed account (3TXyhbRD)
Either if.. else or switch.. case

1
2
3
4
5
6
if (a == 1)
    // page 1
else if (a == 2)
    // page 2
else
    // invalid page 


1
2
3
4
5
6
7
8
9
10
switch (a){
case 1:
    // page 1
    break;
case 2:
    // page 2
    break;
default:
    // invalid page
}


Docs: http://www.cplusplus.com/doc/tutorial/control/
This seems like it would work but, how do I get it to ask more questions if they pick 1 or 2?
Last edited on
So how would that code above make the program not close but it enable more questions to be asked?
closed account (3TXyhbRD)
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
#include <iostream>

using namespace std;

int main(){
    int option;
    cout << "Option: ";
    cin >> option;
    switch (option){
    case 1:
        cout << "Option 1 selected" << endl << "Option: ";
        cin >> option;
        switch (option){
        case 1:
            cout << "Option 1.1 selected" << endl;
            break;
        case 2:
            cout << "Option 1.2 selected" << endl;
            break;
        default:
            cout << "No option selected" << endl;
        }
        break;
    case 2:
        cout << "Option 2 selected" << endl;
        break;
    }
    default:
        cout << "No option selected" << endl;
}
dangit i wrote the comment when he changed the code.
Last edited on
Ok I will let you know if you doesn't work.
closed account (3TXyhbRD)
My bad, a bad brace placed.

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
#include <iostream>

using namespace std;

int main(){
    int option;
    cout << "Option: ";
    cin >> option;
    switch (option){
    case 1:
        cout << "Option 1 selected" << endl << "Option: ";
        cin >> option;
        switch (option){
        case 1:
            cout << "Option 1.1 selected" << endl;
            break;
        case 2:
            cout << "Option 1.2 selected" << endl;
            break;
        default:
            cout << "No option selected" << endl;
        }
        break;
    case 2:
        cout << "Option 2 selected" << endl;
        break;
    default:
        cout << "No option selected" << endl;
    }
    return 0;
}


Also added return statement for int main();
this will work.
closed account (3TXyhbRD)
Program Programmer wrote:
dangit i wrote the comment when he changed the code.

Is that aimed at me? Because I see no "last edited" thingy at any of my ports on this topic.
closed account (3TXyhbRD)
posts*
i commented the same time you did. and i screwed.
and if you will excuse me, i have a calculator to program for my homework assigment.
Last edited on
closed account (3TXyhbRD)
Oh, it's okay. Just be more clear next time.
It looks like program programmer's code worked best for me. But i'm still not understanding how to make it so that when they pick option 1 or 2 it goes to the next question on a fresh cmd screen?
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
#include <iostream>
using namespace std;

int main ()
{
  int choice;

  cout << "Male or Female?\n\n> 1 = Male\n\n 2= Female\n\n";
  cin >> choice;

 if (choice == 1)

 {

  cout << "<You have selected Male!>\n\n";
  
  }

 if (choice == 2)

 {

  cout << "You have selected female!\n\n";

  }

 if (choice > 2)

 {

   cout << "ERROR INVALID SELECTION\n\n\n\n";

  }

return 0;

}



This is my modified version of "program programmer's" code! although this is a test and not a part of my game. This seems to work i just need help with what i have posted above!^^
[post was here]
Last edited on
closed account (3TXyhbRD)
Lines 36 to 46 will never get executed that way.
Pages: 12