Switch statement, char counting two-character sequences

closed account (EwCjE3v7)
Exercise:
Modify our vowel-counting program so that it counts the number of occurrence of the following two-character sequences: ff,fl and fi


What I thought it would be
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Keyboard keys \ |
#include <iostream>
#include <string>
#include <vector>
using std::cout; using std::endl; using std::cin;
using std::isspace;
using std::string; using std::vector;

int main()
{
    char ch; // getting input
    unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, ffCnt = 0, flCnt = 0, fiCnt = 0; // counters
    constexpr char ff = 'ff', fl = 'fl', fi = 'fi';

    cout << "Type in letters, once finished press CTRL+D/Z (UNIX/WINDOWS)" << endl; // instructions

    while (cin.get(ch))
    {
            switch (ch)
            {
                case 'a':
                    ++aCnt;
                    break;
                case 'e':
                    ++eCnt;
                    break;
                case 'i':
                    ++iCnt;
                    break;
                case 'o':
                    ++oCnt;
                    break;
                case 'u':
                    ++uCnt;
                    break;
                // Capitals
                case 'A':
                    ++aCnt;
                    break;
                case 'E':
                    ++eCnt;
                    break;
                case 'I':
                    ++iCnt;
                    break;
                case 'O':
                    ++oCnt;
                    break;
                case 'U':
                    ++uCnt;
                    break;

                case ff:
                    ++ffCnt;
                    break;
                case fl:
                    ++flCnt;
                    break;
                case fi:
                    ++fiCnt;
                    break;
            }
        }
    }

    cout << "Number if vowel a: \t" << aCnt << '\n'
            << "Number of vowel e: \t" << eCnt << '\n'
            << "Number of vowel i: \t" << iCnt << '\n'
            << "Number of vowel o: \t" << oCnt << '\n'
            << "Number of vowel u: \t" << uCnt << '\n'
            << "Number of the following two-character sequence ff: \t" << ffCnt << '\n'
            << "Number of the following two-character sequence fl: \t" << flCnt << '\n'
            << "Number of the following two-character sequence fi: \t" << fiCnt << endl;
    return 0;
}
When you read an 'f', you should also read the next character to see if it's an f, l, or i.

So something like
1
2
3
4
5
6
7
8
9
case 'f':
    if (cin.get(ch))
    {
        switch (ch)
        {
            case 'f':
                ++ffCnt;
                break;
            // ... 

And if next character is not one of those you are looking for, you should put it back:

1
2
3
4
5
6
7
8
9
10
11
case 'f':
    if (cin.get(ch))
    {
        switch (ch)
        {
            case 'f':
                ++ffCnt;
                break;
            // ... 
            default:
                cin.unget(ch);
You probably should use a map/array for your counters and not a bunch of variables. Another thing to mention is if you do the way the two before me suggest don't forget that when you have "fi" it will be counted as a vowel (i) and the sequence (fi).

PS. constexpr char ff = 'ff', fl = 'fl', fi = 'fi'; a character is a character not two characters.
Last edited on
Topic archived. No new replies allowed.