trouble counting words in a file

the program is supposed to read a file specified by the user, and count the number of words. the program is to loop until the user types in 'quit'. The loop and exit functions are working, but my counter is always off by a couple of numbers. I know that my loop is not counting a word if there is no space after it, such as in the end of a file. The professor requires us to do the word count by comparing individual characters and not strings. the example data sets are below.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//


#include <iostream>
#include <cassert>
#include <fstream>


using namespace std;

int main ()
{
    
    string astring;
    ifstream infile;  
    
    string filename;
    
    int wordCount = 0;
    
    char currChar;
    char prevChar; 
    
    
    
    while (1>0) {
    cout<<"what is the file name? (type quit to stop)";
    cin>>filename;
        infile.close();
        infile.clear();
    
        if (filename == "quit") {
            exit(1);
        }
            
    infile.open(filename.c_str());   
    
    assert(infile);
    
    infile>>astring;
        wordCount=0;  //reset wordcount
        infile.get(prevChar);       // Initialize previous value
        infile.get(currChar);       // Initialize current value
        while (infile)              // While input succeeds . . . 
        {
            if ((currChar == ' ' && prevChar != ' ' && prevChar != '\n') || (currChar == '\n' && prevChar != ' ' && prevChar != '\n'))
                wordCount++;                
            prevChar = currChar;      
            infile.get(currChar);     
        
        }
        
        infile>>astring;
    
        cout<<"wordcount = "<<wordCount<<endl;
        infile.close();
        infile.clear();
    }
        
 }
    


data1
1
2
3
4
5
This &%file              should!!,...



have exactly 7 words.


data2
1
2
3
4
5
This is a &%file    that          should!!,...   



have exactly 10 words.


data3
1
2
3
4
5
This is a &%file    that          should!!,...


This file must have several spaces at the end of the file.
have 21 words.   


data4
1
2
3
4
5
6
This &%file    that          has!!,...   

'

This file must have several spaces at the end of the file.
19 words.    


data5
1
2
3
4
5
6
7
8
9
10
11
Mr. &%Brown    can          moo!!,...   

'

can you?  This file should have several blank lines before the end of the file.

22 words.



 


example from professor
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
//***********************************************************
// This program counts the occurrences of "!=" in a data file
//***********************************************************
#include <iostream>
#include <fstream>            // For file I/O
using namespace std;
 
int main()
{
  int    count;               // Number of != operators
  char   prevChar;            // Last character read
  char   currChar;            // Character read in this iteration
  ifstream inFile;            // Data file
 
  inFile.open("myfile.dat");  // Attempt to open file
  if ( !inFile )
  { // If file wouldn't open, print message, terminate program
    cout << "Can't open input file" << endl;
    return 1;
  }
  count = 0;                  // Initialize counter
  inFile.get(prevChar);       // Initialize previous value
  inFile.get(currChar);       // Initialize current value
  while (inFile)              // While input succeeds . . . 
  {
    if (currChar == '=' &&    // Test for !=
        prevChar == '!')
      count++;                // Increment counter
    prevChar = currChar;      // Update previous value to current
    inFile.get(currChar);     // Get next value
  }
  cout << count << " != operators were found." << endl;
  return 0;
}
Also, when I run it on Dev-C++ (I know, but my teacher requires it), I get an error message:

"Assertion failed: infile, file (then lists the C: name)

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information."


Help?
I guess you miss the first and the last words.

<1> Remove line 40 and 53. Why to you read word into a string? This eats the first word.
<2> your loop-terminating condtition on line 24 condition has problem. As in data1, the last character is '.' (you won't get a white space character into currChar), thus the last word won't be counted. So you should take of 'end of file' condition into account.

Topic archived. No new replies allowed.