title: A c++ program to count word
body: <blockquote>
    <p>Write a program that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a “word” to be any text that is surrounded by whitespace (for example, a space, carriage return, newline, etc.) or borders the beginning or end of the file.</p>
  </blockquote>
  
  <p>During compiling of the program, when I input the <a href="http://i.stack.imgur.com/WnjRV.png" rel="nofollow">file</a> which I put some words inside, I keep getting "0 word" for the output. Can anyone tell me what's wrong with it?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  const string filename="words.txt";
  const string PUNC=".?!,;";
  
  int main()
  {  
  int numWords, numChars;
  string word;
  
  cout &lt;&lt; "Enter filename:" &lt;&lt; endl;
  std::string filename;
  cin &gt;&gt; filename;
  
  ifstream in_stream;
  in_stream.open(filename.c_str());
  
  if(in_stream)
  {
      int numWords=0, numChars=0;
      string word;
  }
  
  cout&lt;&lt; fixed &lt;&lt; setprecision(0);
  
   while(in_stream &gt;&gt; word)
  {
      ++numWords;
      int len=word.size();
  
      if(PUNC.find(word[len-1])!=string::npos)
          word.erase(len-1);
      numChars+=word.size();
  
  
  }
  
  in_stream.close();
  
  cout &lt;&lt; "This is my \"slash\" &lt;--" &lt;&lt; endl; 
  if(numWords&lt;=1)
  {
  cout &lt;&lt; "There is " &lt;&lt; numWords &lt;&lt; " word in the file " &lt;&lt; filename &lt;&lt; "." &lt;&lt; endl;
  }
  
  else{
      cout &lt;&lt; "There are " &lt;&lt; numWords &lt;&lt; " words in the file " &lt;&lt; filename &lt;&lt; "." &lt;&lt; endl;
  
  }
  
  return 0;
  }
  </code></pre>