Thread: Word Counting

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    15

    Angry Word Counting

    Hello all this is my first post here :-). I'm having trouble in a C++ program that I'm trying to write i have a text file with two lines in it that i've made up here are the following two lines.

    The dog goes bark.
    The cat goes meow.

    These two lines are in a txt file called test.txt.
    Here is the following code for this program that I cann't get to work.

    When i try to print the file it only prints out "goes bark and the full sentance the cat goes moew. I have spent countless hours on this program this weekend and I haven't figured one thing out about it. Please Help me..
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include "tstring.h"
    
    
    class WordCount
    {
    	private:
              String word;
              ifstream infile;
              int count;
              char c;
    
       public:
       	int ReadStr();
          int WriteStr();
          void CountWords();
    
    };
    
    int WordCount::ReadStr()
    {
       infile.open("test.txt");
    
    
           infile >> word;
    
       return 0;
       }
    
    
    int WordCount::WriteStr()
    {
      infile >> word;
    while(getline(infile, word, '\n'))
    {
     infile >> word;
    }
    cout << word << endl;
     return 0;
     }
    
     void WordCount::CountWords()
    {
    
    
     count= count+1;
    
    
    
    
    
       cout << "The number of words  in the file is: " << count << endl;
    
     }
    
    
    
    int main()
    {
    WordCount WC;
    char res;
    
    
    do
    {
    	cout << "1 Read String." << endl;
       cout << "2 Print String." << endl;
       cout << "3 Count Words." <<endl;
       cout << "4 Exit." << endl;
       cout << "Please Select an operation: " << endl;
    
    cin >> res;
    
    switch(res)
    {
    case '1':
       		WC.ReadStr();
             break;
    case '2':
    			WC.WriteStr();
             break;
    case '3':
    			WC.CountWords();
             break;
    case '4':
             cout << "Thanks for using my program." << endl;
    			exit(1);
    			break;
    default:
    	cout << "Invaild Option... Please Try again " << endl;
    }
       } while(res!='4');
    }
    Any type of help would be appreciated.
    Thanks,
    Achilles
    Last edited by Achillles; 09-08-2002 at 10:13 PM.

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Question

    Achillies,

    What in this header file??
    Code:
    #include "tstring.h"
    Also, does this program require the use of classes- if not keep it simple and do not use them. Then go back and use them.

    Mr. C
    Last edited by Mister C; 09-08-2002 at 10:24 PM.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    tstring.h allows me to use the String indentifier...

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Click on this link to see an example of a word counting program that shows the individual words along with a count of the number of times that word appears in a file. It is a tiny program by someone else that at first surprised me with its simplicity and is worth a quick look.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The >> operator has to be overloaded for the String class. Assuming it is implemented in the routine fashion, then it will ignore any leading whitespace and terminate at the first whitespace after the first non-whitespace. This behavior can be harnassed to count words as words are routinely separated by whitespace.

    Likewise, getline() needs to rewritten to work with String class. The syntax you use is similar to that used fro the STL string class, but I can't say whether is appropriate for the String class or not. Assuming the syntax is appropriate, then getline() should read all characters from the stream infile into the buffer word until it finds a newline character. I'm not sure whether you would want/should have a terminating size or not.

    In any event. Look at the following code snippet. I have commented every line so you can see what I think is happening, if my assumptions regarded implementation of the above istream methods for the String class is correct.


    infile >> word;
    //this means ignore any leading whitespace in the file, then read in the first word of the file before stopping

    //the following line means read in the rest of the first line of the file, then every line thereafter, one line at a time, untile the file is ended or getline() fails. We store the data in word, overwriting any previous data that may have been in word the last time through the loop
    while(getline(infile, word, '\n'))
    {


    infile >> word;
    //now we read in one word at a time again each time through the loop, overwriting anything placed there last time throgh the loop
    }

    cout << word << endl;
    //now we print out just the last piece of data stored in word

    To fix this you should use just >> or just getline() to read the file.

    First version: using just >>

    infile >> word;//read first word
    cout << word << endl;//display first word
    while(infile >> word) //as long as infile doesn't fail read in next word
    {
    cout << word << endl;//display next word;
    }


    Second version: using just getline()

    getline(infile, word, '\n'); //read first line of file
    cout << word << endl;//display first line of file
    while(getline(infile, word, '\n')//read each line file one at a time
    {
    cout << word << endl;//displaying them as you go
    }

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    Hey thanks guys that works now i just need to get the program to count the words in the file. Cool Thanks elad
    and everyone for your help. How should I go about the counting of the words ?? should i use for loop and a while loop inside the for loop ??? any type of information to lead me in the right way well agian me appreciated....
    Thanks
    Achilles

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    counting spaces maybe... but make sure you dont count multiple spaces in a row as multiple words...

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    Ok this is what i have so far
    Code:
     int WordCount::CountWords()
    {
    count = ' ';
    while(infile >> word)
    {
    
    count++;
    }
    
    
        cout << "The number words in the program are " << count << endl;
    
    
     }
    when i run this the count come to 32 and i'm wondering how can i change it, i know it's counting every character and space as a number
    when it's supposed to be 8 this is what the text file contains
    the dog goes bark.
    the cat goes meow.
    So how can i fix this minor problem anyone know ???

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You've initilized count to the ASCII value of a space, which is 32. It should be count = 0;

    So, you arn't executing the stuff in the while-loop. (Your while condition is never true.) I don't know why... I'll have to leave that for the experts... Got to get back to work.

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    you seem to be trying to mix methods of counting words. Reading in each word one by one will work, just make sure you use a buffer large enough to hold the largest word in the file (or just use string).

    Code:
    #include <string>
    ...
    string buf;
    int count = 0;
    
    while (cin >> buf)
       count++;
    
    cout << "there are " << count << " words in the file";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. word counting program
    By mashour06 in forum C Programming
    Replies: 2
    Last Post: 06-09-2009, 03:58 AM
  2. String - word counting!
    By shardin in forum C Programming
    Replies: 2
    Last Post: 07-11-2007, 05:08 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. follow up with char I/O and line, word, and char counting
    By Led Zeppelin in forum C Programming
    Replies: 1
    Last Post: 03-23-2002, 09:26 PM