Scope. Mr D's lines 33-34 are within the inner loop.
Frankly, there should not be any inner loop, but exploit the map::find instead.
Pseudo:
* We have taken the next character (x)
* There are exactly two possibilities:
1. The map has key x. Increment the value of that element.
OR
2. The map does not have key x yet. Emplace( x, 1 ) to the map.
Well, there is also third, optional possibility:
0. This is not a character that we will count (e.g. a space).
Style issue:
The letterCounter takes the map as reference, but the caller (main) does not care about the map. The "counting" function does some printing too (a "side effect"). One function, one task.
On the words: my test input was "foobar" and the program says: "words 0".
There is a monster too; what is seriously scary in this:
1 2 3
|
const std::string input = "foobar";
unsigned int index = 0;
std::cout << input[ index - 1 ]; // Which character is this?
|
Hint: see "exception safety" in
http://www.cplusplus.com/reference/string/string/operator%5B%5D/