How do I initialize 2 arrays (one is 2-dimensional) from 1 input file

I am trying to find out how to initialize 2 arrays (1 one-dimensional and 1 two-dimensional) from 1 input file. This is for a college group project and I would like to know if anyone has a hint or link to something I can reference on how to do this.

I've searched through my text and online, but cannot find what I need yet. I already know how to initialize 2 one-dimensional arrays from 1 input file. This is what I have so far:

infile >> stockSymbol[NUM_STOCKS] >> stockList[NUM_STOCKS][NUM_COLUMNS];

while (infile)
{
int row;

for (row = 0; row < NUM_STOCKS; row++)
{
infile >> stockSymbol[row];

int col;

for (col = 0; col < NUM_COLUMNS; col++)
infile >> stockList[row][col];
} // end for

} // end while
Do you know how big your array is going to be?

If it's a static sized array you can use
int stockList[10][10]; etc
Then you can use memset() to set the values.

If it's a dynamically sized array something like this is more appropriate.
1
2
3
4
5
int **pGrid = 0;

pGrid = new int*[iHeight];
for (int i = 0; i < iHeight; ++i) {
 pGrid[i] = new int[iWidth];


The only problem you will have here, is that if your 2D Array is storing a class you cannot use a non-default constructor. This is some thing with the C++ ISO standard.

Just remember you need to clear your memory using delete when you have finished with it.

HTH.
Z.

Edit: Here is a link that will show you how to create your own Array2D type. This is sometimes used, but in the end will achieve the same thing as the above code. Just with more work.
http://en.allexperts.com/q/C-1040/creating-2D-array-dynamically.htm
Last edited on
Yes, the 2D array will be 10 rows and 6 columns [10][6]. I'm sorry I didn't mentions that I was using named constants of 10 and 6.

I don't understand your comment here:

"The only problem you will have here, is that if your 2D Array is storing a class you cannot use a non-default constructor. This is some thing with the C++ ISO standard."

We have not yet covered classses. Thank you for the information, and I will save it. We have not yet covered vectors either (only a brief introduction).
Ok.

Then you can use something like
 
int stockList[10][6] = {0}; // Create Array, All = 0 
I've done that already and it's already in my code. I just didn't include it with my code posted. I need to initialize 2 arrays (1 one-dimensional and 1 two-dimensional) from 1 input file. Please check my code and you'll get the idea of what I am trying to do.
Your code looks basically OK but whether it is going to work or not depends on the layout of the file you are trying to read.

Could you post the file? It would help a lot.
Last edited on
It is a text file exactly like below in format and content.

ABC 123.45 130.95 132.00 125.00 120.50 10000
MSET 120.00 140.00 145.00 140.00 115.00 30920
AOLK 80 75 82 74 83 5000
CSCO 100 102 105 98 101 25000
IBD 68 71 72 67 75 15000
CWC 200 210 215 172 175 25000
DMV 127.9 46.3 197.2 200.5 142.02 20071
OMG 140 136 117 33 53 900
BGR 120.9 114.67 132.79 16.08 15.74 50000
UWW 402 461 403 127 196 100000
Your code looks fine. 1 think I would personally change is your for loops so they look like for (int row = 0; row < ROWCOUNT; ++row) so you declare row as an int in the loop instead of before it.

Another way you could do it would be to use the getline() function to read a line from your file, from there you can split the line up and assign various elements in your array with the values from it.
Last edited on
Topic archived. No new replies allowed.