Thread: word count problem

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    I think your right that this rule is the problem : typedef vector<double>::size_type vec_sz ;

    When I deleted that rule in the loop and put it before the while . It seems to work.

    Roelof

  2. #17
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by roelof View Post
    Hello Darren78,

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

    Roelof
    Start again, do each small bit and then test!

    Write the loop to read words and store in the vector. Check that this works by writing a test loop to print each word.

    Once that works, do the next part.

    I learn quicker by breaking each part down as small as I can and then coding and testing. If you write too much at once, you will not know where the issues lie and will get frustrated.

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think what you want to do is something like this:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main() {
    	vector<string> eg;
    	string data;
    
    	cout << "Enter all words then hit ctrl-D:\n";
    	while (!cin.eof()) {
    		cin >> data;
    		eg.push_back(data);
    	}
    
    	int i, len = (int)eg.size();
    	cout << endl;
    	for (i=0;i<len;i++) {
    		cout << (i+1) << ": " << eg[i] <<endl;
    	}
    
    	return 0;
    }
    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

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

    Not complety.

    What I try to do is count every distinct word which the user has input.

    Im now beginning again in very little steps so I can see what went wrong.

    Roelof

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, here is the output from that:
    [root~/C++] ./a.out
    Enter all words then hit ctrl-D:
    one two and then
    some more
    etc

    1: one
    2: two
    3: and
    4: then
    5: some
    6: more
    7: etc
    8: etc
    As you can see, for some reason "etc" is repeated twice. I'm pretty new to C++ too! I can maybe guess why that repetition is there tho; anyway, changing the while loop to this fixes that problem:
    Code:
    	cout << "Enter all words then hit ctrl-D:\n";
    	while (cin >> data) {
    		eg.push_back(data);
    		if (cin.eof()) break;
    	}
    I'm not trying to spoil anything for you roelof, just you seem to be having a lot of trouble with this and I thought a working example might be helpful.
    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

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

    Thank you for the tip and the help.
    tomorrow I will work on this project.
    It''s now late here.

    Roelof

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    I found the answer
    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 crtl-z enter on Windows: ";
    
        vector<string> words;
        vector<int> words_count ;
        typedef vector<double>::size_type vec_sz ;
        vec_sz size ;
    
        int found, place, teller ;
        string x ;
    
        while (cin >> x )
        {
            size = words.size ();
            for (teller =0; teller < size; teller ++)
            {
                if (x == words [teller])
                {
                    found = 1 ;
                    place = teller ;
                }
            }
            if (found == 1)
            {
                found = 0 ;
                words_count [place] +=1 ;
            }
            else
            {
                words.push_back (x) ;
                words_count.push_back  (1) ;
            }
        }
        size = words.size () ;
        for (teller= 0; teller < size; teller ++)
        {
            cout << "Het woord : " << words[teller] << " komt " << words_count [teller] << " voor in de input" << endl ;
        }
        return 0;
    }
    Any remarks on this solution.

    Roelof

  8. #23
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Looks good, the way words_count gets incremented is clever. Here's a way you could optimize it slightly by losing the "found" and "place" variables, so there is slightly less copying and testing:
    Code:
        while (cin >> x )
        {
            size = words.size ();
            for (teller = 0; teller < size; teller ++)
            {
                if (x == words [teller])
                {
    				words_count [teller] +=1 ;
    				teller = -1;
    				break;
                }
            }
    		if (teller < 0) continue;
    		words.push_back (x) ;
    		words_count.push_back  (1) ;
        }
    "Break" ends a loop (since it's done at that point) and "continue" proceeds to the next iteration, skipping the remaining code in the loop.
    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

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

    I get what you mean.

    But one thing I don't see.
    If you use continue there, are the data put into the vector.
    For me it don't seem so.

    Roelof

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, but the idea is that if teller is negative, then the word already exists in the vector, so you do not want to push it into the vector. Actually, instead of assigning -1 to teller, it would be simpler to make use of the fact that if teller == size, then the word was not found in the vector, hence you would replace the part of MK27's example that uses continue with:
    Code:
    if (teller == size)
    {
        words.push_back(x);
        words_count.push_back(1);
    }
    By the way, later you will learn that it is easier to use a std::map container to solve this problem, but for now this is good practice
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

    I get what you mean.
    If the teller reaches the value of the size then it's not found.

    Thank you for the tip.
    Im now trying the exercise where multiple students enter thier grades and I have to compute the average of every student.

    So this one is solved.
    Everyone thanks for the tip and the help.


    Roelof

  12. #27
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Actually, instead of assigning -1 to teller, it would be simpler to make use of the fact that if teller == size, then the word was not found in the vector, hence you would replace the part of MK27's example that uses continue with:
    Correct. For some reason I was thinking that if the match were to the last element teller would == size anyway but of course the break prevents that.

    Quote Originally Posted by laserlight View Post
    By the way, later you will learn that it is easier to use a std::map container to solve this problem, but for now this is good practice
    That's what I would have done, but I think in fact roelof's solution is more elegant and definitely more efficient -- sometimes less is more, syntax usage wise. A map would be a waste here.
    Last edited by MK27; 06-18-2010 at 08:55 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

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

    Maybe I could use a map but the book from which I try to learn c++ has not talked about maps.
    So Im not allowed to use maps.

    If I could maps , i would map.find to find out if a name is in the vector.

    Roelof
    Last edited by roelof; 06-18-2010 at 09:19 AM.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by roelof
    Maybe I could use a map but the book from which I try to learn c++ has not talked about maps.
    So Im not allowed to use maps.
    Yes, do not use them yet. It is just something to keep in mind for the future.

    Quote Originally Posted by MK27
    I think in fact roelof's solution is more elegant and definitely more efficient
    No, roelof's solution is less efficient in the long run: it performs a linear search of the existing words for each word, compared to the O(log n) search of the map for each word. As for elegant: that depends on your point of view, but the map solution is certainly more concise, even if one makes use of stuff like std::find instead of manually implementing the linear search.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    No, roelof's solution is less efficient in the long run: it performs a linear search of the existing words for each word, compared to the O(log n) search of the map for each word. As for elegant: that depends on your point of view, but the map solution is certainly more concise, even if one makes use of stuff like std::find instead of manually implementing the linear search.
    Okay, I thought you meant simply for the count, but obviously you would only need a single map, not a vector + map there. Silly me again!
    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

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, 10:12 AM
  3. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  4. Word COM problem
    By UnclePunker in forum Windows Programming
    Replies: 6
    Last Post: 01-06-2005, 11:51 AM
  5. MS Word Problem
    By golfinguy4 in forum Tech Board
    Replies: 8
    Last Post: 06-22-2003, 01:33 AM