Read a file with numbers and letters and total as well as AVG

Hello all,

I have little experience with C++ code, i have this program that i am not sure how to build.


• read the data from the file (via Linux redirection, no prompts)
• for each employee
determine how much the employee has earned
display the employee's ID number, category (using a descriptive word), and total
earned
• The output should be in the form of a table and each column in the table must have a
heading. The data in the ID number and category columns must be left justified. The total
earned values must be right justified, include a $ and 2 digits to the right of the decimal.
Remember to use constants for fixed values.
• The program must keep processing employee information until the end of file is
encountered.
• When all data has been processed, display the total amount earned by all employees as
the last line of the table with a suitable label. The final total must be right justified with
the amounts above it.
• Display messages stating the total number of employees processed and the average
amount paid


the raw file that it might read is.

1005 S 500.00
20 h 41.0 8.00
33 t 10.0 5.0 100.0
2109 H 30.0 10.0
4438 p 5.0 7 12 4 0
189 c 100.0 125.0 0.0 175.0 100.0
3149 P 2.5 7 10 18 4 6 0

Data File
A file contains the data needed to generate the weekly payroll summary. It is made up of several
employee records. Each record begins with the employee's identification number (int) and pay
code (char), see description of employee categories above. Then, depending on what category an
employee is in, the appropriate data will follow. Each data value will be separated by 1 or more
blanks.
• If the employee is salaried, the amount of his/her weekly salary (double) will be provided.
Example: 1005 s 500.00
• If the employee is hourly, the number of hours worked (double) followed by the hourly
pay rate (double) will be provided.
Example: 2017 h 41.0 8.75
• If the employee is on commission, his/her sales (double) made for the 5 days of the work
week will be provided.
Example: 1896 C 100.0 125.0 0.0 157.75 40.0
• If the employee is temporary, his/her hours worked (double) followed by hourly pay rate
(double) followed by the total amount of sales (double) will be provided.
Example: 2531 T 13.5 7.5 250.0
Page 2/4
• If the employee is a pieceworker, the amount paid (double) for an item will be provided,
followed by a series of values (int) representing his/her production for the week. The end
of the production values will be indicated by a 0. Note: all of the integers indicating
number of items produced will be greater than 0 except for the last value (sentinel) which
will be a 0.
Example: 3149 P 2.25 7 10 18 4 6 11 0
In the example above, the total number of items produced should be 56.



expected output should look like.

======================================================

ID#            CATEGORY         EARNINGS
------------------------------------------
1005           Salaried             $ 500.00
20             Hourly               $ 332.00
333            Temporary            $  65.00
2109           Pieceworker          $ 300.00
4438           Commissioned         $ 125.00
189            Salaried             $ 500.00
3149           Salaried             $ 112.00
TOTAL EARNED                        $ 1934.50

7 employees processed
Average pay per employees: $276.36




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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <iomanip>
#include <string> 
#include <cctype>

using namespace std;

int main()
{      
    int    id;
    char   paycode;
    string category;
    double earnings;
    int employee = 0;
    double sum = 0.00;

    cout << "==========================================\n\n";

    cout << left  << setw(12) << "ID #"
         << left  << setw(12) << "CATEGORY"
         << right << setw(12) << "EARNINGS"
         << "\n------------------------------------\n";
         


    while (cin >> id >> paycode)
    {
        
        paycode = toupper(paycode);
       //earnings = 0.0;
        //cin.get(paycode);
        // logic to find  the type of employee. 

        if      (paycode == 'S')
        {
            category =  "Salaried";
            cin >> earnings;
        }
        else if (paycode == 'H')
        {
            category =  "Hourly" ;
            double hours = 0;
            double rate  = 0;
            cin >> hours >> rate;
            earnings = hours * rate;
        }
        else if (paycode == 'T')
        {
            category =  "Temporary" ;
            double hours = 0;
            double rate  = 0;
            double sales  = 0;
            cin >> hours >> rate >> sales;
            earnings = (hours * rate) + sales;
        }
        else if (paycode == 'P')
        {
            category =  "Pieceworker" ;
            double rate  = 0;
            int item;
            int itemsum;
            cin >> rate >> item;
            while (item != 0)
            {
                itemsum= itemsum+itemsum;
                cin>>item;
            }
            earnings = itemsum * rate;
        }
        else if (paycode == 'C')
        {
            category =  "Commissioned" ;
            double sales  = 0;
            double salestotal =0;
            cin >>sales;
            for (int f = 1; f <= 5; f++)
            {
                salestotal = salestotal + salestotal;
            }
            earnings = salestotal;
        }
        cout << left  << setw(12) << id
             << left  << setw(12) << category
             << right << setw(12) << earnings
             << endl;

        //cin.ignore(1000, '\n'); // ignore rest of line
        employee++;
    }
    for (int j = 1; j < earnings; j++)
    {
        
        sum = sum + earnings;
    }
    cout <<"TOTAL EARNED " << right << setw(12) <<"$ "<<sum <<endl<<endl;
    cout << employee<<" employee processed"<<endl;
    cout <<"Average pay per employess; "<< "$ "<< sum /  employee <<endl;

    return 0;
}


Last edited on
The format of the input file can be quite varied. Only the first two items, identification number and pay code will always be present.

By convention in C++, all upper-case names tend to be reserved for special purposes such as for macros rather than ordinary variables. You need to store the category in a string rather than a single character.

You could start with something like this:

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

using namespace std;

int main()
{      
    int    id;
    char   paycode;
    string category;
    double earnings;

    cout << "==========================================\n\n";

    cout << left  << setw(12) << "ID #"
         << left  << setw(12) << "CATEGORY"
         << right << setw(12) << "EARNINGS"
         << "\n------------------------------------\n";

    while (cin >> id >> paycode)
    {
        paycode = toupper(paycode);
        earnings = 0.0;
        
        // logic to find  the type of employee. 

        if      (paycode == 'S')
        {
            category =  "Salaried";
            cin >> earnings;
        }
        else if (paycode == 'H')
        {
            category =  "Hourly" ;
        }
        else if (paycode == 'T')
        {
            category =  "Temporary" ;
        }
        else if (paycode == 'P')
        {
            category =  "Pieceworker" ;
        }
        else if (paycode == 'C')
        {
            category =  "Commissioned" ;
        }

        cout << left  << setw(12) << id
             << left  << setw(12) << category
             << right << setw(12) << earnings
             << endl;

        cin.ignore(1000, '\n'); // ignore rest of line

    }

    return 0;
}

Note line 32, cin >> earnings;. That is all you need for salaried employees. For the remainder, you will need to read different items from the input and calculate the earnings accordingly.


thank you for your guidance. sorry but i am still way off with what is expected.


here is what i am getting now




==========================================

ID #        CATEGORY        EARNINGS
------------------------------------
1005                             500
20                               500
33                               500
2109                             500
4438                             500
189                              500
3149                             500
TOTAL EARNED           $ 249500

7 employee processed
Average pay per employess; $ 35642.9








with this code below.

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

using namespace std;

int main()
{      
    int    id;
    char   paycode;
    string category;
    double earnings;
    int employee = 0;
    double sum = 0.00;

    cout << "==========================================\n\n";

    cout << left  << setw(12) << "ID #"
         << left  << setw(12) << "CATEGORY"
         << right << setw(12) << "EARNINGS"
         << "\n------------------------------------\n";
         


    while (cin >> id >> paycode)
    {
        
        paycode = toupper(paycode);
       // earnings = 0.0;
        cin.get(paycode);
        // logic to find  the type of employee. 

        if      (paycode == 'S' || paycode == 's')
        {
            category =  "Salaried";
            earnings = 500;
        }
        else if (paycode == 'H' || paycode == 'h')
        {
            category =  "Hourly" ;
            cin>>id;
        }
        else if (paycode == 'T')
        {
            category =  "Temporary" ;
        }
        else if (paycode == 'P')
        {
            category =  "Pieceworker" ;
        }
        else if (paycode == 'C')
        {
            category =  "Commissioned" ;
        }
        cout << left  << setw(12) << id
             << left  << setw(12) << category
             << right << setw(12) << earnings
             << endl;

        cin.ignore(1000, '\n'); // ignore rest of line
        employee++;
    }
    for (int j = 1; j < earnings; j++)
    {
        
        sum = sum + earnings;
    }
    cout <<"TOTAL EARNED " << right << setw(12) <<"$ "<<sum <<endl<<endl;
    cout << employee<<" employee processed"<<endl;
    cout <<"Average pay per employess; "<< "$ "<< sum /  employee <<endl;

    return 0;
}
Well, I'd rather not just complete the whole thing for you. But the way to approach it is to read the original specification which you were given carefully, and work from that.

What I did was to copy+paste the instructions in the form of a comment into the program at the appropriate point, and then write some code which carried out in C++ terms what the instructions described in ordinary English

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        if      (paycode == 'S')
        {
            // If the employee is salaried, the amount of his/her 
            // weekly salary (double) will be provided. 
            category =  "Salaried";
            cin >> earnings;
        }
        else if (paycode == 'H')
        {
            // If the employee is hourly, the number of hours worked (double) 
            // followed by the hourly pay rate (double) will be provided.
            category =  "Hourly" ;
            double hours = 0;
            double rate  = 0;
            cin >> hours >> rate;
            earnings = hours * rate;
        }
        else if 

The various cases tend to be variations on a theme, some are a little bit easier than others. But by looking at the input file as well, you should get the idea.


Take the first one:
• If the employee is salaried, the amount of his/her weekly salary (double) will be provided.
Example: 1005 s 500.00

We have already read the id (1005 and the paycode(s) so that just leaves the salary (500.00) to be read.

Next example:
• If the employee is hourly, the number of hours worked (double) followed by the hourly
pay rate (double) will be provided.
Example: 2017 h 41.0 8.75

We have already read the id(2017) and the paycode (h). That leaves the hours(41.0) and the rate(8.75) to be read in.

See the code I posted above.

Now the next example:
• If the employee is on commission, his/her sales (double) made for the 5 days of the work
week will be provided.
Example: 1896 C 100.0 125.0 0.0 157.75 40.0

We have already read the id(1896) and the paycode(C). That leaves five individual sales figures which need to be read, and added together.
(I've not shown the code for this - have a go at it yourself).


... and so on.

I hope this makes some kind of sense. You do need to examine the input file in order to get a feel for what each line contains, and written descriptions explain how to interpret the values contained in the file.
Last edited on
By the way. I used the function toupper() which converts the code letter to upper-case so there is no need to write
 
if      (paycode == 'S' || paycode == 's')
because we know it will never be a lower-case letter.

The aim is to simplify the code, make it less cluttered and easier to read and understand.

See http://www.cplusplus.com/reference/cctype/toupper/
so i added all the fixes you recomemded but now its outputting it blank.


also my totals and avereges are broken too.


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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

#include <iostream>
#include <iomanip>
#include <string> 
#include <cctype>

using namespace std;

int main()
{      
    int    id;
    char   paycode;
    string category;
    double earnings;
    int employee = 0;
    double sum = 0.00;

    cout << "==========================================\n\n";

    cout << left  << setw(12) << "ID #"
         << left  << setw(12) << "CATEGORY"
         << right << setw(12) << "EARNINGS"
         << "\n------------------------------------\n";
         


    while (cin >> id >> paycode)
    {
        
        paycode = toupper(paycode);
       //earnings = 0.0;
        //cin.get(paycode);
        // logic to find  the type of employee. 

        if      (paycode == 'S' )
        {
            category =  "Salaried";
            cin >> earnings;
        }
        else if (paycode == 'H')
        {
            category =  "Hourly" ;
            double hours = 0;
            double rate  = 0;
            cin >> hours >> rate;
            earnings = hours * rate;
        }
        else if (paycode == 'T')
        {
            category =  "Temporary" ;
            double hours = 0;
            double rate  = 0;
            double sales  = 0;
            cin >> hours >> rate >> sales;
            earnings = (hours * rate) + sales;
        }
        else if (paycode == 'P')
        {
            category =  "Pieceworker" ;
            double rate  = 0;
            int item;
            int itemsum;
            cin >> rate >> item;
            while (item != 0)
            {
                itemsum= itemsum+itemsum;
                cin>>item;
            }
            earnings = itemsum * rate;
        }
        else if (paycode == 'C')
        {
            category =  "Commissioned" ;
            double sales  = 0;
            double salestotal =0;
            cin >>sales;
            for (int f = 1; f <= 5; f++)
            {
                salestotal = salestotal + salestotal;
            }
            earnings = salestotal;
        }
        cout << left  << setw(12) << id
             << left  << setw(12) << category
             << right << setw(12) << earnings
             << endl;

        cin.ignore(1000, '\n'); // ignore rest of line
        employee++;
    }
    for (int j = 1; j < earnings; j++)
    {
        
        sum = sum + earnings;
    }
    cout <<"TOTAL EARNED " << right << setw(12) <<"$ "<<sum <<endl<<endl;
    cout << employee<<" employee processed"<<endl;
    cout <<"Average pay per employess; "<< "$ "<< sum /  employee <<endl;

    return 0;
}
Last edited on
Well, I tested, exactly as posted, the latest code posted above, and got this output:
==========================================

ID #        CATEGORY        EARNINGS
------------------------------------
1005        Salaried             500
20          Hourly               328
33          Temporary            150
2109        Hourly               300
4438        Pieceworker          640
189         Commissioned           0
3149        Pieceworker        10240
TOTAL EARNED           $ 1.04847e+008

7 employee processed
Average pay per employess; $ 1.49782e+007

Not perfect, but it is looking very promising. Almost there :)

The compiler did give one warning, at line 62 itemsum was not initialised. It should be set to an initial value of zero. with that done, the output is now:
==========================================

ID #        CATEGORY        EARNINGS
------------------------------------
1005        Salaried             500
20          Hourly               328
33          Temporary            150
2109        Hourly               300
4438        Pieceworker            0
189         Commissioned           0
3149        Pieceworker            0
TOTAL EARNED           $ 0

7 employee processed
Average pay per employess; $ 0


OK ... I took a look more closely. The problems I found are these:

line 62-68:
62
63
64
65
66
67
68
            int itemsum;
            cin >> rate >> item;
            while (item != 0)
            {
                itemsum= itemsum+itemsum;
                cin>>item;
            }
Should be
62
63
64
65
66
67
68
            int itemsum = 0;        // make sure to initialize 
            cin >> rate >> item;
            while (item != 0)
            {
                itemsum = itemsum + item;  // add each item
                cin>>item;
            }


There's a similar problem in lines 76-80
76
77
78
79
80
            cin >>sales;
            for (int f = 1; f <= 5; f++)
            {
                salestotal = salestotal + salestotal;
            }
which should be:
76
77
78
79
80
            for (int f = 1; f <= 5; f++)
            {
                cin >>sales;
                salestotal = salestotal + sales;
            }


One more fix.
Just after line 89
 
        employee++;
insert this line:
 
        sum += earnings;           // accumulate total 


... and delete the loop at lines 91 - 95, it isn't needed
1
2
3
4
5
//    for (int j = 1; j < earnings; j++)
//    {
//        
//        sum = sum + earnings;
//    } 


The output I get is now:
==========================================

ID #        CATEGORY        EARNINGS
------------------------------------
1005        Salaried             500
20          Hourly               328
33          Temporary            150
2109        Hourly               300
4438        Pieceworker          115
189         Commissioned         500
3149        Pieceworker        112.5
TOTAL EARNED           $ 2005.5

7 employee processed
Average pay per employess; $ 286.5



All that remains I think is a little prettying-up of the output formatting.
Topic archived. No new replies allowed.