Determine Frequency of Entered Letters

It all works fine except all of my 'a' values are always 1 less than they are supposed to be.

For example: aaa

Will come up A: 2

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
void LetterFreqIO()
{
    int letterCount[26] = { 0 };
    int index;
    char input;
    char k = 'A';

    cout << "Please enter some characters: ";
    cin >> input;

    while (input != '!')
    {
        input = getchar();
        tolower(input);
        isalpha(input);
        index = input - 'a';
        letterCount[index] += 1;
    }
    for (int i = 0; i < 26; i++)
    {
        cout << k++ << ": " << letterCount[i] << endl;
    }
    cout << "\n";
    main2();
}
Last edited on
I've made some progress on my question since I last posted.

Can anyone help me now with the revisions I've made?
Consider an input string of "abc"
- Line 9 reads 'a' into input
- Line 13 reads 'b' into input
- Next time through the loop, Line 13 reads 'c' into input.

The points is that the first character isn't added to letterCounts.

Try getting rid of line 9 and change line 11 to while ((cin << input) && input != '!')

Also, you need to skip characters that aren't letters. You're making a call to isalpha() which will tell you if the character is a letter, but you don't do anything with that knowledge.
Topic archived. No new replies allowed.