Sign in to follow this  
emforce

consecutive loop problem

Recommended Posts

hi guys i need help i have created my game with functions and i have created the main game loop and it doesn't work... the problem with it is that it just keep repeating itself and doesn't take the input from the combat options can someone please help :D
#include <iostream>

// different functions
void intro();
void combat();
// global variables
int monster_health = 50;
int player_health = 100;



int main()
{
	using namespace std;
	cout << " Welcome to the text based game Warriors of Troy! \n";
	intro();
	combat();
	cin.get();
	return 0;
}
void intro()
{	
	char name[20];
	using namespace std;
	cout << " to begin please enter your name! \n";
	cin >> name;
	cout << " hello and welcome : " << name << "\n";
	cin.get();
	cin.get();
	
}
void combat()
{
	using namespace std;

	while(monster_health > 1){
	int choice;
	cout << " you are walking down the road to the city "
		<< " and suddely you are attacked by a wild boar! ";
	cout << endl;
	cout << " what do you do? " << endl
		<< "1) crushing blow " << endl
		<< "2) accurate lunge " << endl
		<< "3) quick lunge " << endl;
	cin >> choice;
	switch(choice)
	{
		case '1' :
			cout << " Crushing blow !";
			monster_health = monster_health - 20;
			cout << " the monster now has :" << monster_health
				<< " health! ";
			
			break;
		case '2' :
			cout << " Accurate lunge! ";
			monster_health = monster_health - 15;
			cout << " the monster now has :" << monster_health
				<< " health! ";
			
			break;
		case '3' :
			cout << " quick lunge! ";
			monster_health = monster_health - 10;
			cout << " the monster now has :" << monster_health
				<< " health! ";
			
			break;
		default :
			cout << " you have entered an unavailable option! ";
			break;
	}

	};
}

Share this post


Link to post
Share on other sites
I'm not great with basic C++ but does cin >> choice return a number or a char? It looks like it's defined as an int but is reading in as a char. This won't fix your looping issue though. From what I see it should be pausing at every one of your cin statements.

I'd try testing it but I'm at work.

Share this post


Link to post
Share on other sites
Quote:
Original post by emforce
there is no bugs found


I don't think you understand. The debugger is a program that allows you to control the execution of your program. You can pause it, inspect the values of variables, jump into and out of functions. Read more here.

I will give you a hint. Your variable "choice" is of type int. The constant '1', '2' and '3' are char literals. Finally, the value of the integer 1 doesn't match the value of the character '1'.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this