Count newlines, blanks and tabs

closed account (EwCjE3v7)
I have the following exercise:
Modify our vowel-counting program so that it counts the number of blank spaces,tabs and newlines read


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
#include <iostream>
#include <string>
#include <vector>
using std::cout; using std::endl; using std::cin;
using std::string; using std::vector;

int main()
{
    char ch = 0; // getting input
    unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, blankCnt = 0, tabCnt = 0, newlineCnt = 0; // counters
    cout << "Type in letters, once finished press CTRL+D/Z (UNIX/WINDOWS)" << endl; // instructions

    while (cin >> ch)
    {
        // if its a vowel, increment the appropriate counter
        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 ' ':
                ++blankCnt;
                break;
            case '\t':
                ++tabCnt;
                break;
            case '\n':
                ++newlineCnt;
                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 spaces: \t" << blankCnt << '\n'
            << "Number of tabs: \t" << tabCnt << '\n'
            << "Number of newlines \t" << newlineCnt << endl;
    return 0;
}


That is what I got, but I`m lost
So, what's the problem?
Use the get function instead because >> will ignore whitespace.
http://www.cplusplus.com/reference/istream/istream/get/
closed account (EwCjE3v7)
So how do I implement the getline?


And my problem was that I do not know how to find out spaces newlines and tabs

Okay so I got it done with strings:

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
// Keyboard keys \ |
#include <iostream>
#include <string>
#include <vector>
using std::cout; using std::endl; using std::cin;
using std::string; using std::vector;

int main()
{
    string ch; // getting input
    unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, blankCnt = 0, tabCnt = 0, newlineCnt = 0; // counters
    cout << "Type in letters, once finished press CTRL+D/Z (UNIX/WINDOWS)" << endl; // instructions

    while (getline(cin, ch))
    {
        for (auto c : ch)
        {
            // if its a vowel, increment the appropriate counter
            switch (c)
            {
                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 ' ':
                    ++blankCnt;
                    break;
                case '\t':
                    ++tabCnt;
                    break;
                case '\n':
                    ++newlineCnt;
                    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 spaces: \t" << blankCnt << '\n'
            << "Number of tabs: \t" << tabCnt << '\n'
            << "Number of newlines \t" << newlineCnt << endl;
    return 0;
}


But I do not know how to type a newline in the console.

Could you show me your getline way and how you did it on a char
Last edited on
Use unformatted input.

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
#include <iostream>
#include <cctype>

int main()
{
    std::cout << "Type in letters, once finished press CTRL+D/Z (UNIX/WINDOWS)\n" ;

    constexpr char TAB = '\t' ;
    constexpr char NEWLINE = '\n' ;

    std::size_t num_tabs = 0 ;
    std::size_t num_newlines = 0 ;
    std::size_t num_spaces = 0 ;

    char c ;
    // http://en.cppreference.com/w/cpp/io/basic_istream/get
    while( std::cin.get(c) )
    {
        if( std::isspace(c) )
        {
            switch(c)
            {
                case TAB: ++num_tabs ; break ;
                case NEWLINE: ++num_newlines ; break ;
                default: ++num_spaces ;
            }
        }
    }

    std::cout << "#tabs: " << num_tabs << '\n'
               << "#newlines: " << num_newlines << '\n'
               << "#other space characters: " << num_spaces << '\n' ;
}
Topic archived. No new replies allowed.