Program for Reading Employees' Pay Info From a File

So my assignment is the read a list of employee's pay information from a file, and to output each employee's name and weekly pay, then the gross pay for the company for the week. The employee data is as such, where H represents hourly pay and S represents salary:
Filbert Fred H 50 14.5
Ianni Indira S 200000
Atom Andy S 50000
Johnson James S 20000
Michael Cindy S 30000
Smith Paul S 20300
Zorn Charles H 40 20.3
Able Joyce S 70200
Bean Fred S 45600
Jones Jack S 15000
Baltic Mary S 12400
Daniels Mary S 23600
Gale Sandy S 26800
Sanders Carol H 40 12
Sanders Robert H 48 15
Talbot Irene S 13500
Carol David S 19000
Frender Janice S 46700
Michaels Thomas S 78000
Paulson Philip H 40 18.5
Norgin Jerry S 25000
Patel Khalid H 40 19
Chen Sue S 47500

The code I have appears as if it is not even beginning the loop, and that none of the employees' information is being read. Some help would be much appreciated.


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
  // Week 10.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
	ifstream myemployees;
	string filename, lname, fname, emptype;
	double hours, weeklypay = 0, sumpay = 0, pay;

	cout.imbue(locale(""));

	cout << "Enter the filename: " << endl;
	getline(cin, filename);

	myemployees.open(filename);
	
	while (myemployees >> lname >> fname >> emptype >> hours >> pay)
	{
		myemployees >> lname >> fname >> emptype;
		if (emptype == "H") //how pay is to be calculated for hourly employees, including overtime if present
		{
			weeklypay = 0;
			if (hours > 40)
				weeklypay = pay * (40 + (hours - 40)*1.5);
			else
				weeklypay = pay * hours;
		}
		else if (emptype == "S") //how pay is to be calculated for salaried employees
			weeklypay = 0;
			weeklypay = hours;
		sumpay += weeklypay;
	} 
	cout << fname << " " << lname << " " << setw(25) << left;
	cout << weeklypay << setw(20) << setprecision(2) << fixed << right;
	cout << "Total payroll is: " << sumpay << endl;

	return(0);
}
Last edited on
Hello acscicplusplus,

Thank you for using code tags and including the input file.

My first question is how do you know that the file stream is actually open? You have no way to tell. Add this after your open statement:
1
2
3
4
5
if (!myemployees)
{
	std::cout << "\n File \"" << filename << "\" did not open" << std::endl;
	return 1;  //exit(1);  // If not in "main".
}

This will let you know if the file did not open.

Line 25 the while condition is correct, but you are reading to much. You only need to read up to "emptype". Leave reading what is left to the if/else statement.

Line 27 starts reading the second line, but not all the fields.

if (emptype == "H") now you need to read the hours and pay rate.

The "else if" can just be an "else". You do not need to check if it equals "S". Since you have only two choices if it is not "H" then it must be an "S".

The next problem I see if the else statement is missing the {}s. Only line 37 is part of the else statement. Line 38 although indented does not make it part of the else statement.

When you enter the else statement the first thing you will to do is read the salary amount. Since this is a yearly amount you will need to divide it by 52 to get the weekly amount.

That is what I see for now. After I make the changes and test the program I wil let you know if I find anything else.

Hope that helps,

Andy
Hello acscicplusplus,

After making the changes it worked much better.

When you defined you "double" variables you initialized some to zero. To be proper it should be "0.0". Also while you are there it would not hurt to initialize all the variables. To deal with salaried employees I added the variable "salary".

I put this before the while loop: std::cout << std::fixed << std::showpoint << std::setprecision(2);. This only needs to be done once and will affect the "cout" statements until something is changed or the program ends.

The "showpoint" will print ".00" should it happen and it does. Otherwise it just prints the whole number.

Finishing the reads inside the if and else statements solved the problem of reading the file properly.

Lines 41 and 42 I believe you want them inside the while loop. Putting them outside the while loop you will only print the last read from the file.

From what I see I do not believe you have a full understanding of "left", "right" and "setw". These are modifiers to the stream and only affect what follows. The cout statement works much better as:
1
2
cout << left << setw(10) << fname << " " << std::setw(10) << lname << " "
	<< right << setw(10) << weeklypay << std::endl;

As you can see you can do this in one "cout" statement. This will give you the output of:

Fred       Filbert        797.50
Indira     Ianni        3,846.15
Andy       Atom           961.54
James      Johnson        384.62
Cindy      Michael        576.92
Paul       Smith          390.38

Charles    Zorn           812.00
Joyce      Able         1,350.00
Fred       Bean           876.92
Jack       Jones          288.46
Mary       Baltic         238.46
Mary       Daniels        453.85
Sandy      Gale           515.38

Carol      Sanders        480.00

Robert     Sanders        780.00
Irene      Talbot         259.62
David      Carol          365.38
Janice     Frender        898.08
Thomas     Michaels     1,500.00

Philip     Paulson        740.00
Jerry      Norgin         480.77

Khalid     Patel          760.00
Sue        Chen           913.46
Total payroll is: 18,669.50

I am not sure where the blank lines came from. I will have to look into that.

Hope that helps,

Andy
Hello acscicplusplus,

Sorry about that the blank lines is my fault. I put in a line of code to use as a break point in the IDE to check variables and forgot to comment it out.

Now the above output does not have the extra blank lines.

Andy
First of all thank you so much for the help, this is more than I could have asked for!
My understanding of setw and left and right is limited, it was just one of the parameters I needed to include based upon the rubric. Lines 41 and 42 I originally had in the loop but when I saw they didn't work, I tried to mix them around to see if it might change anything. But I really appreciate this so much thank you.

Chris
Topic archived. No new replies allowed.