So I have the program written, but for some reason, when I go to compile it, the display box pops up and then disappears right away. Can you please help me to figure out what I did wrong?

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>

using namespace std;

const int array_size=35;

// Setting up all prototypes
int buildArrays( string[], int[], int[] );
void printArrays( string[], int[], int[], int );
void sortArrays( string[], int[], int[], int );

int main()
{
string playerNames[array_size];
int goals[array_size];
int assists[array_size],numPlayers;
numPlayers=buildArrays(playerNames,goals,assists);
cout<<"Chicago Blackhawls UNSORTED Report"<<endl;
cout << endl;
printArrays(playerNames,goals,assists,numPlayers);
sortArrays(playerNames,goals,assists,numPlayers);
cout<<"\nChicago Blackhawls SORTED Report";
cout << endl;
printArrays(playerNames,goals,assists,numPlayers);
system ("pause");
return 0;
}

// Setting up all functions

/*****************************************************************************
int buildArrays( )
This function will read the file of data and fill the three arrays. It
takes as its arguments the array of strings and two arrays of integers.
It returns the number of valid players that were placed in the arrays.
*****************************************************************************/

int buildArrays( string playerNames[], int goals[], int assists[] )
{
ifstream infile;
infile.open("hockey.txt");
if(infile.fail())
{
cout<<"The hockey.txt input file did not open";
exit(-1);
}
int i=0;
while(infile)
{
infile>>playerNames[i];
infile>>goals[i];
infile>>assists[i];
i++;
}
infile.close();
return i-1;
}


/*****************************************************************************
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 the three arrays and
the number of players in the arrays.
*****************************************************************************/

void printArrays( string playerNames[], int goals[], int assists[], int numPlayers )
{
cout<<"Player\t\tGoals\tAssists\tPoints"<<endl;
cout<<"----------------------------------------"<<endl;
 for(int i=0;i<numPlayers;i++)
 cout<<playerNames[i]<<"\t"<<goals[i]<<"\t"<<assists[i]<<"\t"<<goals[i]+assists[i]<<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 the three arrays and the number of players in the arrays.
*****************************************************************************/

void sortArrays( string playerNames[], int goals[], int assists[], int numPlayers )
{
string tmpn;
int tmpg,tmpa;
for(int i=0;i<numPlayers-1;i++)
{
for(int j=i+1;j<numPlayers;j++)
 {
if(goals[i]<goals[j])
 {
tmpg=goals[i];
tmpn=playerNames[i];
tmpa=assists[i];

goals[i]=goals[j];
playerNames[i]=playerNames[j];
assists[i]=assists[j];

goals[j]=tmpg;
playerNames[j]=tmpn;
assists[j]=tmpa;
}
}
}
}

Instead of system pause, I'd recommend something easier on the system itself.

cin.get();

It's basically - "Press and key and <enter> to continue..."

or you could use getch:

#include <conio.h>

getch();

cin.get() is supported in the standard <iostream> header file, good luck!

This article has been dead for over six months. Start a new discussion instead.