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
|
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
//functions...
int loadArrays(int [], int [], int [], int [], int [], int);
int batAvg(int[], int[], int);
void printStats(int [], int [], int [], int [], int [],int[], int);
int main(int argc, const char * argv[]) {
const int SIZE = 20;
int playerNum[SIZE], atBats[SIZE], hits[SIZE], runs[SIZE], rbis[SIZE], batsAvg[SIZE], numberOfPlayers, count = 0;
/*Reads the input data into the arrays. You do not know how many lines of input there will be, so you will need an EOF loop, and you will need to keep a count of the number of players stored.*/
numberOfPlayers = loadArrays(playerNum, atBats, hits, runs, rbis, count);
/*Calculates each player's batting average storing the results in the batAvg array. Batting average is computed by dividing hits by at bats. This will result in a percent, multiply the result by 1000 and round to the nearest integer to get the integer batting average.*/
batsAvg[SIZE] = batAvg(hits,atBats,numberOfPlayers);
void printStats(playerNum, atBats, hits, runs, rbis, batsAvg, numberOfPlayers);
return 0;
}
int loadArrays(int player[], int bat[], int hit[], int run[], int rbi[], int count)
{
ifstream statsIn;
statsIn.open("info.txt");
if (statsIn.fail())
// this will alert user if there is a problem opening the file
cout << "Error opening the file\n";
while(!statsIn.eof())
{
statsIn >> bat[count];
statsIn >> hit[count];
statsIn >> run[count];
statsIn >> rbi[count];
count++;
}
statsIn.close();
return count;
}
//calculate batting average
int batAvg(int hits[], int bats[],int count){
int calcAVG = 0;
for (int i=0; i<= count; i++) {
calcAVG = (hits[i] / bats[i]) * 1000;
}
return calcAVG;
}
//display stats
void printStats(int player[], int bat[], int hit[], int run[], int rbi[],int bavg[],int count){
cout<<"Player Num"<<"\t"<<"At Bat"<<"\t"<<"Hits"<<"\t"<<"Runs"<<"\t"<<"Bat Avg"<< endl;
for (int i = 0; i <= count; i++) {
cout<<player[i]<<"\t"<<bat[i]<<"\t"<<hit[i]<<"\t"<<run[i]<<"\t"<<rbi[i]<<"\t"<<bavg[i]<<endl;
}
}
|