Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
void ReadPlayerInfo(ifstream& myIn, string nameArray[], int idArray[], int& size);
void ReadProcessBatting (ifstream& myIn2, int id [], float batAvg [], int walkArray[],int &size);
void ProcessRecord(string batRec, float& batAvgRunning, int& walk);
int SearchPosition(int idArray[], int id[],int firstID, int& pos);
void PrintTable(string nameArray[],int idArray[],float batAvg[],int walkArray[], int counter, int pos);
int FindHigh(float batAvg[], int size);
int FindLow(float batAvg[], int size);
//define global variables
int main()
{
int size = 20;
int pos = 0;
string nameArray[20];
string batRec;
int idArray[20];
int id[20];
int firstID = 0;
int walkArray[20];
float batAvg[20];
int i;
//Begin opening files
string PlayerRecordsFile;
string PlayerBattingFile;
ifstream myIn;
ifstream myIn2;
cout << "Enter file name that contains player records: ";
cin >> PlayerRecordsFile;
myIn.open(PlayerRecordsFile.c_str());
while (!myIn)
{
cout << "Enter file name that contains player records: ";
cin >> PlayerRecordsFile;
myIn.open(PlayerRecordsFile.c_str());
}
ReadPlayerInfo(myIn, nameArray, idArray, size);
cout << "Enter file name that contains batting data: ";
cin >> PlayerBattingFile;
myIn2.open(PlayerBattingFile.c_str());
while (!myIn2)
{
cout << "Enter file name that contains batting data: ";
cin >> PlayerBattingFile;
myIn2.open(PlayerBattingFile.c_str());
}
cout << "Player Name " << "ID " << "Batting Average " << "Number of Walks" << endl;
cout << "--------------------------------------------------------------------------" << endl;
ReadProcessBatting(myIn2, id, batAvg, walkArray, size);
for(i=0;i<size - 1;i++)
{
SearchPosition(idArray, id,firstID, pos);
firstID++;
PrintTable(nameArray, idArray, batAvg, walkArray, firstID, pos);
}
myIn.close();
myIn.clear();
myIn2.close();
myIn2.clear();
return 0;
}
void ReadPlayerInfo(ifstream& myIn, string nameArray[], int idArray[], int& size)
{
string playerName;
int playerNumber;
int i;
for (i=0;i<size;i++)
{
myIn >> playerName;
nameArray[i] = playerName;
myIn >> playerNumber;
idArray[i] = playerNumber;
}
}
void ProcessRecord(string batRec, float& batAvgRunning, int& walk)
{
int x = 0;
walk = 0;
char choice;
float hit = 0;
float out = 0;
while (x < batRec.length())
{
choice = batRec[x];
x++;
switch(choice)
{
case 'O':
out++;
break;
case 'H':
hit++;
break;
case 'W':
walk++;
break;
default:
break;
}
}
batAvgRunning = (hit/(out+hit));
}
void ReadProcessBatting(ifstream& myIn2, int id [], float batAvg [], int walkArray[],int &size)
{
int x;
string batRec;
float batAvgRunning;
int walk;
for (x=0;x<size;x++)
{
myIn2 >> id[x];
getline(myIn2,batRec);
ProcessRecord(batRec, batAvgRunning, walk);
batAvg[x] = batAvgRunning;
walkArray[x] = walk;
}
}
int SearchPosition(int idArray[], int id[], int firstID, int& pos)
{
int b = 0;
while (idArray[firstID] != id[b] && b < 19)
{
b++;
}
pos = b;
return pos;
}
void PrintTable(string nameArray[],int idArray[],float batAvg[],int walkArray[],int counter, int pos)
{
cout << setw(5) << nameArray[counter] << " ";
cout << setw(5) << idArray[counter] << " ";
cout << setprecision(3) << setw(5) << batAvg[pos] << " ";
cout << setw(5) << walkArray[pos];
cout << endl;
}
If you run this I get 19 of the 20 players to show up... The ID 3 is not showing idk why...