I am reading objects from a text file and placing them in a vector. I am then trying to display them using a vector. I have the program working but not quite correctly. The problem I am having is the vector a collection of pointers and the objects are being overwritten and I am only getting the last two objects of the text file. Any idea on how to fix this so I can get all of the information from the text file not just the last two entries?
entities.txt
4
hero
Fred The Strong
100
The Outcast
30.89
22
18
9
6
villain
Scout
25
Goblin
3
0
villain
Arch Shaman
50
Orc
60
1
hero
Countess de Winter
100
none
200.00
9
11
19
14
main
#include "Entity.h"
#include "Hero.h"
#include "Villian.h"
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
vector<Entity *> fileObjects;
Hero hero1;
Villian villian1;
ifstream readFile("entities.txt");
string line;
getline(readFile, line);
while(!readFile.eof())
{
getline(readFile, line);
if(line == "hero")
{
getline(readFile, line);
hero1.name = line;
getline(readFile, line);
hero1.hp = stoi(line);
getline(readFile, line);
hero1.guild = line;
getline(readFile, line);
hero1.coin = stof(line);
getline(readFile, line);
hero1.strength = stoi(line);
getline(readFile, line);
hero1.dexterity = stoi(line);
getline(readFile, line);
hero1.faith = stoi(line);
getline(readFile, line);
hero1.wisdom = stoi(line);
fileObjects.push_back(&hero1);
}
else if (line == "villain")
{
getline(readFile, line);
villian1.name = line;
getline(readFile, line);
villian1.hp = stoi(line);
getline(readFile, line);
villian1.race = line;
getline(readFile, line);
villian1.level = stoi(line);
getline(readFile, line);
if(line == "0")
{
villian1.isBoss = false;
}
else
{
villian1.isBoss = true;
}
fileObjects.push_back(&villian1);
}
}
readFile.close();
for(int i = 0; i < fileObjects.size(); i++)
{
fileObjects[i]->display();
}
return 0;
}
Entity header
#include <iostream>
using namespace std;
#ifndef ENTITY_H
#define ENTITY_H
class Entity {
public:
string name;
int hp;
virtual void display();
Entity(string name, int hp);
Entity();
Entity(const Entity& orig);
virtual ~Entity();
private:
};
#endif /* ENTITY_H */
Entity source
#include "Entity.h"
Entity::Entity() {
name = "";
hp = 0;
}
Entity::Entity(string name, int hp)
{
this->name = name;
this->hp =hp;
}
void Entity::display()
{
cout << "Name: " << name << endl;
cout << "HP: " << hp << endl;
}
Entity::Entity(const Entity& orig) {
}
Entity::~Entity() {
}
Hero header
#include "Entity.h"
#ifndef HERO_H
#define HERO_H
class Hero:public Entity {
public:
string guild;
float coin;
int strength;
int dexterity;
int faith;
int wisdom;
void display();
Hero(string name, int hp, string guild, float coin, int strength, int dexterity, int faith, int wisdom);
Hero();
Hero(const Hero& orig);
virtual ~Hero();
private:
};
#endif /* HERO_H */
Hero source
#include "Hero.h"
Hero::Hero() {
guild = "";
coin = 0.0f;
strength = 0;
dexterity = 0;
faith = 0;
wisdom = 0;
}
Hero::Hero(string name, int hp, string guild, float coin, int strength, int dexterity, int faith, int wisdom):Entity(name, hp)
{
this->guild = guild;
this->coin = coin;
this->strength = strength;
this->dexterity = dexterity;
this->faith = faith;
this->wisdom = wisdom;
}
void Hero::display()
{
Entity::display();
cout << "Guild: " << guild << endl;
cout << "Coin: " << coin << endl;
cout << "Strength: " << strength << endl;
cout << "Dexterity: " << dexterity << endl;
cout << "Faith: " << faith << endl;
cout << "Wisdom: " << wisdom << endl;
}
Hero::Hero(const Hero& orig) {
}
Hero::~Hero() {
}
Villian header
#include "Entity.h"
#ifndef VILLIAN_H
#define VILLIAN_H
class Villian:public Entity {
public:
string race;
int level;
bool isBoss;
void display();
Villian(string name, int hp, string race, int level, bool isBoss);
Villian();
Villian(const Villian& orig);
virtual ~Villian();
private:
};
#endif /* VILLIAN_H */
Villian source
#include "Villian.h"
Villian::Villian() {
race = "";
level = 0;
isBoss = false;
}
Villian::Villian(string name, int hp, string race, int level, bool isBoss):Entity(name, hp)
{
this->race = race;
this->level = level;
this->isBoss = isBoss;
}
void Villian::display()
{
Entity::display();
cout << "Race: " << race << endl;
cout << "Level: " << level << endl;
cout << "Is A Boss: " << isBoss << endl;
}
Villian::Villian(const Villian& orig) {
}
Villian::~Villian() {
}