Repetitions of words

I found this exercise and I don't know how to continue.

Write a sequence of words that ends with **END** on keyboard.
Calculate how many times a word is repeated and print to video only words that are repeated at least once.

Example:
Input:
Hello
My
Name
Is
Name
LOL
LOL

Output:
Name 2
LOL 2

This is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#define N 200

using namespace std;

int main ()
{
	string word;
	string buff[N];
	int i=0;
	cout<<"Write the words."<<endl;
	do
	{
		cin>>word;
		buff[i]=word;
		i++;
	}while(word!="**END**");
	
	return 0;
}


I don't know how to count the words I write. I tried with a "for" but I ended up with nothing but errors.
Can someone explain me how to do it?

EDIT:
This is the code with the "for" too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#define N 200

using namespace std;

int main ()
{
	string word;
	string buff1[N];
	string buff2[N];
	int i=0,j=0,count=0;
	cout<<"Write the words."<<endl;
	do
	{
		cin>>word;
		if(word!="**END**")
		{
			buff1[i]=word;
			buff2[j]=word;
			i++;
			j++;
		}
	}while(word!="**END**");
	int k,h;
	for(k=0;k<i;k++)
	{
		for(h=0;h<j;h++)
		{
			if(buff1[k]==buff2[h])
			{
				count++;
			}
		}
		if(count!=1)
		{
			cout<<buff1[k]<<" "<<count<<endl;
			count=0;
		}
	}
	
	return 0;

But it writes always the words, even if it has been written before.
Last edited on
1) You do not reset your count, so as soon as it reaches 2 all following words will be printed.
2) You do not actually need 2 arrays. One is enough.
3) You do not have any check on if word was written before.

The best approach is to use std::map:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <map>

int main()
{
    std::map<std::string, int> dictionary;
    std::string input;
    while(std::cin >> input && input != "**END**")
        ++dictionary[input];
    for(const auto& e: dictionary)
        if(e.second > 1)
            std::cout << e.first << ' ' << e.second << '\n';
}
http://ideone.com/SGYqQ9

Alternatively you can have a structure representing a word and amount of entries. After reading word you need to check if it is already in array. If so, increment amount of entries, else add it to the end with 1 entry
I didn't know about the library map.
The code you wrote gives me these errors:

In function 'int main()':
[Error] ISO C++ forbids declaration of 'e' with no type [-fpermissive]
[Error] range-based 'for' loops are not allowed in C++98 mode
[Error] request for member 'second' in 'e', which is of non-class type 'const int'
[Error] request for member 'first' in 'e', which is of non-class type 'const int'
[Error] request for member 'second' in 'e', which is of non-class type 'const int'

However, is there a way to do the exercise without the library <map>?
[Error] range-based 'for' loops are not allowed in C++98 mode
That is the problem. You need to enable C++11 mode in your compiler. How to do that differs depending on compiler/IDE
Could it be my version too? I have Dev C++ 5.5.1
I searched for latest version and I saw that they are at 5.7.1
http://www.cplusplus.com/doc/tutorial/introduction/devcpp/
For project compilations, go to: Project >> Compiler >> Code Generation >> (set 'Language standard' to a C++11 option)
If you do not have this option, update your instalation
Okay! Thanks! Problem solved! :D
But can you explain me how the "for" statment work there in the code? I've never seen that before(I'm new to C++ so I don't know so many things).
It is range based for loop:
http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html
http://en.cppreference.com/w/cpp/language/range-for
http://www.cplusplus.com/doc/tutorial/control/ (scroll down a little)

for(const auto& e: dictionary) tells "iterate over all elements in dictionary assigning each to e in order". auto means that type will be deduced automatically (BTW it is std::pair<std::string, int>); const & means that we want a constant reference to elements (avoiding copy, const-correctness)

e is a pair, so it has two members: first (string in our case — word stored) and second (int — amount of repetitions)

Then we have check if it encountered more than once and output.
Oh great! Thank you! :D
Topic archived. No new replies allowed.