# Thread: word count problem

1. ## 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

2. 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.

3. Oke,

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

Roelof

4. 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().

5. Ah, must be time for another roelof "feed me the answer" marathon.

6. 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

7. Code:
```int i, sz = words.size();
for (i=0; i<sz; i++) {
cout << words[i];
}```

8. O,

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

Roelof

9. 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. Originally Posted by roelof

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. 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. 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. 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. Hello Darren78,

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

Roelof

15. Originally Posted by roelof
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.

Popular pages Recent additions