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
#pragma once
#include <string>
usingnamespace 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;
};
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.
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.