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;
}
}
}
}

Dani AI

Generated

The behavior described is almost always caused by the program terminating before the final pause is reached. In this code that happens for two definite reasons visible in the posted source: the call to exit(-1) inside buildArrays will end the process immediately if the input file fails to open, and the input loop as written is fragile (it increments i even when a read fails), which risks returning an incorrect count or writing past the fixed 35-element arrays and invoking undefined behavior.

Quick diagnostics:

  • Run the compiled EXE from a command prompt so any error message stays visible. If the file open fails, the program will have already called exit and closed the window.
  • Verify the input file is in the program's working directory (IDE run-folder) or use an absolute path for testing.
  • Enable compiler warnings and run under a debugger or address-sanitizer to catch out-of-bounds writes.

A safer read pattern (avoids the extra increment and allows variable-length input) is to only accept a record after a successful combined extraction or to use dynamic containers. For example:

vector<string> names;
vector<int> goals, assists;
string nm; int g, a;
while (cin_file >> nm >> g >> a) {
  names.push_back(nm);
  goals.push_back(g);
  assists.push_back(a);
}

Notes tied to earlier replies: is correct that proper headers are needed for standard functions like exit/system; s suggestion to avoid OS-specific pause calls is preferable for portability; correctly points out that conio.h is non-standard and platform-specific. Final checklist: ensure the file opens, guard array bounds (or use vectors), replace the fragile input loop with a read-that-tests-success, and avoid reliance on system/platform-only headers.

Recommended Answers

All 4 Replies

You forgot to include
#include <cstdio>
#include <cstdlib>

Need it for system("PAUSE");

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.