creating rpg battle (or something like that)

Hi guys, i am creating a somekind of rpg battle, where the program reads the input from a .txt file. i created the code but when i want to start the battle, it gave me an error vector subscript out of range. can anyone help me how to fix this? thank you very much :) here is the code

and so that we are on the same track, the content of the txt file for lamanite is
8 2
7 3
6 1

for nephite its
10 3
12 4
11 5

this is the warrior.cpp file i created
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include "warrior.h"
#include <string>
#include <iostream>


warrior::warrior(int h, int r)
{
	HitPoints = h;
	RegPoints = r;
}

int warrior::getDamage() const
{
	int damage = rand () % HitPoints;
	return damage;
}

void warrior::takeDamage(int damage)
{
	HitPoints = HitPoints - damage;
	
}

int warrior::getCurrentHP() const
{
	return HitPoints;
}

void warrior::regenerate() 
{
	 HitPoints = HitPoints + rand () % (RegPoints);
	
}

string warrior::tostring(int h, int r)
{
	return 0;
}




here is the warrior.h file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#pragma once
#include <string>

using namespace std;

class warrior
{
public:

	warrior ();
	warrior (int h, int r);
	int getDamage() const;
	void takeDamage(int damage);
	int getCurrentHP() const;
	void regenerate();
	string tostring(int h, int r);

private:
	int HitPoints;
	int RegPoints;
	int damage;
	
	
};



here is the main file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <ctime>

#include "warrior.h"

using namespace std;

void main ()
{
	srand(time(0));
	ifstream input1;
	cout << "input file name nephite: " ;
	string filename;
	cin >> filename;

	input1.open(filename);

	int HP1, RP1;
	vector <warrior*> nephites;

	while (input1 >> HP1 >> RP1)
	{
		nephites.push_back(new warrior(HP1, RP1));
	}

	cout << nephites.size() << endl;
	

	

	ifstream input2;
	cout << "input file name lamanite : ";
	string filename2;
	cin >> filename2;

	input2.open(filename2);
	int HP2, RP2;
	vector <warrior*> lamanites;

	while (input2 >> HP2 >> RP2)
	{
		lamanites.push_back(new warrior(HP2, RP2));
	}
	cout << lamanites.size() << endl;

	


	cout << endl << "Battle" << endl;

	warrior nephitesw  = warrior (HP1,RP1);
	warrior lamanitesw = warrior (HP2,RP2);	
	
		while ((nephites.size() > 0) && (lamanites.size() > 0))
		{
			
			int rN = rand () % nephites.size() ;
			int rL = rand () & lamanites.size() ;
			cout << rN << "xx" << rL << endl; // so that i know what rN and rL is

			while((nephites[rN]->getCurrentHP() > 0) && (lamanites[rL]->getCurrentHP() > 0)) // the program can't execute this part of the code
			{
				nephites[rN]->takeDamage(lamanites[rL]->getDamage());
				lamanites[rL]->takeDamage(nephites[rN]->getDamage());

				if(lamanites[rL]->getCurrentHP() > 0)
				{lamanites[rL]->regenerate();}
				else
				{lamanites.erase(lamanites.begin() + (rL));} 

				if(nephites[rN]->getCurrentHP() > 0) 
				{nephites[rN]->regenerate();} 
				else					
				{nephites.erase(nephites.begin() + (rN));}								
			}	
			
			cout <<"NEP HP: " << nephites[rN]->getCurrentHP() << " " << "LAM HP: " << lamanites[rL]->getCurrentHP() << endl;
		}
		

	system ("Pause");
	
}



Im not familiar with using the "&" the way you did in line 63, but im gonna go ahead and say rL is bigger than lamanites.size(), and so lamanites[rL] does not exist, so the vector subscript rL IS out of range.

Does that make sense? lol
oh right!! exactly I didnt notice that! thank you very much i really appreciate your help :)
but why is it still vector subscript out of range?
Well i don't know what you changed, but you need to make sure rL is <= lamanites.size().

So put some kind of restrictions on your random number.
i changed the '&' to '%' and i printed what rN and rL is, i got 1 and 0, which both are less than the vector size. but it is still out of range
You change the size of the vectors in your (inner) while loop. You do not change the values of rN or rL.

Last edited on
I don't quite get what you mean, can you explain a little more?
You use erase within the inner while loop. erase removes elements from the vector. Removing elements from the vector reduces the size of the vector.

rN and rL were selected with vectors of a certain size in mind. They are not reselected when the size of the vectors are reduced, therefore they may be out of bounds when the size of the vectors are reduced.
what code should i put in my to string function to make it work? i want to print the warriors with the hp and the rp
Topic archived. No new replies allowed.