Thread: Word Counting

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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