currently i am writing a text based RPG but, when i call for the fight my rand() either does not seed each time or it glitches to continuously gets the "Boar". if you could please take a look at it and see if the problem is in the coding, i would be grateful.

*note: this segment is still under construction. i am still trimming down the fatty extras.

int BattleAthens(int& C_Health,int& C_weapon,int& C_Exp,int& C_Gold,int& C_Dollars)
{
int E_Number = 0, E_Health = 0, E_Exp = 0, E_Gold = 0, E_Dollars = 0, E_DMG = 0;
	
	string E_Name;
	//string E_Name2;

	srand(time(NULL));

	int Enemies = rand() % 8;
		if((Enemies == 0)||(Enemies == 1))
		{
			E_Number = 1;
			E_Health = 15;
			E_Exp = 1;
			E_Gold = (rand()% 2);
			E_Dollars = (rand()%2);
			E_DMG = (rand() % 1);
			E_Name="Pixie";
		}
			if((Enemies == 2)||(Enemies == 3))
			{
				E_Number = 1;
				E_Health = 10;
				E_Exp = 1;
				E_Gold = (rand()% 1);
				E_Dollars = 0;
				E_DMG = (rand() % 5) +1;
				E_Name="Snake";
			}
				if((Enemies == 4)||(Enemies == 5))
				{
					E_Number = 1;
					E_Health = 25;
					E_Exp = 1;
					E_Gold = (rand()% 10) +2; 
					E_Dollars = 0;
					E_DMG = (rand() % 3);
					E_Name="Zealot";
				}
				if((Enemies == 6)||(Enemies == 7))
				{
					E_Number = 1;
					E_Health = 35;
					E_Exp = 1;
					E_Gold = 0;
					E_Dollars = (rand() % 4);
					E_DMG = (rand() % 5) +2;
					E_Name="Boar";
				}
				
		else
		{
			cout<<"It was a false alarm"<<endl;
			return 0;
		}//end of encounter selection
		
		//if(E_Number == 1)
		//{
			cout<<"you have encountered a "<<E_Name<<endl
				<<"get ready for a fight!"<<endl;

			while(E_Health > 0)
			{
				Options = 0;
				int Damage = 0;
				cout<<""<<endl
					<<"you have the advantage."<<endl
					<<"take the first action"<<endl
					<<"1) attack"<<endl
					<<"2) move"<<endl
					<<"3) Run"<<endl;
				cin>>Options;

				if(Options == 1)
				{
					int hit = (rand() % 10);
						if(hit == 0)
						{
							cout<<"you have missed your shot"<<endl;
						}
						else
						{
							if(C_weapon == 1)
							{
								cout<<"you have hit the target for 5dmg"<<endl;
								E_Health= E_Health - 5;
								if(E_Health <= 0)
								{
									cout<<"the "<<E_Name<<" is dead."<<endl;

									return 0;
								}
							}
							Options = 0;
						}
				}//end of attack
				if(Options == 2)
				{
					cout<<"you have run to cover close by"<<endl;

				}//end of cover
				if(Options == 3)
				{
					int Run = (rand() % 5);
					if(Run == 0)
					{
						Damage = E_DMG;
						cout<<"you have failed to escape"<<endl
							<<E_DMG<<" damage taken"<<endl;
						C_Health = C_Health - Damage;
						Damage = 0;

						if(C_Health <= 0)
						{
							cout<<"you have died"<<endl;
							return 0;
						}
						Options = 0;
					}
					else if(Run != 0)
					{
						cout<<"you have run away"<<endl
							<<""<<endl;

						return 0;
}
					}//end of if enemie attacks
				}//end of enemie attack
			}//end of battle loop
		//}//end of 1 v 1 encounter

			C_Exp += E_Exp;// + E_Exp2;
			C_Gold += E_Gold; //+ E_Gold2;
			C_Dollars += E_Dollars ;//+ E_Dollars2;

	return 0;
}

Recommended Answers

All 4 Replies

Have you called srand()? You need to seed the numbers first. You can look at this example:
cpp useful webpage

EDIT: ok, I see srand now :D...
Try to break your code a bit... segment by segment, to pinpoint exactly where it goes wrong...
For example, try to print rand(), to see what it puts out... etc...

ok, that would help. thanks

srand() should be called once and only once at the beginning of your program. Move it into main()

ok, that would help

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.