not sure what the difference is between the code for a standard array

7 Replies - 4171 Views - Last Post: 09 May 2008 - 04:27 AMRate Topic:

Reading file into multi dimensional array

Posted 09 May 2008 - 02:51 AM

Hello, again I have hit a brick wall. the code below is how I believe this should be done. It works when I read into a standadr array but not for a multi dimensional array. Any help would be greatly appreciated.

Replies To: Reading file into multi dimensional array

Re: Reading file into multi dimensional array

Posted 09 May 2008 - 03:33 AM

OK, well the best way to explain this would be:
You have 3 arrays which contain 8 elements each (account[3][8])
But, when it comes to reading the file, you are just trying to read directly into the array of 3. The way that you should think about doing it is something like this:

int counter1 = 0; // this will go up to 3, for the first array
int counter2 = 0; // this will go up to 8 for each array, then reset
while (counter1 < 3)
{
myfile >> account[counter1][counter2];
counter2 ++;
if (counter2 = 8) // reset the counter to read in the next set
{
counter2 = 0; // reset to read from the beginning
counter1 ++; // increment counter1 to read the next set
}
}

Re: Reading file into multi dimensional array

Posted 09 May 2008 - 03:46 AM

Thank you for your help. It now appears that using the code you gave me the file is being read into the array. however when I try to display anything to the screen from the array I get a figure that is not contained in the file "finances.txt". the figure I get is -107374176.0000, could this be an address?

I hade to replace Gwin.command with the standard stuff, you shouldn't have a problem rewriting it, but if you do, let me know and we'll go through it

By the way, you have a line that looks like this: exit(1); //an error code to denote what has gone wrong
That parameter (1) isn't an error code, it is a timer - the exit function will exit the program after a number of milliseconds, determined within the brackets. So, to exit after 1 second, it would be exit(1000);