Chi Blackhawks Array Help

I'm having issues trying to input data from a .txt file into my array program. The whole program takes the name, # of goals and assists, adds them together to get points, and then puts in a given + or - rating. The program works except for the data input.

Also, this is an alphabetical "Unsorted" list, but is there a way to order the list based on point value and be able to display that as well as a separate "Ordered" list?

Thank you for any help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <iomanip>
#include <fstream>

#define NUM_PLAY 30

using namespace std;

int buildArrays(int[], int[], int[]);
void printArrays(string[], int[], int[], int[], int);
void sortArrays(string[], int[], int[], int[], int);

int main()
{
int numPlayers = 0;
int goals[NUM_PLAY];
int assists[NUM_PLAY]; 
int rating[NUM_PLAY];
string players[NUM_PLAY] = {"Bryan_Bickell", "Brandon_Bollig", "Dave_Bolland",
"Sheldon_Brookbank", "Daniel_Carcillo", "Michael_Frolik", "Niklas_Hjalmarsson",
"Marian_Hossa", "Patrick_Kane", "Duncan_Keith", "Marcus_Kruger", "Nick_Leddy",
"Jamal_Mayers", "Johnny_Oduya", "Michal_Rozsival", "Brandon_Saad", "Brent_Seabrook",
"Patrick_Sharp", "Andrew_Shaw", "Viktor_Stalberg", "Jonathan_Toews"};

numPlayers = buildArrays(goals, assists, rating);
sortArrays(players, goals, assists, rating, numPlayers);
printArrays(players, goals, assists, rating, numPlayers);

system ("pause");

return 0; 
}

int buildArrays(int goals[NUM_PLAY], int assists[NUM_PLAY], int rating[NUM_PLAY])
{
ifstream inFile;
int num;
int i;

inFile.open("hockey.txt");

if ( inFile.fail() )
{
cout << "input file did not open";
exit(0);
}

while (inFile)
{
for(i = 0; i < NUM_PLAY; i++)
{
inFile >> num;
goals[i] = num;
inFile >> num;
assists[i] = num;
inFile >> num;
rating[i] = num;
if(inFile.fail())
break; 
}
}

return i;
}

void printArrays(string names[], int goals[], int assists[], int rating[], int numPlayers)
{
int i;
int points = 0;

cout << fixed << showpoint <<setprecision(1) << left;
cout << '\t' << "Chicago Blackhawks Unsorted List";
cout << endl;
cout << endl;
cout << setw(30) << "Players" << setw(10) << "Goals" << setw(10) << "Assists" << setw(10) 
<< "Points" << setw(10) << "Rating";
cout << endl;
cout << "---------------------------------------------------------------------"; 
cout << endl;
for(i = 0; i < numPlayers; i++)
{
points = goals[i] + assists[i];

if(rating[i] == 0)
{
cout << setw(30) << names[i] << setw(10) << goals[i] << setw(10) << assists[i] << setw(10) 
<< points << setw(10) << rating[i] << 0.0 << setw(8) << endl;
}
else
{
cout << setw(30) << names[i] << setw(10) << goals[i] << setw(10) << assists[i] << setw(10) 
<< points << setw(10) << rating[i] << setw(8) << endl;
}
}

}

void sortArrays(string names[], int goals[], int assists[], int shots[], int numPlayers)
{
int top;
int temp = 0;
int ssf;
int ptr;

for(top = 0; top < numPlayers; top++)
{
for(ptr = top, ssf = top; ptr <= numPlayers; ptr++)
{
if(names[ptr] < names[ssf])
{
ssf = ptr; 
names[temp] = names[top];
names[top] = names[ssf]; 
names[ssf] = names[temp]; 
}
}
}
}


And this is the .txt file. Order is Players Name, Goals, Assists, Plus/Minus rating. The points are added when points = goals + assists (Should be in the main coding above).

Bryan_Bickell 2 5 +2
Brandon_Bollig 0 0 0
Dave_Bolland 4 2 -1
Sheldon_Brookbank 0 0 -1
Daniel_Carcillo 0 1 +3
Michael_Frolik 1 1 0
Niklas_Hjalmarsson 0 2 +6
Marian_Hossa 6 6 +6
Patrick_Kane 9 10 +7
Duncan_Keith 1 4 0
Marcus_Kruger 2 2 -1
Nick_Leddy 1 4 +4
Jamal_Mayers 0 0 +1
Johnny_Oduya 0 3 +9
Michal_Rozsival 0 3 +4
Brandon_Saad 1 0 +2
Brent_Seabrook 1 4 +1
Patrick_Sharp 2 9 +3
Andrew_Shaw 2 3 +3
Viktor_Stalberg 3 3 +2
Jonathon_Toews 6 6 +9
Last edited on
When you're using parallel arrays, you cannot just move elements of one array around and call the group of parallel arrays sorted (unless it happens to be an array of indices you're using to index all of the parallel arrays, but that isn't the case here.)

You're sorting the name array (which happens to already be sorted,) and everything else is remaining the same. Those stats will print out in the same order, no matter what the order of the names is.
I see. So when dealing with the points, will the program auto-sort the names based on their total points or do I need to edit it to make that happen?
I figured out how to input the file finally and it sorted correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

#define NUM_PLAY 30

using namespace std;

int buildArrays(string[], int[], int[], int[]);
void printArrays(string[], int[], int[], int[], int);
void sortArrays(string[], int[], int[], int[], int);

int main()
{
int numPlayers = 0;
int goals[NUM_PLAY];
int assists[NUM_PLAY]; 
int rating[NUM_PLAY];
string players[NUM_PLAY];

numPlayers = buildArrays(players, goals, assists, rating);
sortArrays(players, goals, assists, rating, numPlayers);
printArrays(players, goals, assists, rating, numPlayers);

system ("pause");

return 0; 
}

int buildArrays(string playerNames[], int goals[NUM_PLAY], int assists[NUM_PLAY], int rating[NUM_PLAY])
{
ifstream hockeyFile;
	int i = 0, num = 0;
	string name;
	
	hockeyFile.open( "hockey.txt" );

if ( hockeyFile.fail() )
   {
  	 cout << "The hockey.txt input file did not open";
  	 exit (-1);
   }
   
   while( hockeyFile )
   {
	hockeyFile >> name;
	playerNames[i] = name;
	
	hockeyFile >> num;
	goals[i] = num;
	
	hockeyFile >> num;
	assists[i] = num;
	
	hockeyFile >> num;
	rating[i] = num;
	
	i++;
   }
 hockeyFile.close();

 return i;
}

void printArrays(string names[], int goals[], int assists[], int rating[], int numPlayers)
{
int i;
int points = 0;

cout << fixed << showpoint <<setprecision(1) << left;
cout << '\t' << "Chicago Blackhawks Unsorted List";
cout << endl;
cout << endl;
cout << setw(30) << "Players" << setw(10) << "Goals" << setw(10) << "Assists" << setw(10) 
<< "Points" << setw(10) << "Rating";
cout << endl;
cout << "---------------------------------------------------------------------"; 
cout << endl;
for(i = 0; i < numPlayers; i++)
{
points = goals[i] + assists[i];

if(rating[i] == 0)
{
cout << setw(30) << names[i] << setw(10) << goals[i] << setw(10) << assists[i] << setw(10) 
<< points << setw(10) << rating[i] << setw(8) << endl;
}
else
{
cout << setw(30) << names[i] << setw(10) << goals[i] << setw(10) << assists[i] << setw(10) 
<< points << setw(10) << rating[i] << setw(8) << endl;
}
}

}

void sortArrays(string names[], int goals[], int assists[], int rating[], int numPlayers)
{
int top;
int temp = 0;
int ssf;
int ptr;

for(ptr = top, ssf = top; ptr < numPlayers; ptr++)
{
for(top = 0; top <= numPlayers; top++)
{
if(names[ptr] < names[ssf])
{
ssf = ptr; 
names[temp] = names[top];
names[top] = names[ssf]; 
names[ssf] = names[temp]; 
}
}
}
}


How would I go about to re-organize it to show the players in terms of points and print it out on the same screen, just below the first list?
I figured out how to input the file finally and it sorted correctly.


Your input is still wrong, and so is the sorting. What value is top supposed to have when the first for loop begins? Does your compiler warn you about using an uninitialized variable? If it does, pay attention to it. If it doesn't, turn up your warning levels.

Your buildArrays function should look something like:

1
2
3
4
5
6
7
8
9
10
int buildArrays(string playerNames[], int goals[NUM_PLAY], int assists[NUM_PLAY], int rating[NUM_PLAY])
{
    ifstream hockeyFile( "hockey.txt" );

    int i = 0 ;
    while( hockeyFile >> playerNames[i] >> goals[i] >> assists[i] >> rating[i] )
        i++;

    return i;
}


You should check whether an extraction operation was successful as soon as it occurs, not after you've treated it as if it worked (which means, in this case, before i is incremented.) Note that there is no reason to explicitly close the file as the destructor will do that when the function returns.
Last edited on
The initial list is supposed to be unsorted and only alphabetical names. I'm supposed to then sort the array by point values and then print it beneath the unsorted list.

In terms of what is required, I'm already halfway complete. I'm pretty sure next week I'll be dealing with how to sort arrays better and more efficiently
Topic archived. No new replies allowed.