Computer Science Canada

counting number of words using first_of() and first_not_of()

Author:  a22asin [ Mon Nov 03, 2014 6:05 pm ]
Post subject:  counting number of words using first_of() and first_not_of()

Hi, im having trouble trying to count the number of word per line from a file using first_of() and first_not_of(). I am to assume that each word is separated by a space or a tab (\t). I have managed to get each line and count each line; but i have no idea how i would use the two codes to find and count each word per line. can u please help me by showing me how i would do it (im a visual learner). Thanks.

My Code so Far:

code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
        string line;
        int numLines = 0;
        ifstream file ("horton.txt");
        if (file.is_open()){
                while ( getline (file,line) ){
                        ++numLines;
                }
        file.close();
        cout <<numLines<<endl;
        }
        else cout << "Unable to open file";
}


: