word count problem

This is a discussion on word count problem within the C++ Programming forums, part of the General Programming Boards category; Hello, For a exercise I must make a programm which counts how many times a word is being inputted. It's ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    word count problem [ solved]

    Hello,

    For a exercise I must make a programm which counts how many times a word is being inputted. It's a exercise from chapter 3 of Accelerated C++

    So i thought this will work :
    Code:
    #include <string>
    #include <vector>
    #include <iostream>
    
    using namespace std ;
    
    int main()
    {
    	// ask for and read the words to count
    	cout << "Enter all the words, "
    	        "followed by end-of-file: ";
    
    	vector<string> words;
    	vector<int> words_count ;
    	int found, place, teller=0 ;
    	string x ;
    
    	while (cin >> x)
    	{
    		for (teller ; words.begin(); words.end())
    		    if (x == words[teller] then
                     {
                         found = "1"
                         place = teller
                     }
              if found==1 then
                {
                    words_count [place]+=1 ;
                    found = 0 ;
                }
                else
                {
                    words.push_back(x);
                    words_count.push_back(1)
                }
    	}
    
    	for (teller; words.begin();words.end())
    	{
    	    cout << "Word" << words [teller] << "komt" << words_count[teller] << "voor";
    	}
    	return 0;
    }
    But I get a problem with the for loop.

    Who can help me and tell me what's wrong with this line.

    Roelof
    Last edited by roelof; 06-18-2010 at 08:31 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    		for (teller ; words.begin(); words.end())
    Okay, so your initialization statement is just "teller", which does not do anything. It's just the name of an existing variable. Perhaps you are thinking this registers the variable for some purpose in the loop? I dunno. Anyway, as is it's meaningless.

    Then your iteration condition (the one which, if false, will end the loop) is just "words.begin()". I did not check this, but I think even an empty vector will return an iterator, so this is always true. Meaning the loop will be infinite.

    Finally, your update statement, which is an action to take every iteration (such as "i++") is just "words.end()". So that is meaningless too -- it doesn't do anything.

    What is it you want this loop to do? Here's a simple for loop:
    Code:
    int i;
    for (i = 0; i<10; i++) {
         count << i;
    }
    The initialization statement sets i to 0.
    The condition means the loop will end when i is no longer less than 10.
    The update will add 1 to i every iteration.
    Last edited by MK27; 06-17-2010 at 09:24 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    How can I make then a loop which begins at words.begin and ends at words.end ?

    Roelof

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    vector<string>::iterator it = words.begin(), end = words.end();
    while (it != end) {
          cout << *it << endl;
          it++;
    }
    You cannot add to the vector this way, for that you need to use .push_back().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Ah, must be time for another roelof "feed me the answer" marathon.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello MK27.

    Oke, I get this biut in the course that I follow are iterators chapter 5 and I work now in chapter 3.

    Therefore I thought using a loop or can't it be without using iterators.

    Roelof

    Edit :

    I tried this :
    Code:
    while (cin >> x)
    	{
    	    typedef vector<double>::size_type vec_sz ;
    	    vec_sz size = words.size ();
    But now vec_sz is not declared in this scope
    Last edited by roelof; 06-17-2010 at 10:41 AM.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    int i, sz = words.size();
    for (i=0; i<sz; i++) {
         cout << words[i];
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    230
    O,

    I understand from the book that only a vector can hold the size of a vector.
    So I misunderstood it ?

    Roelof

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    I did this problem a short while ago from the same book.

    I can't remember exactly how I did it off the top of my head (trying to watch football as well )

    Essentially the steps I took were:

    Read in words and store in vector
    Initialise counter
    Sort vector
    loop vector (if [i] == [i+1]) ++ counter.
    else output word and counter and then reset counter.

    The problem I had, which I had to think about was the last couple of elements within the vector. You will know what I mean when you get there.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by roelof View Post

    I tried this :
    Code:
    while (cin >> x)
    	{
    	    typedef vector<double>::size_type vec_sz ;
    	    vec_sz size = words.size ();
    But now vec_sz is not declared in this scope
    By declaring size within the while loop, you are doing so on each loop, ie, each time you read a word. That's not what you want.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I solved that one.
    But what i was trying was to have the user have inputs till the user inputs EOF.
    But as x is a string EOF is not reconized as end of file.

    So back to the drawing board.

    Roelof

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello Daren,

    That's what I want because when a new word is entered the vector is 1 bigger.
    So I have to check that.

    Roelof

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    EOF means when there is no more input to read. I will assume that you are using windows and if so, for now, when you have entered all the words that you want to, press ctrl-z which will EOF.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello Darren78,

    I tried that and then the code chrashes.
    Also i tried "112" as stop command and then the same happens.

    Roelof

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by roelof View Post
    Hello Daren,

    That's what I want because when a new word is entered the vector is 1 bigger.
    So I have to check that.

    Roelof
    Okay, so read all input and enter into the vector within the loop and then you can get the size once all input is read.

    BTW. if you are having difficulty following Accelerated CPP, which I do at times, then I would recommend Bjarne Stroustrups book....just Google, Programming Principles C++, I find it explains things in more depth with more examples.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Replies: 1
    Last Post: 03-06-2005, 09:12 AM
  3. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM
  4. Word COM problem
    By UnclePunker in forum Windows Programming
    Replies: 6
    Last Post: 01-06-2005, 10:51 AM
  5. MS Word Problem
    By golfinguy4 in forum Tech Board
    Replies: 8
    Last Post: 06-22-2003, 01:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21