C++ Struct array calculate wage

I have to calculate the wage for each employee. How would I write a function to do this?
And how would I sort the employees according to their wage?
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
#include <iostream>
#include <fstream>
#include<string>
#include <sstream>
#include <stdlib.h>
using namespace std;


struct Record
{
	string name;
	int id;
	double rate;
	int hrWorked;
	double wage;

};

void read_file(string[], int); 
void calculate_wage(string[], int);


int main()
{
	int e = 5;    
	Record payroll[5];
	string s[5];

	

	cout << "Your entered file is: " << endl; 
	read_file(s, e);
	
	//calculate_wage(r);
	

	system("pause");
	return 0;
}
void read_file(string s[], int e)
{
	ifstream myFile;
	myFile.open("Project 3.dat");
	string str;
	int i = 0;

	if (myFile.is_open())
	{
		while (getline(myFile, str))
		{
			s[i++] = str;
			cout << str << endl;
		}

		myFile.close();
	}
	else
	{
		cout << "No Data Found!" << endl;
	}

}

void calculate_wage (Record& payroll) // i know this isn't right
{
	char emplresult[256];  // need to convert int to string
	payroll.wage = atoi(emplresult);
	payroll.rate = atoi(emplresult);
	payroll.hrWorked = atoi(emplresult);
	
	payroll.wage = payroll.rate * payroll.hrWorked;
	

}

How do you want to calculate the wage? What is the point of emplresult? What does your Project 3.dat file look like? I think you're trying to do this in a very roundabout manner, there's probably an easier way of doing this other than getline, if every line has the same amount of words/numbers on it.

Your immediate error is that line 20 calls for an array of strings, and an int, but line 64 is only providing a reference to Record. You need to make these definitions match.
Last edited on
Hello tidematic,

You have void calculate_wage (Record& payroll) // i know this isn't right .

To do this properly and pass the array by reference it would be this:
void calculate_wage (Record& (payroll)[5]). In this case you have to include the size of the array. And something that would be better is:
void calculate_wage (Record& (payroll)[MAXSIZE]). Where "MAXSIZE" is defined above main as constexpr int MAXSIZE{5};. Now any time you need to change the size of the array or any place else in the program all you have to change is the value of "MAXSIZE" in one place.

In the "calculate_wage" function you kind of have the right idea, but you are going about it in the wrong way. You are using n array of structs, but you are not accessing each element of the struct as you should. A for loop would work for this. Also instead of the character array I would use a "std::string", which in the end is not necessary as you should be using the numeric variables from the struct.

A sample of your input file would be handy so I could test the program. And the read function is not written correctly to read a file and fill the struct.

I would suggest correcting the read function first and fix it to red the file and fill each struct in the array.

As a note you do not really have to pass the array by reference for the program to work. Most times when I am working on a program I will pass an array by reference just so I can see what it is doing in the function as I am testing. It comes in handy.

I will see what I can work up to test the program and get a better idea of what needs to be done.

Hope that helps,

Andy
1
2
3
4
5
    A.Smith                       20001 25 40
    T.Philip                      20002 20 35
    S.Long                        20003 15 50
    G.Santos                      20004 30 30
    F.Farkas                      20005 22 55


Andy, so something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13

     void calculate_wage (Record& (payroll)[5])
    {
    /*	    char emplresult[256];  // need to convert int to string
	    payroll.wage = atoi(emplresult);
	    payroll.rate = atoi(emplresult);
	    payroll.hrWorked = atoi(emplresult);
    */
	for (int i = 0; i < 5; i++)
	{
		payroll[i].wage = payroll[i].rate * payroll[i].hrWorked;
	}
Last edited on
Hello tidematic,

Yes, the for loop is correct, but I was wrong about passing by reference. It should be (Record (&payroll)[5]). I had the & in the wrong place. Sorry it has been a little while since I have done this.

Also it would be a good idea to initialize your variables in the struct and else where when you define them. It causes less hassle of having unwanted garbage in the variables when you start. A "std::string" do not need initialized because it is empty when defined.

For the input file if there is leading white space before the name that can be dealt with. If you can change the file I would put a comma after the name and delete any white space between the comma and the ID number. The rest is OK. Of course I am basing on using "std::getline()" to read the name and the name maybe having a space in it.

Hope that helps

Andy
Hello tidematic,

A couple of things I noticed in your program.

"stdlib.h" would be better to use "cstdlib", but this is redundant because it is already included through "iostream".

using namespace std; is a bad idea as it WILL get you n trouble some day. Right now it seems easy, but not really. This is worth a read to start with:
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Using "system" anything will leave your program vulnerable to attack. A replacement would be:
1
2
std::cout << "\n\n Press Enter to continue:";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. 

Hope that helps,

Andy
The normal procedure for this task is to use a vector of employees. If you aren't allowed to use a vector then an array of employees would make your life easier.
Thanks alot I got it to run properly. Appreciate it
Topic archived. No new replies allowed.