Need help getting word count to work in my program here is my code so far but need a count for each line and how many words total at the end.
Thank you in advance.
Code:#include <iostream> #include <fstream> using namespace std; #include <iostream> //for the input output stream #include <fstream> //for file stream #include <cstring> //for strtok using namespace std; //all objects are under namesapce std int main(){ int wordCount = 0; //to store word count char fileName [81]; //to store file name cout << "\nInput File Name : "; cin.getline(fileName,150); //to get the file name from user ifstream file(fileName);//open the file for input while(!file.eof()){ //continue to end of file char line [150]; //to store a line char* word; //to store a word file.getline(line,150); //get a line from file word = strtok(line," "); //get a pointer to string till space while(word){ //till getting a null word = strtok(NULL," "); //keep getting string cout << word << endl; //print the word wordCount++; //increment wordCount } } cout << "\nTotal words in file" << fileName << " are " << wordCount; printf("Press ENTER a few times to terminate the program"); fflush(stdout); getchar(); getchar(); getchar(); getchar(); getchar(); getchar(); getchar(); getchar(); return 0; }



LinkBack URL
About LinkBacks


