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.
#include <iostream>
usingnamespace 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;
elseif (y == b)
cout<<"Wimp."<<endl;
elseif (z == c)
cout<<"You save the princess."<<endl;
}
int main (){
int game;
game = playGame();
return 0;
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.
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?
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!