Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

?
Lv 5

C++ reading text files into a class?

Text file: (short version)

"Garden",0,0,0,2,0,0,0,0,0,0,10,0,"You are now inside the garden. Exits are to the west."

"North area",0,3,1,0,0,0,0,0,0,0,10,0,"You are in a large open area surrounded by shade trees. Exits are south and east."

"South area",2,0,0,0,0,0,0,0,0,0,10,0,"You are standing on a sidewalk. An exit lies to the north."

H file, holding the class

[code]

#ifndef __ROOMS_H__

#define __ROOMS_H__

class rooms

{

public:

rooms(){};

~rooms(){};

string getname(){return name;}

string getdesc(){return desc;}

int getnorth(){return north;}

int getsouth(){return south;}

int geteast(){return east;}

int getwest(){return west;}

int getnortheast(){return northeast;}

int getsoutheast(){return southeast;}

int getnorthwest(){return northwest;}

int getsouthwest(){return southwest;}

int getup(){return up;}

int getdown(){return down;}

int getaolight(){return aolight;}

int gettrap(){return aotrap;}

protected:

string name;

string desc;

int north;

int south;

int east;

int west;

int northeast;

int southeast;

int northwest;

int southwest;

int up;

int down;

int aolight;

int aotrap;

};

class idvroom : public rooms

{

public:

idvroom(string _name="",string _desc="",int _north=0,int _south=0,int _east=0,int _west=0,int _northeast=0,int _northwest=0,int _southeast=0,int _southwest=0,int _up=0,int _down=0,int _aolight=0,int _aotrap=0)

{name=_name;desc=_desc;north=_north;south=_south;east=_east;west=_west;northeast=_northeast;northwest=_northwest;southeast=_southeast;southwest=_southwest;up=_up,down=_down,aolight=_aolight;aotrap=_aotrap;};

~idvroom(){};

};

#endif [/code]

What is the best way to read the text file and it's values into the class ? Descriptions, and values will change, so it can't be static...

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    There is someting incorrect at the end of the code. Not sure

  • ?
    Lv 4
    4 years ago

    the most acceptable thanks to study something from a document is to apply serialization, it really is a set of mechanisms that enable an merchandise to be study or written to a circulate in binary, XML, JSON or, as you should opt for, textual format. yet it really is a extra superior situation. To do what you opt for i'd replace the lines that start up if ((myStream = openFileDialog1.OpenFile()) != null) to study something like: var lines = document.ReadAllLines(openFileDialog1.FileNa... empNUm = int.Parse(lines[0]); call = lines[a million]; address = lines[2]; also, as a area note, you've a lot code that makes use of member purposes to get admission to inner most documents, e.g. public void SetEmpNum(int a) { empNum = a; } public int GetEmpNum() { go back empNum; } it really is a piece of Java/C++ type it really is wiped clean up via houses in C++. you'll oftentimes replace the above lines with both: public int EmpNum { get { go back empNum; } set { empNum = fee; } } or get rid of empNum all at the same time and enable the compiler do the paintings and write: public int EmpNum { get; set; }

Still have questions? Get your answers by asking now.