Write your question here.
I must be doing something wrong here, i am attempting to take a structure pass it to a function that reads some values from a file to populate an array of the structure then pass the populated array to a function for printing...
So far it works without the third function thanks to help from folks here. however the print function doesn't... any help at all is appreciated!
void printInfo(Weather* data, int size) {
for (int i = 0; i < size; ++i) {
// consider using std::setw() here instead of padding manually
std::cout << data[i].Month << " "
<< data[i].Aht << " "
<< data[i].Alt << " "
<< data[i].Mprec << '\n';
}
}
Note on array parameters:
Given this line, Weather* printInfo(Weather *data[12])
Array parameters immediately decay to pointers -- therefore the above line is exactly equivalent to Weather* printInfo(Weather **data)
This is highly unintuitive, so I suggest NEVER using the array parameter syntax again. To pass an array of int, do this:
1 2
void foo(int array[12])void foo(int *array, int size)
As a side effect of the above "array decay", size information is lost. Convention suggests adding a parameter to track the size of the array. You should loop over that size, not over the magic number 11. See the above. Don't forget to change the function declaration to match on line 23.
Thanks for the help, I really hope I can catch onto this.
I am still getting nothing from the printInfo function, i just get my column tops.
My data is like this,
Month High Low Precipitation
Jan 33 18 0.87
Feb 39 21 0.71
Mar 50 28 0.98
Apr 58 33 1.22
May 67 40 2.01
Jun 75 47 2.09
Jul 86 51 0.98
Aug 85 50 1.18
Sep 73 42 1.18
Oct 58 32 0.87
Nov 42 25 1.02
Dec 31 17 1.02
Every time I try to make it work I just get more and more confused...
All i get to print is
You were close to getting it to work but missing some fundamentals. It's up to you whether you just copy this or study it and learn where the fixups were needed.
12
Jan 33 18 0.87
Feb 39 21 0.71
Mar 50 28 0.98
Apr 58 33 1.22
May 67 40 2.01
Jun 75 47 2.09
Jul 86 51 0.98
Aug 85 50 1.18
Sep 73 42 1.18
Oct 58 32 0.87
Nov 42 25 1.02
Dec 31 17 1.02
Program ended with exit code: 0
That's amazing, i can almost see how it works....I will try to use this to patch up my own work.
SO much to know and i am confused by many of these things.
Thanks so much for the time. :)