(Homework Problem) Text based adventure game using While loop, but don't completely understand why it's not working.

So I have been working on my intro to programming assignment and got to this problem where we must create an adventure game with three options all text-based. The directions stated to use a while loop so the user could keep playing until they triggered the option to go home and end the loop and game. I got the code working for some if statement, but for some reason i can't figure out why no matter what letter I type it always prints the first line and skips over the other two options. Also when I use the while code it simply does nothing and reaches none of the if and else if statements.

I am using code blocks within Linux as that is required by our school. I have also done some research into what I needed for a while loop and feel as though my code is correct within the while loop, but not outside the while loop. I also researched others who have done text-based adventures, but they were either way too complicated or contained stuff I purely have no knowledge of.

If someone could help guide me and not give the answer, but rather help me understand where I went wrong and let me figure the problem out that would be greatly appreciated.

Here is my code so far:

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

using namespace std;

int playGame(){

    cout<<"Welcome to your adventure!"<<endl
        <<"Choose your action: "<<endl
        <<"a. Fight the dragon."<<endl
        <<"b. Go home."<<endl
        <<"c. Save the princess."<<endl;
    int x;
    int a;
    cin>>a;
    x = a;
    int y;
    int b;
    cin>>b;
    y = b;
    int z;
    int c;
    cin>>c;
    z = c;
    while (x != b)
        if (x == a)
            cout<<"You fight the dragon."<<endl;
        else if (y == b)
            cout<<"Wimp."<<endl;
        else if (z == c)
            cout<<"You save the princess."<<endl;


}

int main (){


    int game;
    game = playGame();

    return 0;
Last edited on
you forgot brackets {} in your while loop...
Last edited on
Okay I put brackets into the while loop and it still brings nothing when i enter a, b, or c as an input. Is there another issue on the outside or inside of the loop?
Firstly:
your playGame() function doesn't return any int value, so you may remove it

Secondly:
your main() function also lacks a closing bracket (could be a copy-paste problem too)

Also:
I dont totally understand if(x==a), are you trying to compare x with character 'a'?
If yes then the code should be if(x=='a'), same goes for other two conditions

And int is not a good choice to compare chars

HOPE IT HELPS


Okay so I removed the playGame() from main. Now to answer your confusion I am trying to take a users input of a, b, and c for the three options. If this is done correctly it should take the input and if a set it equal to x. When the function runs the while loop then it should check first to see if x = b cause if so it should end the game and say "Wimp", otherwise if a is chosen then it should say "You fight the dragon" (And technically it should select a random number to see if you win or lose, but I havent gotten to that point). So I fixed it with (x=='a') and did that to the others, but now it just says fight the dragon no matter the input and it runs it infinitely too.
12
13
14
15
 int x;
    int a;
    cin>>a;
    x = a;


so logically it will always fight the dragon
Alright, now i'm lost. How do I do it so it chooses between the different conditions? Such that if it is a or c then it keeps looping and does the specific cout part or b and effectively end the loop and print the message?
Okay,

Instead of if else try using switch, aim for this output

Welcome to your adventure!
Choose your action: 
a. Fight the dragon.
b. Go home.
c. Save the princess.
what would you do:


So after taking input compare it in switch statement...

1
2
3
4
5
6
7
8
9
10
cin>>input;
switch(input)
{
case 'a':
            cout<<"You fight the dragon."<<endl;
break;
       
//and so on...
}
FINALLY!!!!!!! I am so excited that it worked. Thanks for leading me into the correct direction. I remember reading about switch statements on a website, but never thought I could use it for this situation.

I will continue my research on the while loop and see how to get it to work too, but this helps greatly thanks!
I would do it like this:

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
#include <iostream>
#include <ctype.h>
#include <stdlib.h>

using namespace std;

char playGame()
{
  cout << "Welcome to your adventure!"<<endl
       << "Choose your action: "<<endl
       << "a. Fight the dragon."<<endl
       << "b. Go home."<<endl
       << "c. Save the princess."<<endl;
  
  char choice = 0;
  cin.get(choice);
  cin.ignore(255, '\n');
  

  return choice;
     
}

int main ()
{
  char choice = 0;
  do
  {
    choice = tolower(playGame());

    if (choice == 'a')
    {
      // fight dragon
      cout << "Fight dragon\n"; 
    }
    else if (choice == 'c')
    {
      // save the princess
      cout << "Save princess\n";
    }
    else if (choice == 'b')
    {
      // go home
      cout << "Go home\n";
    }
    else
    {
      // handle invalid choice
      cout << "invalid choice\n";
    }
  }while(choice != 'b');

  system("pause");
  return 0;
}
I would use integers instead of 'a','b','c'..... Because you can find 1,2,3 on keyboard easier than alphabets so a better UX for console apps/games

And Thomas' way is awesome, it even handles the unexpected scenarios...
Last edited on
Topic archived. No new replies allowed.