I'm going to take a guess that it's your for() loop conditions. It's a newbie off-by-one error. Suppose there are
n characters in the string. Your for() loop is accessing 0, 1, ... , n-1, n. But if you count the elements of this sequence, you'll see that there are n+1 of them! Something's amiss, no?
Rule of thumb is that if
n is the number of elements you want to iterate over, you start at 0 and continue while you are
strictly less than n.
What you should really be doing is to understand what you're accessing. What your conditions are and why? This just requires you to think carefully about what you are doing and you will get better at it with practice...
So, your for loop should look like this:
Code:
for(int current = 0; current < lengthOf; ++current) { // strictly less than
if(sentence.at(current) == ' ') ++spaceCounter;
}
Also, you might want to keep your variables as local in scope as possible. It'll benefit you in the long run!
Edit: mild addition to make my point more clear...