Using file I/O, arrays and functions to read employee data

Hi, I have an assignment I’m working on where I have to read into a file for data on employee hours worked in a week then calculate the number of hours worked each week for each employee and sort the employees in descending order. I have to do this using 3 functions and a 50x8 array. I am having a problem where the employee’s hours are all getting added and then from the first employees total, the next employee’s hours gets added.

Ex.
Employee Weekly Hours:
Name: S M T W T F S TTL
Jane,Patrick 9 3 7 5 8 0 0 32
Lisbon,Teresa 2 3 8 3 6 3 5 62
Cho,Kimball 8 8 3 0 8 2 0 91
Rigsby,Wayne 9 10 4 7 0 0 0 121
VanPelt,Grace 5 6 5 6 5 6 5 159

Edit: I fixed the issue of having the wrong total, I now have the issue that the data will not sort as well as the issue below

In the output there is also several lines of visual studios garbage number like so:
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -1717986924
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -1717986924
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -1717986924
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -1717986924

I am also unsure if I am calling my functions correctly, I have three functions one to read the file, one to bubble sort them and one for my output and I am unsure if I put the calculations in my for loop in the correct place:

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
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

const int TOTAL_HOURS = 8;

void read(int arr[][TOTAL_HOURS], string name[], int employees);

void bubblesort(int arr[][TOTAL_HOURS], string name[], int employees);

void output(int arr[][TOTAL_HOURS], string name[], int employees);


int main()
{

	int hours[50][TOTAL_HOURS];
	string name[50];

	read(hours, name, 50);
	bubblesort(hours, name, 50);
	output(hours, name, 50);

}
void read(int arr[][TOTAL_HOURS], string name[], int employees) {

	ifstream fin;

	string fileName;
	int total = 0;
	fileName = "empdata3.txt";

	fin.open(fileName);

	if (fin.fail()) {

		std::cout << " Your file was unable to open.\n\n";
		exit(1);
	}
	fin >> employees;
	for (int row = 0; row < employees; ++row) {
		fin >> name[row];
		total = 0;
		for (int day = 0; day < 7; day++) {
			fin >> arr[row][day];
			total += arr[row][day];
		}
		arr[row][7] = total;
	}
	fin.close();

}
void bubblesort(int arr[][TOTAL_HOURS], string name[], int employees)
{
	bool swapped;
	swapped = true;
	int temp;
	string temp2;
	int k = 0;

	while (swapped) {
		//k = 1;
		//int flag = 0;
		swapped = false;
		for (int i = 0; i < employees - 1; i++) {
			if (arr[i][7] < arr[i + 1][7]) {
				temp = arr[i][7];
				arr[i][7] = arr[i + 1][7];
				arr[i + 1][7] = temp;
				

				temp2 = name[i];
				name[i] = name[i + 1];
				name[i + 1] = temp2;
				swapped = true;


			//	flag = 1;
			}
		}
		//k++;

		if (swapped == 0)
			break;
	}

}
void output(int arr[][TOTAL_HOURS], string name[], int employees)
{

	cout << "Employee Weekly Hours: " << endl;
	cout << "Name:           S  M  T  W  T  F  S  TTL" << endl;


	int total = 0;

	for (int row = 0; row < employees; ++row) {
		total = 0;
		cout << name[row] << "  ";
		for (int day = 0; day < 7; day++) {
			total += arr[row][day];
			cout << "  " << arr[row][day];
		}
		arr[row][7] = total;

		cout << "  " << total << endl;
	}
}


Also the file that I'm reading in is formatted as so:

"15

Jane,Patrick 9 3 7 5 8 0 0

Lisbon,Teresa 2 3 8 3 6 3 5

Cho,Kimball 8 8 3 0 8 2 0

Rigsby,Wayne 9 10 4 7 0 0 0

VanPelt,Grace 5 6 5 6 5 6 5

Bosco,Sam 7 3 8 7 2 5 7

Minelli,Virgil 2 5 3 0 4 9 4

Lilly,Jack 2 3 8 3 6 3 5

Daniels,Carl 8 8 3 0 8 2 0

Theiss,Bill 9 10 4 7 0 0 0

Peters,Greg 5 6 5 6 5 6 5

Rugg,James 7 3 8 7 2 5 7

Kirk,James 10 10 0 10 12 0 0

McCoy,Lenard 0 4 0 8 4 0 2

Scott,Annis 1 6 2 0 0 1 0"

The number at the beginning is being used to control the for loops inside the read function.
Last edited on
Hello ilikecheesecats,

In the output there is also several lines of visual studios garbage number like so:

It is not just VS. Not that I can see, but I would say that you did not initialize the array when you defined it and you are printing more of the array than you need to. The garbage may be different on a different computer, but it is still garbage.

I am also unsure if I am calling my functions correctly
I am not sure either since you did not include "main" or showed how you are calling the functions.

If this is homework or a lab assignment then pass along the instructions that you were given. It is a great help and helps to avoid suggestions that are beyond what you have learned.

"stdio.h" is a C header file and you are not using any C code for input or output. You do not need this header file as "iostream" has already covered this. If you need to use "stdio.h" the correct C++ header file is "cstdio".

That is a start for now. I will have to load up the program to get a better idea of what it is doing. Maybe by then you will have posted the missing code that will allow the program to compile. It is best to include at least enough code so that the program can compile.

Andy
Thank you for your response! I am calling my functions here:


int main()
{

int hours[50][TOTAL_HOURS];
string name[50];

read(hours, name, 50);
bubblesort(hours, name, 50);
output(hours, name, 50);

}


it is lines 18-28. Did my code not upload properly?

Also this is a lab assignment and the main instructions are "In this program you will need to read from a file employee data for how many hours each employee worked for a week. Then for each of the employees, a total of the hours worked is calculated and then the employees are sorted into descending order based on their total hours.

Task Details:

Create a 50x8 int array to hold the employee’s data, and a size 50 string array to hold the employee names.
Read all the employee data into the arrays
Total number of hours worked in the week and store it in the int array at location 7. Create a const variable to make it obvious that your 7th position is being used to hold the total hours worked. Example: hour[ i ] [ TTL_HRS ] = total;

Sort the employees by the total number of hours they worked for the week. The sort should be in descending order (most hours to least)
Write out the information to the console"

I know there is also a way to do this with structs instead of arrays but we have only had a small lecture about them.
Last edited on
Hello ilikecheesecats,

Do not forget to use code tags. Even on a small bit of code.

Create a 50x8 int array to hold the employee’s data
That is nice, but were you shown how to sort a 2D array because I have yet to see anything the can sort a 2D array. Usually I see people being told to change the 2D array to a 1D array then sort it and finish with putting the 1D array back into a 2D array. But I have not gone that far yet to see what you have done.

I do have a couple of questions. In the input file are the double quotes part of the file? Does the file contain the blank lines that you show? All this makes a difference.

I made some changes to the beginning of the program:
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
//#include <cstdlib> // <--- Not really necessary and may be included through "iostream".
#include <iostream>
#include <iomanip>
#include <string>

#include <fstream>

using namespace std;  // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

constexpr int MAXROWS{ 50 };
constexpr int MAXCOLS{ 8 };

//const int TOTAL_HOURS = 8;  // <--- "TOTAL_HOURS" is a bit misleading.

bool read(int arr[][MAXCOLS], string name[], int& numEmployees);

void bubblesort(int arr[][MAXCOLS], string name[], int numEmployees);

void output(int arr[][MAXCOLS], string name[], int numEmployees);

int main()
{
	int hours[MAXROWS][MAXCOLS]{}, numEmployees{};
	string name[MAXROWS];

	if (read(hours, name, numEmployees))
		return 1;

	//bubblesort(hours, name, 50);

	output(hours, name, numEmployees);// <--- Not the 50 that you started with.

	return 0;  // <--- Not required, but makes a good break point for debugging.
}

In line 24 the {}s, uniform initializer, will initialize the array to (0) zeros and the single variable to (0) zero.

By defining "numEmployees" in main and passing it by reference to the read function you can use this in the other two function calls.

In the output there is also several lines of garbage numbers
This is because when you call the "output" function you are sending 50, telling the for loop to print the entire array when you should be sending "numEmployees", with a value of 15, which is what is used of the array. This will eliminate the garbage numbers that you are getting.

Also the "output" function I changes to:
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
void output(int arr[][MAXCOLS], string name[], int numEmployees)
{

	cout << std::string(10, ' ') << "Employee Weekly Hours: " << endl;
	cout << "Name:            S  M  T  W  T  F  S  TTL" << endl;
	std::cout << std::string(41, '-') << '\n';

	int total = 0;

	for (int row = 0; row < numEmployees; ++row)
	{
		total = 0;

		cout << std::left << std::setw(16) << name[row] << std::right;

		for (int day = 0; day < 7; day++)
		{
			//total += arr[row][day];
			cout << std::setw(2) << arr[row][day] << " ";
		}

		std::cout << "  " << arr[row][7] << '\n';

		//arr[row][7] = total;

		//cout << "  " << total << endl;
	}
}


Which produces the output of:

          Employee Weekly Hours:

Name:            S  M  T  W  T  F  S  TTL
-----------------------------------------
Jane,Patrick     9  3  7  5  8  0  0   32
Lisbon,Teresa    2  3  8  3  6  3  5   30
Cho,Kimball      8  8  3  0  8  2  0   29
Rigsby,Wayne     9 10  4  7  0  0  0   30
VanPelt,Grace    5  6  5  6  5  6  5   38
Bosco,Sam        7  3  8  7  2  5  7   39
Minelli,Virgil   2  5  3  0  4  9  4   27
Lilly,Jack       2  3  8  3  6  3  5   30
Daniels,Carl     8  8  3  0  8  2  0   29
Theiss,Bill      9 10  4  7  0  0  0   30
Peters,Greg      5  6  5  6  5  6  5   38
Rugg,James       7  3  8  7  2  5  7   39
Kirk,James      10 10  0 10 12  0  0   42
McCoy,Lenard     0  4  0  8  4  0  2   18
Scott,Annis      1  6  2  0  0  1  0   10



For the "read" function using the input file that looks like this:

15
Jane,Patrick 9 3 7 5 8 0 0
Lisbon,Teresa 2 3 8 3 6 3 5


And working with what you started with I made these changes:
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
bool read(int arr[][MAXCOLS], string name[], int& numEmployees)
{
	const string fileName{ "empdata3.txt" };

	int total = 0;
	//fileName = "empdata3.txt";
	
	//fin.open(fileName);

	ifstream fin(fileName);

        //if (fin.fail())
        if (!fin)  // <--- This is all you need.
	{
		std::cout << " Your file "<< std::quoted(fileName) << " was unable to open.\n\n";
		return true; //exit(1); // <--- Use "return" to get back to "main" not "exit".
	}

	fin >> numEmployees;

	for (int row = 0; row < numEmployees; ++row)
	{
		fin >> name[row];
	
	        total = 0;

		for (int day = 0; day < 7; day++)
		{
			fin >> arr[row][day];

			total += arr[row][day];
		}

		arr[row][7] = total;
	}

	//fin.close(); // <--- Not needed, but OK if you leave it. The file stream will close when the function looses scope.

	return false;  // <--- In this case "false" means that everything is OK.
}

Sorry I changed some of the variable names. I found it easier to understand and follow when working with the code.

The "return" statements work wit the code in "main"
1
2
if (read(hours, name, numEmployees))
	return 1;

The return value of the function determines is you leave the program or continue on.

By passing "numEmployees" by reference you change the value of that variable back in "main" so that you can use it later.

Line 10 simplifies what you have done by defining the file stream and opening the file in one line.

The comments should explain the rest. If not let me know.

Andy
Hello ilikecheesecats,

I was looking at the sort. It works, but fails beautifully.

You have managed to sort the total hours and the names, but fail to sort the daily hours.

This is where a 1D array of structs would work.

The struct would look something like this:
1
2
3
4
5
struct Employee
{
	std::string s_name;
	int s_hours[MAXCOLS]{};
};


Then in "main" you would define: Employee employees[MAXROWS]; to be passed to the functions.

This way when you get to the sort function it is very easy to sort the whole struct based on the total hours. I have yet to change the code for this, but that is the general idea.

Reading and outputting an array of structs is very easy too.

The problem is having to use a 2D array and sort everything. This is not easily done based on what I know at this time.

Andy
Hi Andy, Thank you so much for your help with this program. To answer your question yes the spaces are like that in the text file. I kept some of the things you commented out in my code bc they were specific things my prof asked for (such as "exit", "fin.open" and the using namespace). I also didn't realize I had to have this "&"
 
bool read(int arr[][MAXCOLS], string name[], int& numEmployees);


Using this made it so my original read would run. I was having an issue with your if statement in main where it would just print out nothing in main so I kept it as:
1
2
3
4
5
6
7
8
9
10
int main()
{

	int hours[50][MAXCOLS], numEmployees{}; // I kept the 50 here because the 
                                                                      // output seemed to not be outputting anything
	string name[MAXROWS];

	read(hours, name, numEmployees);
	bubblesort(hours, name, numEmployees);
	output(hours, name, numEmployees);



The output that you edited also worked wonderfully so thank you I really appreciate all your help! :) I eventually got my sort function to work after asking for help from my professor and it now looks 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
void bubblesort(int arr[][MAXCOLS], string name[], int numEmployees)
{
	bool swapped;
	swapped = true;
	int temp;
	string temp2;
	int k = 0;

	while (swapped) {
		k = 0;
		int flag = 0;
		swapped = false;
		for (int i = 0; i < numEmployees - 1; i++) {
			if (arr[i][7] < arr[i + 1][7]) {
				for (int j = 0; j < 8; j++) {
					temp = arr[i][j];
					arr[i][j] = arr[i + 1][j];
					arr[i + 1][j] = temp;
				}
				temp2 = name[i];
				name[i] = name[i + 1];
				name[i + 1] = temp2;
				swapped = true;

				flag = 1;
			}
		}
		k++;

		if (swapped == 0)
			break;
	}

}


Also thank you for giving an example of the struct because for my next program I will have to do this same thing but with structs and pointers!
My full code ended up being:
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
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <iomanip>
#include <fstream>

#include <string>
using namespace std;

constexpr int MAXROWS{ 50 };
const int MAXCOLS{ 8 };

bool read(int arr[][MAXCOLS], string name[], int& numEmployees);

void bubblesort(int arr[][MAXCOLS], string name[], int numEmployees);

void output(int arr[][MAXCOLS], string name[], int numEmployees);


int main()
{

	int hours[50][MAXCOLS], numEmployees{};
	string name[MAXROWS];

	read(hours, name, numEmployees);
	bubblesort(hours, name, numEmployees);
	output(hours, name, numEmployees);

}
bool read(int arr[][MAXCOLS], string name[], int& numEmployees) {

	ifstream fin;

	string fileName;
	int total = 0;
	fileName = "empdata3.txt";

	fin.open(fileName);

	if (fin.fail()) {

		cout << " Your file was unable to open.\n\n";
		exit(1);
	}

	fin >> numEmployees;
	for (int row = 0; row < numEmployees; ++row) {
		fin >> name[row];
		total = 0;
		for (int day = 0; day < 7; day++) {
			fin >> arr[row][day];
			total += arr[row][day];
		}
		arr[row][7] = total;
	}
	fin.close();

}
void bubblesort(int arr[][MAXCOLS], string name[], int numEmployees)
{
	bool swapped;
	swapped = true;
	int temp;
	string temp2;
	int k = 0;

	while (swapped) {
		k = 0;
		int flag = 0;
		swapped = false;
		for (int i = 0; i < numEmployees - 1; i++) {
			if (arr[i][7] < arr[i + 1][7]) {
				for (int j = 0; j < 8; j++) {
					temp = arr[i][j];
					arr[i][j] = arr[i + 1][j];
					arr[i + 1][j] = temp;
				}
				temp2 = name[i];
				name[i] = name[i + 1];
				name[i + 1] = temp2;
				swapped = true;

				flag = 1;
			}
		}
		k++;

		if (swapped == 0)
			break;
	}

}
void output(int arr[][MAXCOLS], string name[], int numEmployees)
{

	cout << string(10, ' ') << "Employee Weekly Hours: " << endl;
	cout << "Name:            S  M  T  W  T  F  S  TTL" << endl;
	cout << string(41, '-') << '\n';

	int total = 0;

	for (int row = 0; row < numEmployees; ++row)
	{
		total = 0;

		cout << left << setw(16) << name[row] << right;

		for (int day = 0; day < 7; day++)
		{
			cout << setw(2) << arr[row][day] << " ";
		}
		cout << "  " << arr[row][7] << '\n';
	}
}


Once again thank you for all your help and for guiding me in the right direction!
Topic archived. No new replies allowed.