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
|
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
void tellUser();
double calcAvg( int, int, int, int, int &);
void swap( int &num1, int &num2);
int main()
{
const int SZ = 20; // size of arrays to hold scores
string firstname[SZ], lastname[SZ]; // name of golfer
int i, lastpos; // counter
int numgolf; // total number of golfers
int lowscore = 400; // lowest score
int round1[SZ],round2[SZ],round3[SZ],round4[SZ]; //scores on 4 rounds of golf
int total[SZ];// total of four rounds of golf
double average[SZ]; // average for one player
ifstream inputFile; // input filename in program
ofstream outputFile; // output file
bool swapmade = false;
bool screenonly = false;//only write output to screen if true
tellUser(); // tell user what program does
// open file and read input from file golfers.txt
inputFile.open("women.txt");
if (inputFile.fail())
{
cout << "Error opening file women.txt\n\n";
cout << "end of program\n\n";
}
else
{
i = 0; // golfer number for array
while (inputFile >> round1[i]) // there is a line of data to read
{
inputFile >> round2[i]; // read golf score
inputFile >> round3[i]; // read golf score
inputFile >> round4[i]; // read golf score
inputFile >> firstname[i]; // read first name of golfer
inputFile >> lastname[i];// read last name of golfer
i++; // ready for the next golfer
}
cout << "There were " << i << " golfers\n\n";
numgolf = i; // number of golfers
inputFile.close(); // close the file after reading it
// ********************* close input file ************
cout << "input file closed\n\n";
// loop to calculate total and average for each golfer and finds winner
cout << "calculating average and totals\n";
for ( i=0; i < numgolf; i++)
{
average[i]=
calcAvg(round1[i],round2[i],round3[i],round4[i],total[i]);
}
cout << "sorting golfers\n";
lastpos = numgolf;
do
{
lastpos--;
swapmade = false;
for (i=0; i < lastpos; i++)
{
if (total[i] > total[i+1])
{
swap(firstname[i], firstname[i+1]);
swap(lastname[i], lastname[i+1]);
swap(total[i], total[i+1]);
swap(round1[i], round1[i+1]);
swap(round2[i], round2[i+1]);
swap(round3[i], round3[i+1]);
swap(round4[i], round4[i+1]);
swap(average[i], average[i+1]);
swap(round2[i], round2[i+1]);
swapmade = true;
}
}
}
// end for loop open report file and write
//output data ****************
cout << "Report being written to file womengolfreport.txt\n";
outputFile.open("womengolfreport.txt"); // output file
if (outputFile.fail())
{
screenonly = true;
cout << "output file did NOT open\n";
cout << "output will only be sent to the screen\n";
}
cout << "\n\nrnd1 rnd2 rnd3 rnd4 total average Golfer Name\n";
// loop to print report of golfers *******************
for ( i=0; i < numgolf; i++)
{
cout << round1[i] << " " << round2[i] << " ";
cout << round3[i] << " " << round4[i] << " ";
cout << total[i] << " " << average[i] << " ";
cout << firstname[i] << " " << lastname[i] << "\n";
if (!screenonly)
{
outputFile << round1[i] << " " << round2[i] << " ";
outputFile << round3[i] << " " << round4[i] << " ";
outputFile << total[i] << " " << average[i] << " ";
outputFile << firstname[i] << " " << lastname[i] << "\n";
}
} // end of forloop
if (!screenonly)
{
outputFile.close(); cout << "Output file closed\n\n";}
}
return 0;
}// end of main
/* ****************************************************
* function tellUser
* it tells
* the user what the program does
* it prints output to the screen
****************************************************** */
void tellUser()
{
cout << "This program reads a file called golfers.txt,\n";
cout << " and it calculates the total and average for each golfer.\n";
cout << " output is written to the screen. and to an outputfile \n\n";
}
/* *****************************************************
* function calcAvg
* receives the 4 scores and
* address of total. Calculates total and stores it
* calculates the average and returns it
******************************************************* */
double calcAvg(int r1,int r2,int r3,int r4, int &tot)
{
double avg; // calculated average
tot = r1 + r2 + r3 + r4;
avg = 1.0 * to 4.0;
return avg;
}
/* ***************************************************************
* function swap
* swaps two item
* input: 2 whole number
* output: switches vaules of the two 2 items
* passed back: the two 2 whole numbers are switched
* returns: none
****************************************************************/
void swap( int &num1, int num2)
{
int temp;
temp = num1;
num1 = num2;
num2 = temp;
}
|