Structure arrays and functions

Write your question here.
Well I am trying to create a Structure array of [12] that is populated by the function below and prints out using another function in a readable way.
well I have gotten to the point where I am creating a single instance and I can't get any real data to print, I instead get...and that's just with the one function.

Mont
0
6356664
1.21985e+032
I have been stabbing at it for hours and hours...its due tomorrow at midnight so I thought I might reach out to the community, I try not to do this too often.
Yes it's Homework... this assignment though is making me insane. Thanks for any help, sorry the horrendous code.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;



// Structure declaration
struct Weather
  {
  char Month[3]; 
  int Aht; 
  int Alt;
  float Mprec;
  };

// Function prototype
Weather getInfo();

int main(){
	
   Weather c[12];      // structure variable

  
   c[12] = getInfo();
   
	
   
  
 

   return 0;
}


Weather getInfo()
{
   Weather W;  // Temporary structure variable
 ifstream inputFile;
inputFile.open("data.txt");

if (inputFile.is_open()) {

		// While the file is good
		while (inputFile.good() ) {
	  inputFile >> W.Month;
	  inputFile >> W.Aht;
	  inputFile >> W.Alt;
	  inputFile >> W.Mprec;
          cout << W.Month << '\n';
	  cout << W.Aht << '\n';
	  cout << W.Alt << '\n';
	  cout << W.Mprec << '\n';

    }
    inputFile.close();



}  
   // Return the temporary variable.
   return W;

}

The data.txt file is as follows
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
see this link re returning local array's from functions: http://stackoverflow.com/questions/7769998/how-to-return-local-array-in-c

and based on these principles, here's a working program though it can be optimized much further with vectors, operator << overload etc:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;



// Structure declaration
struct Weather
  {
  char Month[3];
  int Aht;
  int Alt;
  float Mprec;
  };

// Function prototype
Weather* getInfo();

int main(){

   Weather* c = getInfo();
   delete [] c;

   return 0;
}


Weather* getInfo()
{

   Weather W;  // Temporary structure variable
   Weather* data = new Weather[12];
   size_t i{};
 ifstream inputFile{"D:\\weather.txt"};

if (inputFile) {

		// While the file is good
		while ( i < 12 && inputFile) {
        //note the first line of the inputFile has been removed
	  inputFile >> W.Month;
	  inputFile >> W.Aht;
	  inputFile >> W.Alt;
	  inputFile >> W.Mprec;
	  data[i] = W;
	  ++i;

          cout << W.Month << '\n';
	  cout << W.Aht << '\n';
	  cout << W.Alt << '\n';
	  cout << W.Mprec << '\n';

    }
}
   // Return the temporary variable.
   return data;
}

Topic archived. No new replies allowed.