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.
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.
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>?
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).
for(constauto& 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.