Missing data from input file

Greetings,

I am trying to get the program to display the contents of employee.in. I have 10 lines in employee in. When I run the program, only lines 2-10 are displayed. What am I missing?

Many thanks!

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

using namespace std;

int main()
{
    int numberofemployees;
    int hoursworked, employeeid;
	int counter = 0;
    float hourlyrate, grosspay, taxamount, netpay;
	float const TAXRATE = 0.10;

    string str;
    ifstream fin("employee.in");
    getline(fin, str);

	cout << "________________________" << endl; 
	cout << "   SomeBusiness, Inc" << endl;
	cout << "Employee Payroll Program" << endl;
	cout << "________________________" << endl;
	cout <<endl;


	while(fin>>employeeid>>hoursworked>>hourlyrate)

    {

	grosspay = hoursworked*hourlyrate;
	netpay = grosspay*(1.0-TAXRATE);
    taxamount = grosspay*TAXRATE;

    cout << "Employee ID Number Is: " << employeeid << endl;
	cout << "Total Clocked Hours: " << hoursworked << endl;
	cout << "Hourly Pay Rate: $ " << hourlyrate << endl;
	cout << "Employee Gross Pay This Week: $" << grosspay << endl;
	cout << "Current Tax Rate: "  << TAXRATE << "%" << endl;
	cout << "Tax Deduction Amount: $" << taxamount << endl;
    cout << "Employee Net Pay: $" << netpay << endl<<endl;

    }//while
    cout << "Press the enter key to close the program.";
    cin.get();
    cin.get();
    return(0);

}//MAIN

closed account (EwCjE3v7)
Can we see what's in your employee.in file?
Sure:

1234 40 10.50
3214 35 9.00
9632 39 10.00
9875 26 8.00
7454 45 12.00
5552 20 11.75
1312 50 15.60
6713 40 13.56
8452 38 8.75
9111 42 9.50

It outputs the data from 3214 down, but skips 1234.
closed account (EwCjE3v7)
I have no computer near me and compiling with my phone is a hassle so could u try take out line 16, the getline one. That should fix it
Thanks! That did the trick. I'm still learning what everything means and when to use it. Taking an online course is more difficult than being in a classroom setting.
closed account (EwCjE3v7)
np, getline reads a until it hits end-of-line. While >> operator reads a word at a time.
So I am making progress with my payroll program. However, I am running into two issues. I am calculating additional taxes for married, single, and head of household. The first problem is that when an employee earns less than $500, there is no tax. If HoH gets a 5% tax reduction. This creates a negative tax deduction which actually adds a positive amount to the gross pay.

The second problem is that I have an employee which grossed $280 (no regular tax) but is single and thus is supposed to have a 5% tax added. This works for the other employees, except for employee 9875.

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

using namespace std;

int main(){

    int hw, id; //hours worked and employee id
    float hr, gp, ta, np, TR; //hourly rate, gross pay, tax amount, net pay, tax rate
    char msh;
    string str;
    ifstream fin("employee.in"); //input file

	cout << "________________________" << endl; //Program title
	cout << "   SomeBusiness, Inc" << endl;
	cout << "Employee Payroll Program" << endl;
	cout << "________________________" << endl;
	cout <<endl;


	while(fin>>id>>hw>>hr>>msh){

  	gp = hw * hr;
	if( gp > 1000 ) TR = 0.30;
	else if( gp > 800) TR = 0.20;
    else if( gp > 500) TR = 0.10;
    else TR = 0.0;
    switch(msh)

    {
    case 'S': TR+=0.05;
    break;
    case 'M': TR+=0.10;
    break;
    case 'H': TR-=0.05;
    break;
    }

    np = gp * (1.0-TR);
    ta = gp * TR;

    cout << "Employee ID Number Is: " << id << endl;
	cout << "Total Clocked Hours: " << hw << endl;
	cout << "Hourly Pay Rate: $ " << hr << endl;
	cout << "Employee Gross Pay This Week: $" << gp << endl;
	cout << "Current Tax Rate: "  << TR << "%" << endl;
	cout << "Tax Deduction Amount: $" << ta << endl;
    cout << "Employee Net Pay: $" << np << endl<<endl;

    }//while
    cout << "Press the enter key to close the program.";

    cin.get();
    return(0);

}//MAIN 


And my employee.in file contents:

1234 40 10.50 M
3214 35 9.00 S
9632 39 10.00 H
9875 26 8.00 S
7454 45 12.00 M
5552 20 11.75 H
1312 50 15.60 M
6713 40 13.56 S
8452 42 20.25 H
9111 60 20.00 M
closed account (EwCjE3v7)
#1

So your first problem is in the switch statement, you need to check if TR(tax rate) is not 0

1
2
3
4
5
6
7
8
9
10
11
12
switch(msh)

    {
    case 'S': TR+=0.05;
    break;
    case 'M': TR+=0.10;
    break;
    case 'H':
    	if (TR != 0.0) // if there is tax pay
    		TR-=0.05; // then decrement tax pay by 0.05
    	break;
    }


#2


No look at employee 9875, he makes 208, not 280. Everything is okay there. 5% of 208 is 10.4 and that is what's taken away.


Another problem you have there is your output, if you look at the first one for example,
Employee ID Number Is: 1234
Total Clocked Hours: 40
Hourly Pay Rate: $ 10.5
Employee Gross Pay This Week: $420
Current Tax Rate: 0.1%
Tax Deduction Amount: $42
Employee Net Pay: $378


The tax pay is not 0.1%, its 0.1 or 10%. So you will have to convert that 0.1, 0.05 etc into a %.

Last edited on
Thanks for your insight! Sorry for the late reply; early the following morning I left for a 6 day road-trip vacation. I turned in the assignment as is and got 100, so I was looking to add things that my professor wasn't looking for. I tend to do that because apart from school assignments, I am really trying to learn which is difficult in this class the way the books and online course is set up.

My professor kept things on schedule regardless of the holiday, so I am scrambling to complete the next assignment, which involves arrays and will probably post a question about that since I am unsure I am doing exactly what he wants and I am running into an error.
Topic archived. No new replies allowed.