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 size);
void PrintTable(string NameArray[],int idArray[],float batAvg[],int walkArray[],int pos);
int FindHigh(float batAvg[], int size);
int FindLow(float batAvg[], int size);
//define global variables
float batAvgRunning = 0;
int size = 20;
int walk = 0;
string NameArray[20];
string batRec;
int idArray[20];
int id[20];
int walkArray[20];
float batAvg[20];
int main()
{
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);
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;
for (x=0;x<size;x++)
{
myIn2 >> id[x];
getline(myIn2,batRec);
ProcessRecord(batRec, batAvgRunning, walk);
batAvg[x] = batAvgRunning;
walkArray[x] = walk;
cout << x;
}
SearchPosition(idArray, id, size);
}
int SearchPosition(int idArray[], int id[], int size)
{
int m;
int i = 0;
int pos;
for (m=0;m<size;m++)
{
i = 0;
while (idArray[m] != id[i])
{
i++;
}
pos = i;
PrintTable(NameArray, idArray, batAvg, walkArray, pos);
}
}
void PrintTable(string NameArray[],int idArray[],float batAvg[],int walkArray[],int pos)
{
int x;
cout << NameArray[x] << " " << idArray[x] << " " << batAvg[pos] << " " << walkArray[pos];
cout << endl;
x++;
}
Ok all I get is a segmentation fault.