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.
#include <iostream>
#include <fstream>
constint NO_MONTHS = 12; // JUST TO MAKE LIFE EASY
usingnamespace std;
struct Weather
{
char Month[3];
int Aht; // AVOID CRAP VARIABLE NAMES
int Alt;
float Mprec;
};
int getInfo(std::string, Weather *);
void printInfo(Weather *);
int getInfo(std::string file_name, Weather *aWeatherArray){
int counter = 0;
std::ifstream inputFile(file_name);
if (inputFile.is_open())
{
std::string dummy;
getline(inputFile, dummy); // I.E. IGNORE HEADERS
char date[] = "???"; // REALLY SHOULD USE <string>'s
int high = 0;
int low = 0;
float rainfall = 0; // USE DOUBLE's - FORGET FLOAT's
while ( inputFile >> date >> high >> low >> rainfall and counter < NO_MONTHS ){
strcpy(aWeatherArray[counter].Month, date);
aWeatherArray[counter].Aht = high;
aWeatherArray[counter].Alt = low;
aWeatherArray[counter].Mprec = rainfall;
counter++;
}
}
else
std::cout << "Unable to open file\n";
return counter;
}
void printInfo(Weather *aWeatherArray, int record_count){
for(int x = 0;x < record_count; ++x){
cout << aWeatherArray[x].Month << " ";
cout << aWeatherArray[x].Aht << " ";
cout << aWeatherArray[x].Alt << " ";
cout << aWeatherArray[x].Mprec << '\n';
}
}
int main(){
Weather* c = new Weather[NO_MONTHS];
int no_of_records = getInfo("weather.txt", c);
std::cout << no_of_records << '\n';
printInfo(c, no_of_records);
return 0;
}
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. :)