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.

>> in >> attack;
The line has already been read. So just convert the substr to an int similar to the previous string values attach = atoi(line.substr(weaponDamage.length()+1);

>> in >> attack;
The line has already been read. So just convert the substr to an int similar to the previous string values attach = atoi(line.substr(weaponDamage.length()+1);

I changed the code to

if( weaponDamage == line.substr(0, weaponDamage.length()))
        {
               attack = atoi(line.substr( weaponDamage.length()+ 1 ));
              
         }

but I get the same error

cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `const char*' for argument `1' to `int atoi(const char*)'

Or use istringstream instead of atoi().
Just hope that there is a position after line Weapon_Damage.

a position? The line within the text file is

Weapon_Damage 100

I simply just want to be able to get that 100 into a int.

oh I googled istringstream not sure how to use it? This is the only part of programming I hate. I just don't get streams at all.

I changed the code to

if( weaponDamage == line.substr(0, weaponDamage.length()))
        {
               attack = atoi(line.substr( weaponDamage.length()+ 1 ));
              
         }

but I get the same error

cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `const char*' for argument `1' to `int atoi(const char*)'

attack = atoi(line.substr(weaponDamage.length()+1).c_str()); fixes that problem, I were gonna mention it earlier just as AD posted.

As a side note it is also usually good to check if a file is open before attempting operations with the file usually something like

if (in.is_open())
{
while(getline(in, line)).... rest of code
}
else
cout << "Error Opening File\n";

attack = atoi(line.substr(weaponDamage.length()+1).c_str()); fixes that problem, I were gonna mention it earlier just as AD posted.

As a side note it is also usually good to check if a file is open before attempting operations with the file usually something like

if (in.is_open())
{
while(getline(in, line)).... rest of code
}
else
cout << "Error Opening File\n";

ah finally thank you. I've been wanting to finish implementing my games weapons/armor/creature, but I want them in files so they can be adjusted without recompiling. Thank you for the help everybody it works now.

As a side note it is also usually good to check if a file is open before attempting operations with the file usually something like

if (in.is_open())
{
while(getline(in, line)).... rest of code
}
else
cout << "Error Opening File\n";

That reminds, I don't see it closed after it's done.

That reminds, I don't see it closed after it's done.

:D You're right, I totally missed that... Alas I have good excuse at the mo considering I'm not very well at the moment so my head is abit fuzzy!

Well may aswell add to the thread a little & mention that system("PAUSE"); is bad practice also, better to use cin.get() or similar at the end of the program if you don't want it to just close.

This article has been dead for over six months. Start a new discussion instead.