c++ - How to take input from a text file into an object and use it in another classes constructor -
im making game , been taken in text files. weapons,rooms,players etc... question how take in single weapon text file of weapons:
id: 1. weapon name: katana. damage: 20. weight: 6. id: 2. weapon name: longsword. damage: 17. weight: 9. id: 3. weapon name: waraxe. damage: 22. weight: 20. id: 4. weapon name: staff. damage: 9. weight: 6.
and add player classes constructor. code have far:
void weapons :: getweapon() { string filename = "weapons\\weapons.txt"; ifstream infile(filename); string garbage; int id; string weapon; int damage; int weight; while(infile>>garbage,infile>>id,infile>>garbage, infile>>garbage,infile>>garbage,infile>>weapon, infile>>garbage,infile>>damage,infile>>garbage, infile>>garbage,infile>>weight,infile>>garbage) { /*infile>>garbage; infile>>id; infile>>garbage; infile>>garbage; infile>>garbage; infile>>weapon; infile>>garbage; infile>>damage; infile>>garbage; infile>>garbage; infile>>weight; infile>>garbage;*/ cout << "id: \t\t\t"<< id << "\n"; cout << "weapon name: \t\t"<< weapon << "\n"; cout << "damage: \t\t" << damage <<"\n"; cout << "weight: \t\t" << weight << "\n"; weapons w1 (id,weapon,damage,weight); weaponslist.append(w1);//appends linkedlist } } player :: player(string firstname,string secondname,int level,int experience,int strength,weapons weapons) { this->firstname = firstname; this->secondname = secondname; this->level = level; this->experience = experience; this->strength = strength; this->weapons = weapons; } player :: player(string firstname,string secondname,int level,int experience,int strength,weapons weapons) { this->firstname = firstname; this->secondname = secondname; this->level = level; this->experience = experience; this->strength = strength; this->weapons = weapons; }
like how set players object particular weapon loaded in text file , put list.
thanks in advance.
Comments
Post a Comment