title: Use of getline to parse and store variables
body: <p>I'm writing a simple sandbox text-based game in c++ and have just got around to save files. Having looked at a few of the questions on this site and others I opted to do my own rather that use the boost libraries etc.</p>
  
  <p>My save code works fine however, my load code isn't working like I want! </p>
  
  <pre><code>int loadGame(string filename){
  string delim=":";
  string readline;
  
  ifstream filestream;
  filestream.open(filename.c_str());
  
  while(getline(filestream,readline)){
      cout &lt;&lt; readline &lt;&lt; endl;
      stringstream linestream(readline);
  string         data;
  getline(linestream, data, ':');
  linestream &gt;&gt; playername &gt;&gt; location[0] &gt;&gt; location[1] &gt;&gt; hitpoints &gt;&gt; coins &gt;&gt; axes &gt;&gt; swords &gt;&gt; food &gt;&gt; maps;
  }
  return 0;
  }
  </code></pre>
  
  <p>It currently loops, as you would expect but then stores only the last 'maps' variable to playername.
  I understand that the <code>while(getline(filestream,readline)){</code> is the cause of the problem but as a newbie I can't see how to work around this without adding in a loop with lots of ifs that does an
  (pseudo code)</p>
  
  <pre><code>while(n&lt;10){
  if n=1{ linestream &gt;&gt; playername}
  if n=2{linestream &gt;&gt; location[0]...
  </code></pre>
  
  <p>which seems to me to be very drawn out and inefficient.</p>
  
  <p>A sample save might look like this:</p>
  
  <pre><code>playername:John
  location[0]:400
  location[1]:840
  hitpoints:100
  coins:35
  axes:0
  swords:0
  food:0
  maps:3
  </code></pre>
  
  <p>I know this is probably badly worded but I'd appreciate any help on this problem. Thanks in advance.
  ps any other tips on the code posted here would be great :)</p>