Thread: count words

  1. #1
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29

    Question count words

    i am currently workin through the thinking in c++ ebook 2nd edition, at the end of the 2nd chapter i get the following taski:::

    Create a program that opens a file and counts the whitespace-separated words in that file....

    this is the program i wrote, but i just dont think this s right... is there any keyword that i can use to count words? (i know i can use strlen, but that would only give number of caghracters and not words???)


    Code:
    #include<vector>
    #include<fstream>
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
    	vector<string> vect;
    	string norm;
    	cout<<"This program will count the words in a file.\n";
    	ifstream count("exe3.txt");
    	while(count>>norm)
    	vect.push_back(norm);
    	for(int ln=0;ln<vect.size();ln++)
    	cout<<ln<<": "<<vect[ln]<<endl;
    	cin.get();
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, seems you should output vect.size() to the console and not the list of words. You don't need to store the words at all really, just have a counter variable that gets incremented in the while loop and then display that counter's value at the end.
    "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

  3. #3
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    how do i o that?

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Try counting white-space characters. Every word is followed by one (except the last). So increment only if the current byte is a whitespace, and the previous one wasn't. That's my idea anyways
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    unsigned counter = 0;
    while( count >> norm ) ++counter;
    cout << "There were " << counter << " words found." << endl;
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help, i cant count spaces or words
    By cdosrun in forum C Programming
    Replies: 2
    Last Post: 11-25-2005, 03:13 PM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. Replies: 2
    Last Post: 05-05-2002, 01:38 PM