Using Arrays

Jun 29, 2012 at 8:13pm
So I'm making a sample Payroll Program to learn how to use C++ and I wanted to be able to input the company name at the top and be able to input the employee first and last names in the program so it displays in a tabular form. I just started reading about Arrays but how would I go about doing this in my program? Here's what I have at this point for my payroll program made from scratch

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
#include <iostream>
#include <fstream>
using namespace std;
int main () {
    ifstream input;// Declare file streams.
    input.open("employee.txt"); //Open the input file
        int numberofemployees;
        int employeeid, hoursworked;
        char ms;
        float hourlyrate, grosspay, taxamount, netpay, overtimepay=0;
        float TAXRATE=0.20;
        numberofemployees=0;
        while(input>>employeeid>>hoursworked>>hourlyrate>>ms) {
        grosspay=hoursworked*hourlyrate;
        if( grosspay > 1000 )    TAXRATE=0.30;
              else if( grosspay > 800 ) TAXRATE=0.20;
              else if( grosspay > 500 ) TAXRATE=0.10;
              else TAXRATE = 0.0;
              switch(ms)
{
   case 'S': TAXRATE+=0.05;
   break;
   case 'M': TAXRATE+=0.10;
   break;
   default: TAXRATE+=0.20;
}

        if(hoursworked>40){
              overtimepay = (hoursworked-40)*hourlyrate*1.5;
        }
        taxamount=grosspay*TAXRATE;
        netpay=grosspay-taxamount+overtimepay;
        cout<<"EMPLOYEE ID IS " << employeeid<<endl;
        cout<<"THE HOURS WORKED ARE " << hoursworked<<endl;
        cout<<"THE HOURLY RATE IS " << hourlyrate<<endl;
        cout<<"THE GROSSPAY IS " << grosspay<<endl;
        cout<<"THE TAXAMOUNT IS " << taxamount<<endl;
        cout<<"THE OVERTIMEPAY IS "<< overtimepay <<endl;
        cout<<"THE NETPAY IS " << netpay<<endl<<endl;
        }//WHILE
        input.close();//Close the files
    system ( "pause" );
}//MAIN() 
Last edited on Jun 29, 2012 at 8:22pm
Jun 30, 2012 at 2:15am
There is a tutorial on array here.

http://www.cplusplus.com/doc/tutorial/arrays/


Personally, I would use a vector

http://www.cplusplus.com/reference/stl/vector/


I would also encapsulate an employee into a struct or class like so.


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
struct Employee
{
	void Set()
	{
		grossPay = hoursWorked * hourlyRate;
 		SetTaxBracket();
		
		if (hoursWorked > 40)
      		overtimePay = (hoursWorked - 40) * hourlyRate * 1.5;
		
		taxAmount = grossPay * taxRate;
		netPay = grossPay - taxAmount + overtimePay;
	}

	void Print(ostream& out) const
	{
		out << "EMPLOYEE ID IS " << employeeID << endl;
		out << "THE HOURS WORKED ARE " << hoursWorked << endl;
		out << "THE HOURLY RATE IS " << hourlyRate << endl;
		out << "THE GROSSPAY IS " << grossPay << endl;
		out << "THE TAXAMOUNT IS " << taxAmount << endl;
		out << "THE OVERTIMEPAY IS "<< overtimePay << endl;
		out << "THE NETPAY IS " << netPay << endl << endl;
	}
	
	void SetTaxBracket()
	{
		if (grossPay > 1000 )    
			taxRate = 0.30;
	  	else if (grossPay > 800 ) 
			taxRate = 0.20;
	  	else if(grossPay > 500 ) 
			taxRate = 0.10;
	  	else 
			taxRate = 0;
	  	
		switch(toupper(ms))
		{
		   case 'S': taxRate += 0.05;
		   	break;
		   case 'M': taxRate += 0.10;
		   	break;
		   default: taxRate += 0.20;
		}
	}

	char ms;
	int employeeID; 
	int hoursWorked;
	float hourlyRate;
 	float grossPay; 
	float taxAmount; 	
	float netPay; 
	float overtimePay;
	float taxRate;
};

istream& operator >> (istream& input, Employee& e) 
{
    input >> e.employeeID >> e.hoursWorked >> e.hourlyRate >> e.ms;
	return input; 
}

int main() 
{ 
	ifstream input;
	Employee employeeTemp;
	vector<Employee> employees;

	input.open("employee.txt"); 
	
	
	
	while (input >> employeeTemp)	
	{	
		employeeTemp.Set();
		employees.push_back(employeeTemp);
	}
	
	for (int i = 0; i < employees.size(); i++)
	{
		employees[i].Print(cout);
	}
	
	return 0;
}
Last edited on Jul 1, 2012 at 1:19am
Jun 30, 2012 at 4:06pm
Ok so I put that into my Dev C++ program and I'm getting errors from the void. What's the whole deal with the void anyway? When using voids you don't have to input a main ? Program didn't execute. What's your thoughts
Last edited on Jun 30, 2012 at 4:25pm
Jul 1, 2012 at 1:16am
Void just means that the method does not return anything.

I didn't test the code out with actual data so if it doesn't work correctly then you will have to fix it. I would rather you just create your own Employee class using the concepts in the code that way you actually learn something.

Jul 2, 2012 at 3:08am
Alright thank you bud
Topic archived. No new replies allowed.