strange results running a fstream loop

So I'm trying to count the number of words in a file in such a way that multiple spaces or breakliines will not effect my answer. The goal of the loop being to test each line of the file, find whitespace or a breakline after a word (any grouping of non whitespace characters) and when that happens adding to a wordcounter. however, for the file

This &%file should!!,...



have exactly 7 words.

for example, I get a wordcount of 5... all of my files were off by between 1-3 words, and I can't find any correlations between the text and the error margin.
are my parameters not logical?

edit: okay, fixed that problem, changed if((letter ==' ' || letter == '/n') to if((letter ==' ' || position== Length) where Length is InputString.length -1.

It now outputs the proper number
it refuses to loop however, so I can only check 1 file at a time, but I haven't given that issue much thought yet myself. I'll probably be able to fix that issue on my own, thanks Yanson.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
do
{
    cout << "enter filename you wish to count the words in "
         << "(file1.txt, file2.txt, file3.txt, file4.txt, or file5.txt) type quit to exit: ";
    cin  >> FileName;
    if (FileName == "quit")
        break;
    inFile.open(FileName.c_str());
    if (!inFile)
    {
        cout << "Error oppening file" << endl;
        return 1;
    }
    while(!inFile.eof())
    {
      getline(inFile, InputString);
      for(int position=0; position<InputString.length(); position++)
      {
          letter=InputString.at(position);
          if(letter!=' ')
          {
              letterBool = true;
          }
          if((letter ==' ' || letter == '/n') && letterBool == true)
          {
              letterBool = false;
              WordCount++;
          }

      }
      letterBool = false;
    }
    if (FileName != "quit")
    {
    cout << WordCount << endl;
    }
    FileName.clear();
    }
    while(true);

Last edited on
Please use code tags
http://www.cplusplus.com/articles/z13hAqkS/

letter will never equal '\n' getline does not store the delimiter

while(!inFile.eof()) is better written as while(getline(inFile, InputString))

http://www.cplusplus.com/reference/string/string/getline/?kw=getline
Last edited on
Don't use this structure:
1
2
3
    while (!inFile.eof())
    {
        getline(inFile, InputString);

Instead do this:
1
2
    while (getline(inFile, InputString))
    {


You could of course do this:
1
2
3
4
5
6
    string word;
    WordCount = 0;
    while (inFile >> word)
        WordCount++;    

    cout << "WordCount = " << WordCount << endl;

Job done.
Last edited on
will do, so that just automatically cuts off at the end of the file then?
Yes. Some operations such as extracting an integer from the file might fail for other reasons, but reading a word, or getting a whole line, will stop when the input fails, and it will fail when the end of the file is reached.
thank you, and well damn, that does do it, I must have missinterpreted the source material, I was led to believe that >> would jump to the next line after finding word, now I just feel silly for the over complicated method.
Topic archived. No new replies allowed.