Im trying to write a program that reads from a text file and then outputs the total words, most words per line, total bumber of lines and average words per line. The problem is that ive got a total mental block on how to get the most words per line thing working. All i can do so far is get it to be the same as the word count, and its starting to do my head inso any help will be appreciated. I get the feeling that its one of those things where ill kick myself once i know the anwer.
Thanks for your help, by the way i know its a bit messy but ill tidy it up when finished lolCode:#include <iostream> #include <fstream> #include <sstream> #include <string> using namespace std; void main(int argc, char **argv) { // make sure that the user has specified a filename if(argc < 2) { cout << endl << "You must enter a filename" << endl << endl; return; } string filename = argv[1]; ifstream fin(filename.c_str()); // define our file input stream // make sure that the file exists if(!fin) { cout << endl << "Cannot find file" << endl << endl; return; } /******************************************************************** Process the text data file ********************************************************************/ cout << endl << "Reading input file: " << filename << endl << endl; int words = 0, max = 0; // to count words and longest line string temp; for (int line = 0; !fin.eof(); line ++) { // for each line in file ... getline(fin, temp); // get the line into temp string istringstream sstr(temp); // create string stream tokeniser while (sstr >> temp) { // for each word from line ... words++; // increment word count for line max++; // update total words so far } // update longest line if latest was longer } /******************************************************************** Output the stats ********************************************************************/ cout << endl << "Total lines:" << line ; cout << endl << "Total words:" << words ; cout << endl << "Max line: " << max << " words" ; cout << endl << "Average: " << (words/(double)line) << " words per line" ; cout << endl << endl ; }