...

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

int main()
{
    if( std::ifstream file{ "empdepart.txt" } ) // if the file is opened for input
    {
        const int ARRAY_SZ = 4 ;
        int counts[ARRAY_SZ] {} ; // initialise to all zeroes

        int id ;
        while( file >> id ) // for each id read from the file
        {
            // classify and increment appropriate count
            // note: array positions start at zero
            if (id >= 100 && id <= 299) ++counts[0] ; // Security
            else if (id <= 499) ++counts[1] ; // Accounting // note: else if
            else if (id <= 799 ) ++counts[2] ; // Engineering
            else if (id <= 899 ) ++counts[3] ; // Legal
            else ; // *** error *** this is an invalid id
        }

        // TO DO: add code to print out the four counts
    }

    else std::cerr << "failed to open input file\n" ;
}
Registered users can post here. Sign in or register to post.