Text based rpg (loop help)

Hi,

I am having trouble with the loop function. I have to write a text based game.
When the user inputs 1 or 2, the program displays a logic error. When the user inputs 3, it does work. I would appreciate your feedback.

Thank you

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
 // This is a text based game
#include<ctime>
#include<iostream>
using namespace std;
int main()
{

    int rules (1);
    int play (2);
    int quit (3);
    int chose;

    cout << "You are about to enter the arena" << endl;
    cout << "1:Rules\n2:Play\n3:Quit\n";

     cin >> chose ;
   {
  while (chose == 1 || chose == 2)

        if (chose == 1)
            {
                cout << "You chose the rules.";
            }
        else if (chose == 2)
            {
                cout << "you want to play.";
            }
    chose++;
        if (chose == 3)
        {
            cout << "you quit the game.";
        }

   }
   cin.get();
 }
Your curly braces are misplaced.
Try using a Switch Statement.

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
#include <iostream>
using namespace std;
//declared a function to present the game rules.
void rules();
int main()
{
int choice;
do
{
cout<<"0 - Quit.\n";
cout<<"1 - View the Game Rules.\n";
cout<<"2 - Play.\n";
cout<<"Choice: ";
cin>>choice;
switch (choice)
{
case 0:
cout<<"Thank you for playing.\n";
break;
case 1:
//Provide your own rules here. I used a function.
rules();
break;
case 2:
cout<<"You decided to Play, Goodluck!\n";
break;
default:
cout<<"Illegal choice.\n";
}
}while(choice != 0);//The loop will keep looping as long as the user doesn't enter '0'.
return 0;
}
//function definition:
void rules()
{
	cout<<"The rules of the game are as follows...\n";
        //Enter the rules here

}
Last edited on
Thank you. I will give this a try.
Topic archived. No new replies allowed.