i wrote this up but seem to have done something wrong and honestly i cant seem to find it, it compiles just fine, but when i run i get stuck in an infinite loop.

my WHILE doesnt seem to be working, i know its to do with the variable STAMINA but im unsure how to get it to reduce from its originally stated value.

//$$---- Form CPP ----
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//Variables
	int INIT_ENM_SKILL;
	int CURR_PLAY_SKILL;
	int ENM_Result;
	int ENM_ATTK;
	int DResult;
	int PLAY_ATTK;
	int STAMINA;
	int ENM_STAMINA;

	STAMINA=8;
	ENM_STAMINA=7;
	CURR_PLAY_SKILL=10;
	INIT_ENM_SKILL=11;

 //fight loop
do
 {

//dice code
 DResult = rand()%6 + rand()%6;   //players dice rolls
 ENM_Result = rand()%6 +rand()%6; //enemy dice rolls


//Determine opponent stats
ENM_ATTK=INIT_ENM_SKILL + ENM_Result;  //enemy's attack number

//Determine Player stats
PLAY_ATTK=CURR_PLAY_SKILL + DResult;  //players attack number




	if 	(ENM_ATTK == PLAY_ATTK)   //equal attck strength = miss
		{
		ShowMessage("Miss!");
		}

	else if (ENM_ATTK > PLAY_ATTK) // enemy attck strgth is higher
		{
		STAMINA -2;
		ShowMessage("Enemy Attacks");
		}

	else if (ENM_ATTK < PLAY_ATTK) //players is higher
		{
		ENM_STAMINA -2;
		ShowMessage("PLayer Attacks");
		}
		  }
 while (STAMINA || ENM_STAMINA != 0);   // when players stamina OR enemy's hits 0, end 

	if (STAMINA ==0)
	{
		ShowMessage("Enemy WINS!");
		exit(0);
	}
	else if (ENM_STAMINA==0)
	{
		ShowMessage("Player WINS!");
		exit(0);
	}
}
//---------------------------------------------------------------------------

thanks very much in advance

Recommended Answers

All 8 Replies

Look at your line 60 and line 66. This is not the way you decrement the value.

You should do this:

STAMINA = STAMINA - 2;

or this:

STAMINA -= 2;

thanks Denniz, i think i did try that at 1 point, ill give it another go though. thank you.

tried it, still getting stuck in infinite ; ;

Perheps

while (STAMINA || ENM_STAMINA != 0 )

never will be false if

ENM_STAMINA reduces by

ENM_STAMINA = ENM_STAMINA - 2;

as it was declared to 7.
In that case it will pass zero from +1 to -1 and therefore the loop will be infinite.

mabye it has been something that simple ill try <=0, and see what happens hopefully its that, thank you Liszt.

my poor hero must never have won in any test, poor guy ^^

just a note on the loop

while (STAMINA || ENM_STAMINA != 0 )

I'm not to sure what you think this means exactly (althought its ok this time), but im guessing its not what it actually means. it would be evaluated as follows

while ((STAMINA) || (ENM_STAMINA != 0) )

not

while ((STAMINA || ENM_STAMINA) != 0 )

Hope that makes sense

Chris

ah, thank you chris

If there's no further issue, please mark the thread as solved. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.