Long story short I am trying to pull 2 types of data from a file.
A string (which I have now got working) and a int. While these are easy on their own what I am trying to do is not so. I am trying to search through a file for a certain word then extract the information that follows that word. For example search for MONSTER_NAME then extract the rest of the line which holds the monster's name. I have gotten the last part to work, but the problem is when I want to get the WEAPON_DAMAGE and put the value into a int it doesn't work and the int's value remains unchanged.
Here is the code
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream in("happy.txt");
string line;
string key = "MONSTER_NAME";
string two = "Weapon_Name";
string weaponDamage = "Weapon_Damage";
string dataSought;
string weaponName;
int attack = 0;
// Read the label "attack" followed by the value
//_file >> label >> attack;
while( getline(in, line) )
{
//weaponDamage >> attack;
if( key == line.substr(0, key.length()) )
{
dataSought = line.substr( key.length()+1 );
cout << dataSought;
}
if( two == line.substr(0, two.length()) )
{
weaponName = line.substr( two.length());
cout << weaponName;
}
if( weaponDamage == line.substr(0, weaponDamage.length()))
{
in >> attack;
}
}
cout << attack;
cout << endl;
system("pause");
}
and here is the text file
xxx ... ... ...
xxz ... ... ...
MONSTER_NAME King Fish Crab
Weapon_Name Zanbato
Weapon_Damage 100
xzz ... ... ...
int attack remains at 0.
Now what I want is the program to searched for Weapon_Damage then set a int to the value that follows the word Weapon_Damage.
Sorry I'm just really bad using streams. Hopefully someone can help thanks.