1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Problem with reading a int

Discussion in 'C++' started by Renzokusen, Jan 10, 2009.

  1. Renzokusen

    Renzokusen New Member

    Joined:
    Jan 10, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Ok I am simply trying to reduce the size of my item data base for this console rpg I am making. I wish to put all the creature names & hp & atk etc in a file then use the ifstream to retrieve this data when I need and place the values in certain variables. I am able to retrieve names, but when it comes to the int's I have problems of conversion.

    Here is my test code.

    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");
    }
    
    The test txt file

    Code:
    xxx ... ... ...
    xxz ... ... ...
    MONSTER_NAME King Fish Crab
    Weapon_Name Zanbato
    Weapon_Damage 100
    xzz ... ... ...
    
    Now I can display the name of the monster, weapon name, but the attack variable remains unchanged.

    Anybody know how to fix this?
     
  2. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    getline() will return including the integer value... so I think when we say in>>attack then it will try to read the next value which will be in the next line.. but we need int value which is already read from the file and is in the line...
    so instead of taking from file better to take from line variable...

    if( weaponDamage == line.substr(0, weaponDamage.length()))
    {
    //in >> attack;
    /* to get the position of space.. or we can do pos = strlen(weaponDamage); */
    int pos = line.find(" ");
    /* as pos will point to starting position of " " increamenting that to get the next position so that we will get the number.. */
    pos++;
    attack = atoi(line.substr(pos)); // converting string to integer

    }
    I think we can we try like this..
     

Share This Page