So I have the code written, and it is compiling fine. But for some reason, when I compile it and it displays the results, the way it displays isn't the way it should be. It is supposed to pull information in from a binary file.
Here is the link to the file. http://faculty.cs.niu.edu/~byrnes/csci240/pgms/binary_hockey.txt
Here is the code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <math.h>
using namespace std;
// Setting up the structure
struct playerInfo
{
char playerName[25];
int goalsScored;
int assist;
};
// Setting up a symbolic constant for the array size
const int array_size=35;
// Setting up all prototypes
int buildArrays( playerInfo[]);
void printArrays( playerInfo[],int );
void sortArrays( playerInfo[],int);
void calcStats(playerInfo[], int, double &, double & );
int main()
{
// Declaring variables and setting the array size
playerInfo players[array_size];
int numPlayer;
double averageGoals, averageAssists;
numPlayer = buildArrays( players );
// Displays the header for the unsorted report
cout << "Chicago Blackhawks UNSORTED Report" << endl;
cout << endl;
// Calling statments for the printArrays and sortArrays function
printArrays( players, numPlayer );
sortArrays( players, numPlayer );
// Displays the header for the sorted report
cout << "\nChicago Blackhawks SORTED Report";
cout << endl;
// Calling statement for the printArrays function
printArrays( players, numPlayer );
// Calling statement for the calcStats function
calcStats( players, numPlayer, averageGoals, averageAssists );
// Displays the average number of goals and assists and pulls
// in the information from the calcStats function
cout << endl << "Blackhawks Statistics" << endl;
cout << "---------------------" << endl;
cout << endl << "Average Number of Goals Scored " << averageGoals << endl;
cout << "Average Number of Assists " << averageAssists << endl;
system ("pause");
return 0;
}
/*****************************************************************************
int buildArrays( )
This function will read the file of data and fill in the structure array. It
takes as its arguments the array of playerInfo. It returns the number of valid
players that were placed in the arrays.
*****************************************************************************/
int buildArrays( playerInfo playersAr[] )
{
ifstream inFile;
int cnt = 0;
playerInfo tempPlayer;
inFile.open( "binary_hockey.txt", ios::binary );
// If statment to execute if the binary hockey file does not open
if( inFile.fail() )
{
cout<<"The binary_hockey.txt input file did not open";
exit(-1);
}
inFile.read( (char *) &tempPlayer, sizeof(playerInfo) );
// Loop to execute while in the binary hockey file
while( inFile )
{
playersAr[ cnt ] = tempPlayer; // Places the player record into the players array
cnt++;
inFile.read( (char *) &tempPlayer, sizeof(playerInfo) ); // Gets the next player from input using the read function
}
inFile.close();
// Returns the number of players that were placed in the array
return cnt;
}
/*****************************************************************************
void printArrays( )
This function will display the information for the players. For each player,
display the player name, number of goals scored, number of assists, and the
number of points. This function takes as its arguments structure playerInfo
arrays and the number of players in the in player array.
*****************************************************************************/
void printArrays( playerInfo playersAr[], int numPlayer )
{
// Declaring variables
int sub;
// Format the heading for the table
cout << setw(20) << left << "Player" << right << "\tGoals\tAssists\tPoints" << endl;
cout << "----------------------------------------" << endl;
for( sub = 0; sub < numPlayer; sub++ )
{
// Displaying the results in a table format
cout << left << setw(18) << playersAr[sub].playerName
<< "\t" << playersAr[sub].goalsScored
<< "\t" << playersAr[sub].assist
<< "\t" << playersAr[sub].goalsScored+playersAr[sub].assist << endl;
}
cout << "----------------------------------------" << endl;
}
/*****************************************************************************
void sortArrays( )
This function will sort the arrays in DESCENDING order based on the number
of goals. Use the selection sort algorithm presented in lecture. This function
takes as its arguments playerInfo array and the number of players in the arrays.
*****************************************************************************/
void sortArrays( playerInfo playersAr[], int numPlayer )
{
// Declaring all variables
int sub1, sub2, sub3;
playerInfo tempPlayer;
// Creating a for loop to sort the player information
for( sub1 = 0; sub1 < numPlayer; sub1++ )
{
for( sub3 = sub1, sub2 = sub1 + 1; sub2 < numPlayer; sub2++ )
{
if(strcmp(playersAr[sub2].playerName, playersAr[sub3].playerName))
sub3 = sub2;
}
// Sorting the players in alphabetical order
tempPlayer = playersAr[sub3];
playersAr[sub3] = playersAr[sub1];
playersAr[sub1] = tempPlayer;
}
}
/*****************************************************************************
void calcStats( )
This function will calculate average number of goals and average number of
assists. This function takes as its arguments playerInfo array and the number
of players in the arrays,reference of average goals and reference of average
assists.
*****************************************************************************/
void calcStats( playerInfo playersAr[], int numPlayer, double &averageGoals,double &averageAssists )
{
// Declaring and initializing variables
int totalGoals = 0, totalAssists = 0;
int sub;
for( sub = 0; sub < numPlayer; sub++ )
{
// Adds up the amount of goals and assists
totalGoals += playersAr[sub].goalsScored;
totalAssists += playersAr[sub].assist;
}
// Calculate the average goals and average assists
averageGoals = (double) totalGoals / numPlayer;
averageAssists = (double) totalAssists / numPlayer;
}
And this is what the display results should look like:
Chicago Blackhawks UNSORTED Report
Player Goals Assists Points
----------------------------------------------------
Jonathon_Toews 30 41 71
Patrick_Sharp 34 34 68
Patrick_Kane 24 38 62
Marian_Hossa 21 26 47
Tomas_Kopecky 13 27 40
Brent_Seabrook 4 35 39
Duncan_Keith 5 32 37
Dave_Bolland 15 22 37
Bryan_Bickell 16 20 36
Troy_Brouwer 17 17 34
Michael_Frolik 10 24 34
Brian_Campbell 4 21 25
Viktor_Stalberg 11 10 21
Jake_Dowell 6 14 20
Chris_Campoli 3 14 17
Fernando_Pisani 7 9 16
Niklas_Hjalmarsson 3 6 9
Jassen_Cullimore 0 8 8
Ryan_Johnson 1 5 6
Nick_Leddy 3 0 3
Jeremy_Morin 2 1 3
Jordan_Hendry 1 0 1
John_Scott 0 1 1
Jeff_Taffe 0 0 0
Evan_Brophey 0 0 0
Rob_Klinkhammer 0 0 0
Ben_Smith 0 0 0
Brandon_Pirri 0 0 0
Corey_Crawford 0 1 1
Marty_Turco 0 0 0
Chicago Blackhawks SORTED Report
Player Goals Assists Points
----------------------------------------------------
Ben_Smith 0 0 0
Brandon_Pirri 0 0 0
Brent_Seabrook 4 35 39
Brian_Campbell 4 21 25
Bryan_Bickell 16 20 36
Chris_Campoli 3 14 17
Corey_Crawford 0 1 1
Dave_Bolland 15 22 37
Duncan_Keith 5 32 37
Evan_Brophey 0 0 0
Fernando_Pisani 7 9 16
Jake_Dowell 6 14 20
Jassen_Cullimore 0 8 8
Jeff_Taffe 0 0 0
Jeremy_Morin 2 1 3
John_Scott 0 1 1
Jonathon_Toews 30 41 71
Jordan_Hendry 1 0 1
Marian_Hossa 21 26 47
Marty_Turco 0 0 0
Michael_Frolik 10 24 34
Nick_Leddy 3 0 3
Niklas_Hjalmarsson 3 6 9
Patrick_Kane 24 38 62
Patrick_Sharp 34 34 68
Rob_Klinkhammer 0 0 0
Ryan_Johnson 1 5 6
Tomas_Kopecky 13 27 40
Troy_Brouwer 17 17 34
Viktor_Stalberg 11 10 21
Blackhawks Statistics
---------------------
Average Number of Goals Scored 7.67
Average Number of Assists 13.53
My results are not displaying like that at all. Please help!