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.