Jump to content
  • Advertisement
Sign in to follow this  
starfruit64

Strange bug in my text based game

This topic is 593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!