Help!!! trouble with Array loading and Debugging

I'm having some issues with this project I've got a baseline of code but my void is reading as incomplete. I'm thinking that it might have to do with the loading function. I have attached the project premise... I'llfigure out the comments and what not after I get the initial parts working....HELP!

Instructions:
Your college baseball team is rated number one in the state and the coach would like to keep it that way. After discovering that you know how to program, the coach has asked that you write a program to generate some important statistics about the players. With this information, the coach hopes to spot and improve any of the player's weak areas and thus keep the team's top ranking.
The coach wants you to store all the player information in an input file with the following data:
Player Number At Bats Hits Runs RBIs
10 36 25 2 5
2 12 5 0 1
34 18 7 1 0
63 41 20 4 2
12 10 3 1 0
14 2 1 1 1
27 55 27 10 8
8 27 12 3 4
42 32 8 2 1
33 19 4 1 0
Requirements:
1. Use an input file containing the defined data
2. Store all input information in parallel arrays.
3. The following arrays will be needed in your program:
a. Int playerNum[SIZE],
b. atBats[SIZE],
c. hits[SIZE],
d. runs[SIZE],
e. rbis[SIZE],
f. batAvg[SIZE];
g. where size is a defined constant with a value of 20.
4. Create the following functions
a. loadArrays - Reads the input data into the arrays. You do not know how many lines of input there will be, so you will need an EOF loop, and you will need to keep a count of the number of players stored.
2
b. batAvg - Calculates each player's batting average storing the results in the batAvg array. Batting average is computed by dividing hits by at bats. This will result in a percent, multiply the result by 1000 and round to the nearest integer to get the integer batting average.
c. printStats – Print a neatly formatted table with headings and one output line for each player containing player number, at bats, hits, runs RBIs, batting average and a comment about the player. Use the following scale to determine the comment:
Bat AvgComment
500-1000 WORLD SERIES
300-499 FARM LEAGUE
0-299 LITTLE LEAGUE
5. Use functions as you see fit to:
a. Calculate and output the total at bats, hits, runs, and rbis, and the team's batting average. Figure the team's batting average by dividing the total hits by the total at bats then multiplying by 1000 and rounding as previously described.
b. Output the best and worst values (and which player achieved them) for batting average, hits, and runs.
6. Functions must pass parameters and return values as needed.


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

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

//functions...
int loadArrays(int [], int [], int [], int [], int [], int);
int batAvg(int[], int[], int);
void printStats(int [], int [], int [], int [], int [],int[], int);



int main(int argc, const char * argv[]) {
   
    const int SIZE = 20;
    int playerNum[SIZE], atBats[SIZE], hits[SIZE], runs[SIZE], rbis[SIZE], batsAvg[SIZE], numberOfPlayers, count = 0;
   
    
    /*Reads the input data into the arrays. You do not know how many lines of input there will be, so you will need an EOF loop, and you will need to keep a count of the number of players stored.*/
    
    numberOfPlayers = loadArrays(playerNum, atBats, hits, runs, rbis, count);
    
   /*Calculates each player's batting average storing the results in the batAvg array. Batting average is computed by dividing hits by at bats. This will result in a percent, multiply the result by 1000 and round to the nearest integer to get the integer batting average.*/

    
    batsAvg[SIZE] = batAvg(hits,atBats,numberOfPlayers);
    

    void printStats(playerNum, atBats, hits, runs, rbis, batsAvg, numberOfPlayers);
   
    
    
    
    
    return 0;
}

int loadArrays(int player[], int bat[], int hit[], int run[], int rbi[], int count)
{
    ifstream statsIn;
    statsIn.open("info.txt");
    if (statsIn.fail())
        // this will alert user if there is a problem opening the file
        cout << "Error opening the file\n";
    while(!statsIn.eof())
    {
        statsIn >> bat[count];
        statsIn >> hit[count];
        statsIn >> run[count];
        statsIn >> rbi[count];
        count++;
    }
    statsIn.close();
    return count;
}

//calculate batting average
int batAvg(int hits[], int bats[],int count){
      int calcAVG = 0;
    
    for (int i=0; i<= count; i++) {
      
         calcAVG = (hits[i] / bats[i]) * 1000;
    }
        return calcAVG;

}
//display stats
void printStats(int player[], int bat[], int hit[], int run[], int rbi[],int bavg[],int count){

    cout<<"Player Num"<<"\t"<<"At Bat"<<"\t"<<"Hits"<<"\t"<<"Runs"<<"\t"<<"Bat Avg"<< endl;
    
    for (int i = 0; i <= count; i++) {
         cout<<player[i]<<"\t"<<bat[i]<<"\t"<<hit[i]<<"\t"<<run[i]<<"\t"<<rbi[i]<<"\t"<<bavg[i]<<endl;
    }
   
}
I'm having some issues with this project I've got a baseline of code but my void is reading as incomplete.


Your void is reading as incomplete. In C++, void is a type name meaning "nothing", so we don't know what you mean when your "nothing" is reading as incomplete. Perhaps you can be more specific.

In loadArrays if the condition on line 44 is true, then the condition on line 47 will always be true, resulting in an infinite loop that clobbers memory you have no business accessing (and you shouldn't be looping on eof anyway.) You also make no effort to ensure you don't overrun the bounds of the arrays.
Last edited on
The only place where count is used is inside loadArrays, so make it a local variable there.

batAvg() is supposed to store the result in the batAvg array. This is confusing because the assignment says that the function and the array should have the same name. Let's change the function to calcBatAvg() instead:

Line 49: your aren't reading the player ID. Also, the problem with checking for EOF is that the object won't realize it's at the end of the file until it tries to read past it. To get around this, try reading a whole record at once and then increment count if you succeed:
1
2
3
4
5
6
7
    while( statsIn >> player[count]
           >> bat[count]
           >> hit[count]
           >> run[count]
           >> rbi[count]) {
        count++;
    }

The loops at lines 63 & 75 should go until i<count, not i<=count.

Integer division rounds off the value, so at line 65:
(hits[i] / bats[i]) * 1000;
the division always results in 0. You can avoid this by simply doing the multiplication first:
 
hits[i] * 1000 / bats[i];


Line 31 is a common (and nasty) syntax error. By specifying the return type, this line declares the function printStats(), and does it incorrectly. You want to call the function:
printStats(playerNum, atBats, hits, runs, rbis, batsAvg, numberOfPlayers);

STILL TODO: you need to round off the batting average.

Putting this all together and cleaning up the format a little:

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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

//functions...
int loadArrays(int [], int [], int [], int [], int []);
void calcBatAvg(int[], int[], int[], int);
void printStats(int [], int [], int [], int [], int [],int[], int);



int main(int argc, const char * argv[]) {

    const int SIZE = 20;
    int playerNum[SIZE], atBats[SIZE], hits[SIZE],
        runs[SIZE], rbis[SIZE], batAvg[SIZE],
        numberOfPlayers;


    /* Reads the input data into the arrays. You do not know how many
       lines of input there will be, so you will need an EOF loop, and
       you will need to keep a count of the number of players stored. */

    numberOfPlayers = loadArrays(playerNum, atBats, hits, runs, rbis);

    /*Calculates each player's batting average storing the results in
      the batAvg array. Batting average is computed by dividing hits
      by at bats. This will result in a percent, multiply the result
      by 1000 and round to the nearest integer to get the integer
      batting average.*/

    calcBatAvg(hits, atBats, batAvg, numberOfPlayers);

    printStats(playerNum, atBats, hits, runs, rbis,
               batAvg, numberOfPlayers);
    return 0;
}


int loadArrays(int player[], int bat[], int hit[], int run[], int rbi[])
{
    int count = 0;
    ifstream statsIn;
    statsIn.open("info.txt");
    if (statsIn.fail()) {
        // this will alert user if there is a problem opening the file
        cout << "Error opening the file\n";
        return 0;
    }
    while( statsIn >> player[count]
           >> bat[count]
           >> hit[count]
           >> run[count]
           >> rbi[count]) {
        count++;
    }
    statsIn.close();
    return count;
}

//calculate batting average
void calcBatAvg(int hits[], int bats[], int batAvg[], int count){
    for (int i=0; i< count; i++) {
        batAvg[i] = (hits[i] * 1000 / bats[i]);
    }
}


//display stats
void printStats(int player[], int bat[], int hit[], int run[], int rbi[],int ba\
vg[],int count){

    cout<<"Player Num"<<"\t"<<"At Bat"<<"\t"<<"Hits"<<"\t"<<"Runs"<<"\t"<<"Bat \
Avg"<< endl;

    for (int i = 0; i < count; i++) {
        cout<<player[i]<<"\t"<<bat[i]<<"\t"<<hit[i]<<"\t"<<run[i]<<"\t"<<rbi[i]\
<<"\t"<<bavg[i]<<endl;
    }
}

Dhayden,
This was a big help and maybe you can help on one more thing or anyone else, but now that I'm not facing any errors I see that the file is failing to open. if anyone could shed some light on that, seeing as the code looks pretty good.
I see that the file is failing to open

Assuming the filename is correct, most likely the path is not as expected: you placed the file in one location, the program is looking for it in another location.
One answer is to use a fully-qualified path and filename.

Another might be to create a uniquely-named output file from your program, and see where your program puts it, then move your input file to that same location.
I've made some changes to my code but I still can't get the file to open.
I'm not sure how to use a fully qualified path and file name.

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

//functions...
int loadArrays(int [], int [], int [], int [], int []);
void calcBatAvg(int[], int[], int[], int);
void printStats(int [], int [], int [], int [], int [],int[], int);
void teamAVG(int[], int[], int[],int[], int);
void best(int[], int[],int);
void worst(int[], int[], int);

const int SIZE = 20;
int playerNum[SIZE];
int atBats[SIZE], hits[SIZE],runs[SIZE], rbis[SIZE], batAvg[SIZE];


int main(int argc, const char * argv[]) {
    
    
    int numberOfPlayers;

    
    /* Reads the input data into the arrays.  */
    
    numberOfPlayers = loadArrays(playerNum, atBats, hits, runs, rbis);
    
    /*Calculates each player's batting average */
    
    calcBatAvg(hits, atBats, batAvg, numberOfPlayers);
    
   //Displays data
    printStats(playerNum, atBats, hits, runs, rbis,batAvg, numberOfPlayers);
    
    //Calculate and output teams Average, find best and worst
    teamAVG(atBats, hits,runs,rbis, numberOfPlayers);
    best(batAvg, playerNum ,numberOfPlayers);
    worst(batAvg, playerNum ,numberOfPlayers);
    
    
    return 0;
}

//load array
int loadArrays(int player[], int bat[], int hit[], int run[], int rbi[])
{
    int count = 0;
    ifstream statsIn;
    //open file
    statsIn.open("statsFile.rtf");
   
    // this will alert user if there is a problem opening the file
    if (statsIn.fail()) {
        cout << "Error opening the file\n";
        return 0;
    }
    
    while( statsIn >> player[count]>> bat[count]>> hit[count]>> run[count]>> rbi[count])
    { count++;}
    
    statsIn.close();
    return count;
}

//calculate batting average
void calcBatAvg(int hits[], int bats[], int batAvg[], int count){
    for (int i=0; i< count; i++) {
        batAvg[i] = (hits[i]  / bats[i]) * 1000;
    }
}


//display stats
void printStats(int player[], int bat[], int hit[], int run[], int rbi[],int bavg[],int count){
    
    
    
    cout<<"Player Num"<<"\t"<<"At Bat"<<"\t"<<"Hits"<<"\t"<<"Runs"<<"\t"<<"Bat Avg"<<"\t"<<"Comment"<< endl;
    
    
    for (int i = 0; i < count; i++) {
        string comment;
        if(bavg[i] >= 500 && bavg[i]>=1000){ comment = "World Series";}
        else if (bavg[i] < 500 && bavg[i]>=300){comment = "Farm League";}
        else if (bavg[i] < 300){comment = "Little League";}
        else{comment ="Not rated";}
        
        cout<<player[i]<<"\t"<<bat[i]<<"\t"<<hit[i]<<"\t"<<run[i]<<"\t"<<rbi[i]<<"\t"<<bavg[i]<<"\t"<< comment <<endl;
    }
}

//team AVG
void teamAVG(int bat[], int hit[], int run[],int rbi[], int count){
 
 
 int btotal = 0, htotal = 0, rtotal = 0, rbtotal= 0, tavg = 0;
 
 for (int i = 0; i<count; i++) {
 btotal += bat[i];
 htotal += hit[i];
 rtotal += run[i];
 rbtotal += rbi[i];
 }
 tavg = (htotal/btotal) *1000;
 
 cout<<"Team"<<"\t"<<btotal<<"\t"<<htotal<<"\t"<<rtotal<<"\t"<<rbtotal<<"\t"<<tavg<<"\t"<<"N/A";
 cout<<"Teams Batting AVG: "<< tavg<< endl;
 
 
 }

 //finding best
 void best(int bavg[], int num[], int count){
 int i, maxIndex = 0;
 int max = bavg[0];
 
 for(i=0; i < count; i++)
 {
 if(max > bavg[i])
 {max = bavg[i];
 maxIndex = i;}
 }
 cout<< "Best Player in PLayer #: "<< num[maxIndex]<<" With a Batting AVG of: "<< bavg[maxIndex];
 
 
 }

//finding worst
 void worst(int bavg[], int num[], int count){
 
 int i, minIndex = 0;
 int min = bavg[0];
 
 for(i=0; i < count; i++)
 {
 if(min < bavg[i])
 {min = bavg[i];
 minIndex = i;}
 }
 cout<< "Worst Player in PLayer #: "<< num[minIndex]<<" With a Batting AVG of: "<< bavg[minIndex];
 
 }

I'm not sure how to use a fully qualified path and file name.

Depends on your operating system, on Windows it might be something like statsIn.open("C:/myfiles/info.txt") rather than just statsIn.open("info.txt"). But only you know what the correct path will be, what I put was just a random example. Use whatever drive and directory name is the correct one for your file.

My other suggestion was to do something like this:
ofstream test_output("StoriesOfRen123456789.txt");
which will create an empty file named "StoriesOfRen123456789.txt". Then use the search command to find where on your system the file has been placed - that is the location where your input file should be, if you want to use an unqualified file name such as "info.txt".


By the way, it's probably not a good idea to use a file of type .rtf as that will contain additional formatting information which could complicate matters.
I'm using a mac and I switched to a .txt file. But I have to zip it to turn in so having a fully qualified path may not work
Ok thanks for the info. I'm not familiar with the mac environment. However, so long as you can identify the current working directory for the program, you should be able to run your program with just the simple filename.
> Another might be to create a uniquely-named output file from your program,
> and see where your program puts it,
http://linux.die.net/man/3/getcwd
https://msdn.microsoft.com/en-us/library/windows/desktop/aa364934(v=vs.85).aspx


> But I have to zip it to turn in so having a fully qualified path may not work
the suggestion is for you to find out where's the error in your program.
If it works witha full path, then your relative path is incorrect.


Also, don't double post http://www.cplusplus.com/forum/general/182816/
Topic archived. No new replies allowed.