Sign in to follow this  
starfruit64

Strange bug in my text based game

Recommended Posts

Hello, I am making a simple little text-based RPG/Dungeon Crawler game to help me practice C++. But I have come across an odd bug. I have narrowed it down to this bit of code:


...more code

cout << "A " << name << " attacks!" << endl;



	//Checks who attacks first
	int initiative = rand() % 2;
	
	if(initiative == 0){
		cout << "The " << name << " strikes first!" << endl;

		int damage = (rand() % max + min);
		hp =- damage;

		cout << endl;

		cout << "The " << name << " dealt " << damage << " points of damage!" << endl;
	}

	string bin;
	cout << "Press any key to continue > " << flush;
	cin >> bin;




	system("cls");


	//Combat loop
	while(true){
  
             ...more code

 

If initiative = 1, than the program works like its supposed to: the enemy doesn't attack first and after the user inputs something after "Press any key to continue" the game continues on. But if initiative = 0 than it gets weird. It displays the "Enemy struck first...enemy dealt x damage" etc. but when the user enters input after it prompts  "Press any key to continue" the program just enters an infinite loop  with no text on screen. Any ideas as to why this could be happening?

Share this post


Link to post
Share on other sites
hp =- damage

This is wrong. This sets hp equal to -damage. If damage is 5, hp is now -5. It should be:

hp -= damage

I would guess that in the while, you are checking if the player is alive (hp > 0), which will fail because of the error above.

 

EDIT: Added some more info.

Edited by Lactose

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