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:
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 = newint*[iHeight];
for (int i = 0; i < iHeight; ++i) {
pGrid[i] = newint[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.
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).
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 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.