Quick attempt at a solution:
#include <string>
#include <fstream>
#include <map>
#include <sstream>
#include <cstdlib>
using namespace std;
class Attack {
string name;
int minDamage, maxDamage;
string type;
public:
Attack(string n, int mind, int maxd, string t) : name(n), minDamage(mind), maxDamage(maxd) type(t) {}
string getName() { return name; }
int getMinDamage() { return minDamage; }
int getMaxDamage() { return maxDamage; }
string getType() { return type; }
};
class Monster {
string desc;
int health;
map<string, Attack> attacks;
public:
Monster(string d, int h, map<string, Attack> a) : desc(d), health(h), attacks(a) {}
string getDesc() { return desc; }
Attack getAttack(string s) { return attacks.at(s); }
};
map<string, Monster> monsters;
/*
wolf
desc = A slavering wolf. Beware!
health = 100
attacks
bites = 5-10 pierce
claws = 2-5 slash
*/
void processMonster(stringstream data) {
string name, desc, healthstr;
int health;
map<string, Attack> attacks;
getline(data, name);
getline(data, desc);
desc = desc.substr(desc.find(" = "+4));
getline(data, healthstr);
healthstr = healthstr.substr(healthstr.find(" = "+4));
health = atoi(healthstr.c_str());
data.getline(); // discard the "attacks" line
string temp;
while(getline(data, temp)) {
string atkname = temp.substr(0, temp.find(" = "));
string dmg = temp.substr(temp.find(" = ")+4, temp.find(" ", temp.find(" = ")+4));
string type = temp.substr(temp.find(" ", temp.find(" = ")+4)+1);
int mindmg, maxdmg;
if(dmg.find("-") != string::npos) { // variable damage
mindmg = atoi(dmg.substr(0, dmg.find("-")).c_str());
maxdmg = atoi(dmg.substr(dmg.find("-")+1).c_str());
}
else { // static damage
mindmg = maxdmg = atoi(dmg.c_str());
}
attacks[atkname] = Attack(atkname, mindmg, maxdmg, type);
}
monsters[name] = Monster(name, desc, health, attacks);
}
void readFile(string fname) {
string line;
ifstream f(fname);
stringstream lines;
bool newMonsterFlag = true;
while(getline(f, line)) {
if(line.at(0) != " " && line.at(0) != "\t") {
processMonster(lines);
lines.str("");
}
lines << line << endl;
}
}