Completely Lost On reading Data Into Array

I'm having a lot of trouble on how I go about writing data from a file into multiple arrays. The data file contains five columns and 10 rows and looks like this:

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

Each column should be a separate array. My trouble is I have no clue on how to set up the data file (or if I need to at all ie, have the string then integers in same row, or leave as is?). Then, I haven't found a way to read the data from the file into each of the separate arrays.

Any guidance or help would be greatly appreciated.
Reading to/from a file is actually really cool when you think about what all you can start doing with it. Here is the link to this website's little article, it has helped me recently just make sure you understand the streams, their uses, and then the structure of the example programs:


http://www.cplusplus.com/doc/tutorial/files/


I suggest learning how to do that first, make a working function, and then you can create a file with the numbers in it and learn how to read them from the file / write to a new one.

I'm working on a program before my semester starts that has to do something similar. Hope this helps.
int arr[5][10];

for(10 times)
{
in>> arr[1][i];
in >> arr[2][i];
.
.
in>>arr[5][i]
}
So anup30 didn't explicitly state it, but preferring you use a double (or two-dimensional) array, which is probably the simplest method to work with, assuming "in" is the file input stream object you are defining. I use "incoming" for incoming from a file and "outgoing" for outgoing to a file myself.

Another solution could be to simply store them sideways, so it would be 5 rows with 10 columns, but taking in each data member belonging to each single array at a time.
SO:
int column;
for (column = 0; column < 10; column++)
{
incoming >> playerNumber[column];
}
for (column = 0; column < 10; column++)
{
incoming >> atBats[column])
}
etc., but it doesn't look as clean as using a 2D array.
This makes a known number of iterations for each array.

A third solution is simply alternating between arrays for input something like:

for(int row = 0; row < 10; row++)
{
incoming >> playerNumber[row];
incoming >> atBats[row];
incoming >> hits[row];
incoming >> runs[row];
incoming >> rbi[row];
}

Hope this helps. Please let me know if this is unclear. I tend to type faster than I think sometimes.

P.S. Looking it over again, it could also be easiest to have a player class and have each category be a stat of a player object, rather than them being split up into different arrays, but I am assuming you haven't made it into classes yet....so disregard this P.S.?
So I now have something set up, but I can't seem to get the numbers to read in properly. My data file looks like:

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

The problem is when I try displaying the first two arrays, it just shows 0's. I know there's something now right with my while loop, but I don't know what it is.

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
//
//
//

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

void loadArrays(int[],int[],int[],int[],int[],int[], const int, string);

int main() {
    
    ofstream statsFileIn;
    
    const int SIZE = 20;
    int playerNum[SIZE], atBats[SIZE], hits[SIZE], runs[SIZE], rbis[SIZE], batAvg[SIZE];
    string file = "baseballStats.doc";
    
    loadArrays(playerNum, atBats, hits, runs, rbis, batAvg, SIZE, file);
    
    return 0;
}

void loadArrays(int playerNum1[], int atBats1[], int hits1[], int runs1[], int rbis1[], int batAvg1[], const int SIZE1, string file1 )
{
    
    ifstream statsFileIn;
    statsFileIn.open(file1);
    
    
    int i = 0;
    int numOfPlayers = 0;
    
    while (statsFileIn >> playerNum1[i] >> atBats1[i] >> hits1[i] >> runs1[i] >> rbis1[i])
    {
        i = 0;
        
        numOfPlayers = numOfPlayers + 1;
        
        statsFileIn >> playerNum1[i] >> atBats1[i] >> hits1[i] >> runs1[i] >> rbis1[i];
        
        i++;
    
    }
    
    statsFileIn.close();
    
    for (i = 0; i < SIZE1; i++)
    {
        cout<<playerNum1[i]<< " " <<atBats1[i]<<endl;
    }
    
}
Not sure, but I THINK it is the condition you setup might be affecting it? Take a look at this page, which basically says how to check for the end of the file's data you are looking at:

http://mathbits.com/MathBits/CompSci/Files/End.htm

....OK, that may have been helpful to you anyway, but I also just realized that INSIDE that while loop, you are setting i to 0 at the beginning in each iteration, so you are always doing i = 0 for each time through. You initialized i just before the loop at 0, so the line is simply unnecessary inside it.

Hope it was helpful!
line 39, i = 0;
Topic archived. No new replies allowed.