Thread: Max Word Count

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    Max Word Count

    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.

    Code:
    #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 ;
    
     
    
    }
    Thanks for your help, by the way i know its a bit messy but ill tidy it up when finished lol

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Havn't looked at your code to much because I stopped at the void main. main is not void it should be int main.
    As for your problem you could just create a variable(bigLine for ex) to hold the current line number you are reading then compare the line you just read to the last one you just read and if its bigger than that you change bigLine to that line number
    Woop?

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    ok so what if i did leave main at say void. What would that mean? Im new to C++ as i used to use java.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It would mean you should click on the FAQ link and go read why it's wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Hello,

    Along-side with the FAQ, here is more documentation of why returning main() as void is undefined.

    In the C language, the void main() construct is undefined. C++ somewhat is safer in this regard; any definition of the main function whose return type is other than int requires a diagnostic. However, the behavior is still ultimately undefined.

    Here are 3 key points why void main() is undefined:
    • The standard says so.
    • The startup routines that call main() could be assuming that the return value will be pushed onto the stack. If main() does not do this, then this could lead to stack corruption in the program's exit sequence, and cause it to crash.
    • You are likely to return a random value to the invokation environment. This is bad, because if someone wants to check whether your program failed, or to call your program from a makefile, then they won't be able to guarantee that a non-zero return code implies failure.

    Remember, declaring a function as void does not merely shut off or rearrange warnings: it may also result in a different function call/return sequence, incompatible with what the caller (in main's case, the C run-time startup code) expects. The ANSI standard says "no" to void main(), which should be an end of it.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Thank you, still cant seem to get most words per line working though-i know its something simple and i think its something i know i should be able to do which is why it is so annoying

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about creating a program to find the largetst value in an array? Once you have a handle on that, try adapting that method to work with what you want to do?

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Ok i now realise i have to create a new variable-say int words_per_line and assign that to max if it is bigger with each line. But my question now is that in the code i have, how do i take it on a line by line basis, because all it seems to do is read it all in and i want to be able to have a count at the end of the line.

    Code:
    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
    		
    		
    
          }
    I think its because of the while loop i have used but i cant think of another way of doing it. Any help would be appreciated

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    If you set 'words' to zero before counting the words in each line, then when the while loop is finished you would know exactly how many words were in only that line. If its greater than the current max line, update the current max otherwise don't.

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Right ive sorted it-well kind of. The problem now is that unless the return key is pressed at the end of a line, the words per line continues to count unless return has been pressed. This means that when word wrap is turned on, the number of lines is calculated correctly, but the words per line is not, as it is really a words till return key is pressed calculation. Does anyone know how i could solve that given this code:
    Code:
          // 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
    
    		 for (int wc=0; sstr>>temp; wc++) {
    									// for each word from line ...
    									// increment word count for line
    		words++;
    									// update total words so far
    		 }	
    		 if (wc>max) {
    			 max = wc;
    		 }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bintree and count (withouth using template)?
    By cubimongoloid in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2009, 06:22 AM
  2. word count
    By unity_1985 in forum C Programming
    Replies: 3
    Last Post: 07-29-2007, 10:34 AM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. word count troubles
    By Hoser83 in forum C Programming
    Replies: 13
    Last Post: 02-09-2006, 09:12 AM
  5. Replies: 5
    Last Post: 09-28-2004, 12:38 PM