title: C++ program need lines words and characters count
body: <p>I have an assignment where i need to count lines, words and characters from a file. I'm having a problem counting the right amount of characters and words since if it gets doubled space it counts like a character and a word.</p>
  
  <p>the output should be</p>
  
  <p>Example </p>
  
  <pre><code>lines  words  characters  filename
  
   3     5        29        testfile
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;cstring&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  
  int main(int argc, const char * argv[]) {
  string lines, words, chars, file1, file2;
  
  ifstream infile;
  ofstream outfile;
  char c;
  
  int countLines = 0;
  int countChars = 0;
  int countWords = 0;
  cout&lt;&lt; "Enter the file name" &lt;&lt; endl;
  cin &gt;&gt; file1;
  infile.open(file1.c_str());
  
  
  while(!infile.eof())
  {
      if(infile.peek() == 1)
          break;
      c = infile.get();
      if(c != '\n')
         countChars++;
      else
          countLines++;
  
      if(c == ' '|| c =='\n')
          countWords++;
  
  }
  
    //  countChars = countChars - countWords;
  
  
  
  cout &lt;&lt; setw(12) &lt;&lt; countLines &lt;&lt; setw(12) &lt;&lt; countWords &lt;&lt; setw(12) &lt;&lt; countChars  &lt;&lt; endl;
  
  infile.close();
  
  
  
  return 0;
  }
  </code></pre>