Strange bug in my text based game

Started by
1 comment, last by Lactose 6 years, 11 months ago

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?

Advertisement

Well, without seeing what is under 'while(true)' I can't really tell what the problem is, so if you could also upload the contents of that loop that would be helpful.


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.

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement