title: How to read a list of names with ID numbers into a map c++
body: <p>Say I have:</p>
  
  <pre><code>Charlie Smith   134
  Alice Jones     245
  Bob Richards Hughes 325
  </code></pre>
  
  <p>in a data file and I want to read them into a map, how would I do this?</p>
  
  <p>This code works fine for just first names, not sure how to extend it to many names...</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;map&gt;
  #include &lt;iterator&gt;
  
  #define TRUE    1
  #define FALSE   0
  
  int main() {
      std::map&lt;std::string, int&gt; contents;
      std::map&lt;std::string, int&gt;::iterator it;
  
      bool repeat(true);
      std::ifstream infile;
      while (repeat) {
          repeat = false;
          std::cout &lt;&lt; "Enter the filename: " &lt;&lt; std::endl;
          std::string filename;
          std::getline (std::cin, filename);
          infile.open(filename.c_str());
          if (infile.fail()) {
             std::cerr &lt;&lt; "Invalid Filename" &lt;&lt; std::endl;
             repeat = true;
          }
      }
      while(!infile.eof()) {
          int a;
          std::string b;
          infile &gt;&gt; b &gt;&gt; a;
          contents[b]=a;
      }
      std::cout &lt;&lt; "\n";
      for (it = contents.begin(); it != contents.end(); it++) {
          std::cout &lt;&lt; it-&gt;first &lt;&lt; " " &lt;&lt;  it-&gt;second &lt;&lt; std::endl;
      }
      return 0;
  }
  </code></pre>