Computer Science Canada Question regrading arrays, functions and file i/o |
Author: | JR [ Thu Nov 04, 2004 7:59 pm ] | ||
Post subject: | Question regrading arrays, functions and file i/o | ||
ok lets say i got something like this
ok this is the function basically. now what i want to do is when lets say u press 1. it loads the numbers from the file data7. i dont have the menu here so its just an example. now first i gotta initialize the array to 0 which is not that hard. now the thing is lets say i dont load the file and i press show, it will show the array which will be equal to 0. now i wnat it to do such as that when i press load it will load the array and when i press show it will show it. the probelm is that i cant do it right. i have another ifstream in the output function to output the array when its loaded but then it will always show that array because the load happens inside of the output function. how to make it work? like how to make it such that when i press 1 it loads and if i press 2 before 1 it will output the array that was initialied and when i press 1 and then 2 it will output the numbers from the loaded file. |
Author: | wtd [ Thu Nov 04, 2004 8:23 pm ] | ||||||||||||
Post subject: | |||||||||||||
First off, let's refomat that so it's easier to read.
I've also taken the liberty of changing things to make them standards compliant, and made the variable names a bit more descriptive. Now, your primary problem is:
You're creating the function locally, then expecting to be able to use it elsewhere. You can't. Instead, you should pass the array into the function. Note that you'll also have to pass in the length of the array.
Of course, we could go one step further. Instead of hard-coding the name of the file...
Calling it can then be:
And if you want something that maybe looks a bit prettier...
|