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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstring>
using namespace std;
const int SZ = 20; // size of arrays to hold scores
struct payrollStruct
{
string firstname; // first name of golfer
string lastname; // last name of golfers
string empID;
// int hrs;
};
void tellUser();
int readInput(payrollStruct []);
void bubbleSort(payrollStruct [], int);
int writeReport(payrollStruct [], int);
void swap(payrollStruct *, payrollStruct *);
int main()
{
int i, code, ret; // counter
payrollStruct employee[SZ]; // array of golfers and data
int numemp; // total number of golfers
tellUser(); // tell user what program does
numemp= readInput(employee);
cout << "There were " << numemp << " golfers\n\n";
if (numemp < 1)
{
cout << "Program ending\n\n";
code = 1;
}
else
{
// calculate total and average for each golfer
cout << "calculating average and totals\n";
// sort golfers
cout << "Sorting the golfers in order with the winner first\n";
bubbleSort(employee, numemp);
code = writeReport(employee, numemp);
}
return code;
}
/* **************************************************************************
* output report
* this function writes data to screen and file
* input parameters: array of golfStructs and number golfers
* ouput written to screen and file
-
womengolfreport.txt
* ************************************************************************* */
int writeReport(payrollStruct emp[],int numE)
{
int i; // counter
ofstream outputFile; // output file
// open report file and write output data ********************
cout << "Report being written to file payroll.txt\n";
outputFile.open("payroll.txt"); // output file
if (outputFile.fail())
{
cout << "output file did NOT open\n";
cout << "output will only be sent to the screen\n";
cout << "\n\nGolfer Name Pos rnd1 rnd2 rnd3 rnd4 total average\n";
for ( i=0; i < numE; i++)
{
//emp[i].place = (i + 1);
cout << emp[i].firstname << " ";
cout << emp[i].lastname << " ";
cout << emp[i].empID << "\n";// << emp[i].hrs << " ";
// cout << emp[i].rate << " ";// << glf[i].rounnd[3] << " ";
// cout << glf[i].totalScore << " ";
// cout << glf[i].averageScore << "\n";
}
} // end if file did not open
else
{
cout <<"First Last Employee Hours Rate Regular Overtime Gross\n"; //table of employees payroll data sheet to the screen
cout <<"Name Name Number Worked of Pay Pay Pay Pay\n";
cout <<"===========================================================================================\n";
outputFile <<"First Last Employee Hours Rate Regular Overtime Gross\n";//table of employees payroll data sheet to txt file
outputFile <<"Name Name Number Worked of Pay Pay Pay Pay\n";
outputFile <<"=========================================================================================\n";
// loop to print report of golfers ********************
for ( i=0; i < numE; i++)
{
// emp[i].place = (i + 1);
//outpute << setw(12);
//outputFile << setw(9) << fixed << left << glf[i].lastname << " ";
//outputFile << setw(9) << fixed << left << glf[i].firstname << " ";
//outputFile << glf[i].place << " ";
//outputFile << setw(4) << fixed << left << glf[i].rounnd[0] << " ";
//outputFile << setw(4) << fixed << left << glf[i].rounnd[1] << " ";
//outputFile << setw(4) << fixed << left << glf[i].rounnd[2] << " ";
//outputFile << setw(4) << fixed << left << glf[i].rounnd[3] << " ";
//outputFile << setw(6) << fixed << left << glf[i].totalScore << " ";
//outputFile << setw(7) << fixed << setprecision(2) << glf[i].averageScore << "\n";
cout << fixed << left << emp[i].lastname << setw(10) << emp[i].firstname;
cout << emp[i].empID << "\n";
}
outputFile.close();
cout << "Output file closed\n\n";
} // end else output file open
return 0;
} // end of writeReport **************************************
/* *********************************************************
* bubble sort with flag
* this function sorts golfers into order by total score
* the winner is first with the lowest score, alphabetical
* order when scores match
* input parameters: array of golfStruct and number of golfers
* in that array
* ouput: those array items are now sorted
*************************************************************** */
void bubbleSort(payrollStruct emp[], int numE)
{
bool swapmade = false; // when true a swap was made in this pass
int i, lastpos; // last position to look for correct order
// sort the golfers from winner to loser
cout << "sorting golfers with winner first\n";
lastpos = numE;
do
{
lastpos--;
swapmade = false;
for ( i = 0; i < lastpos; i++)
{
if ( emp[i].lastname > emp[i+1].lastname) // they are in the wrong order
{ // swap all items here
swap(emp[i], emp[i+1]);
swapmade = true;
{
} // end of for loop
} while(swapmade); // end of do loop
}
/* *********************************************************
* readInput
i* This function reads golfer data from a file "women.txt"
* It stores the first and last names of golfers and 4 rounds
* of golf scores for each golfer in an array of structures
* input: first and last names, 4 golf scores each person
* passes back: array of golfer data in structure
*
returns: number of golfers
*************************************************************** */
int readInput(payrollStruct emp[])
{
ifstream inputFile; // input filename in program
int numEmp; // number employees
int i;// counter
// open file and read input from file golfers.txt
inputFile.open("employees2.txt");
if (inputFile.fail())
{
cout << "Error opening file women.txt\n\n";
cout << "end of program\n\n";
return 0;
}
else
{
i = 0; // golfer number for array
while ( ( i < SZ) && (inputFile >> emp[i].empID))
{ // there is a line of data to read and array has rooms
inputFile >> emp[i].lastname; // read last name of golfer
inputFile >> emp[i].firstname; // read first name of golfer
i++; // ready for the next golfer
} // end while
numEmp = i; // number golfers
inputFile.close(); // close the file after reading it
cout << "input file closed\n\n";
return numEmp; // return number golfers read in file
} // end if else
} // end function readInput
/* ***************************************************************
* function tellUser
* it tells the user what the program does
* it prints output to the screen
* input: none output: tells user on s
creen what program does
* returns: none
****************************************************** */
void tellUser()
{
cout << "This program reads a file called employees2.txt,\n";
cout << "and it calculates the total and average for each golfer.\n";
cout << "golfers are sorted into order with winner first and loser last.\n";
cout << "output is written to the screen. and to an output file\n";
cout << "It uses an array of structures to store the data\n";
}
/*************************************
* Function swap
* this function swaps to golfStructs
* input parameters: two golf structs
* output parameters: now swapped with each other
****************************/
void swap(payrollStruct &empa, payrollStruct &empb)
{
payrollStruct temp; // place to store one golfer data
temp = empb;
empb = empa;
empa = temp;
}
|