-1
Bulbasaur|Grass| |2|16|45|65|65|45|0.059
Ivysaur|Grass|Poison|3|32|60|80|80|60|0.059
Venusaur|Grass|Poison| | |80|100|100|80|0.059
Torchic|Fire| |23|16|45|70|50|45|0.045
Combusken|Fire|Fighting|24|36|60|85|60|55|0.045
Blaziken|Fire|Fighting| | |80|110|70|80|0.045

These are some data in my text file, which stores Pokemon's name, type 1, type 2, index number of evolved Pokemon from the list, and stats, with character "|" as dividing lines, how do I read all hundreds of similar data into a 2D array? Or any other form of array which gives better result?

These are my C++ codes, and the result are totally failure.

ifstream inFile;
inFile.open("PokemonBaseStats.txt");
if (inFile.fail())
{
    cerr << "Could not find file" << endl;
}

vector<string> code;
string S;

while (inFile >> S) {
    code.push_back(S);

    inFile >> name >> type1 >> type2 >> evolveTo >> evolveLevel >> hp >> atk >> def >> spd >> catchRate;
    cout << name << type1 << type2 << evolveTo << evolveLevel << hp << atk << def << spd << catchRate;
}
system("PAUSE");
return 0;

The output is:-

output

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58

1 Answers1

1

I wrote a piece of code to do that but I'm not sure if it is exactly what you wanted:

        struct Pokemon
        {
        public:
            std::string Name;
            std::string Type1;
            std::string Type2;
            int IdxVector[6];
            float Stat;
        };

        int main(int argc, char *argv[])
        {
            std::string TxtLine;
            int count;
            std::ifstream F("./TextFile.txt");

            std::list<Pokemon> PList;

            while(std::getline(F,TxtLine))
            {
                Pokemon P;

                int idx=TxtLine.find_first_of('|');
                std::string Element=TxtLine.substr(0,idx);
                P.Name=Element;
                TxtLine.erase(0,idx+1);


                idx=TxtLine.find_first_of('|');
                Element=TxtLine.substr(0,idx);              
                P.Type1=Element;
                TxtLine.erase(0,idx+1);

                idx=TxtLine.find_first_of('|');
                Element=TxtLine.substr(0,idx);              
                P.Type2=Element;
                TxtLine.erase(0,idx+1);

                for(int i=0;i<6;i++)
                {
                    idx=TxtLine.find_first_of('|');
                    Element=TxtLine.substr(0,idx);              
                    P.IdxVector[i]=atoi(Element.c_str());
                    if(Element.compare(" ")==0)
                        P.IdxVector[i]=-1;
                    TxtLine.erase(0,idx+1);
                }
                P.Stat=atof(TxtLine.c_str());
                PList.push_back(P);
            }


            return 0;

        }
Afshin
  • 392
  • 2
  • 11