An "off by one" error is a miscalculation of an integer variable that is off by 1 or another small amount. An example of this problem is illustrated as follows:
/* Prompt for a word and display the number of times the letter 'a'
appears in the word
*/
string s;
cout << "Enter a word";
cin >> s;
int len = s.length();
int count_a = 0;
int i = 1;
while (i < len)
{
if (s.substr(i,1) == "a")
count_a++;
i++;
}
cout << "Letter a was found " << count_a << " times.\n";
The above code has a problem. The loop doesn't check each letter in the word. The first letter is skipped because the loop counter i is set to 1, instead of 0.