c++ - Problem loading object data from text file -


Why does not this code load all values ​​from a text file? It only loads objects of the first object, but after that it is getting weird. Here's the code:

  #include & lt; Iostream & gt; # Include & lt; Fstream & gt; # Include & lt; Cstdlib & gt; using namespace std; Class space {public: location () {locName = "no name"; LocDesc = "no description"; LocID = 0; } Friend Ostream & amp; Operator & lt; & Lt; (Ostream & outs, const space & showMe) {// keeping the output is the same as input ... outs & lt; & Lt; ShowMe.locName & lt; & Lt; Endl; Exclusion & lt; & Lt; ShowMe.locDesc & lt; & Lt; Endl; Exclusion & lt; & Lt; ShowMe.locID & lt; & Lt; Endl; Return boycott; } Buddha Iron & amp; Operator & gt; & Gt; (Istream & amp; IN, location and Inococ) {getline (IN, inLoc.locName); Gateline (inLoc.locDesc); INS & gt; & Gt; InLoc.locID; Return INS; } Private: String Locum; String lockska; Int locID; }; Int main () {ifstream inFile; InFile.open ("loc_data.txt"); If (inFile.fail ()) {cout & lt; & Lt; "\ NInput file should not be lost!"; Exit (1); } List of location files [10]; Int i = 0; While (inFile.good ()) {inFile & gt; & Gt; FileLoc [i]; I ++; } For (int j = 0; j and lt; 10; j ++) coat & lt; & Lt; FileLoc [ja]; Return 0; }   

Input file is:

  Main place for town hall work 1 city park exterior 2 train station commanding point 3 local airport long distance Travel 4   

And the output is:

  Main place for the Town Hall Festival 1 city park 0 No Name No Description 0 No Name No Description 0 No Name No Description 0 No Name Description 0 No Name Description 0 No Name Description 0 No Name Description 0 No Name I have a doubt that the mistake is responsible for this, but not sure enough to ensure that, but I would really like to know why this is happening and only Not a 'right', no.   

Your first problem is that this line does not remove the new line:

  ins & gt; & Gt; InLoc.locID;   

This means removing the remaining line after 1 location in getline > - An empty string for the name - that is

This means that you are quickly coming out of sync with the lines you want to read.

When you loop while you are standing, you check that the stream is "good" before removing a new location, but you do not check whether the clearance is successful or not Believe that extraction works.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -