title: Putting words from a file into a hash map (c++)
body: <p>So, I have a text file that is quite long (10k+ words) and I am trying to put each unique word into a hash map using the standard map library.</p>
  
  <p>I have a while loop reading each word from the file. The problem is, this while loop NEVER seems to end. I even put an if statement in the loop so that if it reached the eof() it would break the loop. It still does not end. Here is my code thus far:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  #include &lt;cctype&gt;
  using namespace std;
  
  
  string lowerCase(string isUpper);
  
  void main()
  {
  //create hash map
  map&lt;string, int&gt; stringCounts;
  
  //temp string
  string nextString;
  
  //import file/write file
  ofstream writeFile;
  ifstream gooseFile;
  
  //open file to read from
  gooseFile.open("goose.txt");
  if (gooseFile.is_open()) {
      //read file word by word
      while (gooseFile &gt;&gt; nextString) { //WORKS DO NOT CHANGE
          //check for punctuation
          for (int i = 0; i &lt; nextString.length(); i++) { //WORKS DO NOT CHANGE
              if (nextString[i] == ',' || nextString[i] == '!' || nextString[i] == ';' || nextString[i] == '-' || nextString[i] == '.' || nextString[i] == '?' || nextString[i] == ':' || nextString[i] == '"' || nextString[i] == '(' || nextString[i] == ')' || nextString[i] == '_' || nextString[i] == '\'') {
                  nextString.erase(i, i);
                  i--;
              }
          }
          //put all into lowercase
          nextString = lowerCase(nextString); //WORKS DO NOT CHANGE
          //cout &lt;&lt; nextString &lt;&lt; endl;
  
          //increment key value
          stringCounts[nextString]++;
  
          if (gooseFile.eof())
              break;
      }
  }
  
  //close current file
  gooseFile.close();
  cout &lt;&lt; "I GOT HERE!";
  //now print to an output file
  writeFile.open("output.txt");
  if (writeFile.is_open()) {
      cout &lt;&lt; "ITS OPEN AGAIN";
      //write size of map
      writeFile &lt;&lt; "The size of the hash map is " &lt;&lt; stringCounts.size() &lt;&lt; endl;
      //write all words in map
      //create iterator
      map&lt;string, int&gt;::iterator i = stringCounts.begin();
      //iterate through map 
      while (i != stringCounts.end()) {
          writeFile &lt;&lt; "The key and value is : (" &lt;&lt; i-&gt;first &lt;&lt; "," &lt;&lt; i-&gt;second &lt;&lt; ")\n";
          i++;
      }
  }
  else
      cout &lt;&lt; "CANT OPEN\n";
  }
  
  
  string lowerCase(string isUpper)
  {
      string toReplace = isUpper;
      for (int i = 0; i &lt; toReplace.length(); i++) {
          if (toReplace[i] &gt;= 65 &amp;&amp; toReplace[i] &lt;= 90) {
              toReplace[i] = tolower(toReplace[i]);
          }
      }
      return toReplace;
  }
  </code></pre>