Struct arrays and functions to print them

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!

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

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



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

// Function prototypes
Weather* getInfo();
Weather* printInfo();


Weather* getInfo()
{
std::cout << "Press ENTER to display file...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
   Weather W;  // Temporary structure variable
   Weather* data = new Weather[12];
   size_t i{};
   int four;
std::ifstream inputFile("C:\\weather.txt"); 
if (inputFile) {
		
		// loops to run and check 12 times.
		for(int i=0;i<11; ++i){
		while ( i < 11 && inputFile) {
    
	  
	  inputFile >> W.Month;
	  inputFile >> W.Aht;
	  inputFile >> W.Alt;
	  inputFile >> W.Mprec;
	  data[i] = W;
	    	if (inputFile.eof()){
	
}
		}

    }


   // Return the temporary variable.

   return data;
}}
	Weather* printInfo(Weather *data[12])
	{
	
		for(int x=0;x<11; ++x){
		
      cout << data[x]->Month << "      ";
	  cout << data[x]->Aht << "      ";
	  cout << data[x]->Alt << "     ";
	  cout << data[x]->Mprec << '\n';
		}
	  }
int main(){
std::ifstream inputFile("C:\\weather.txt"); 
if (inputFile) {
	
		
   Weather* c = getInfo();
   delete [] c;
		string catagory;

		inputFile >> catagory;
		cout << catagory <<"    ";
		inputFile >> catagory;
		cout << catagory <<"    ";
		inputFile >> catagory;
		cout << catagory <<"    ";
		inputFile >> catagory;
		cout << catagory <<endl;
   	if (inputFile.eof()){

}}
   return 0;
}
This will compile, at least:
1
2
3
4
5
6
7
8
9
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.
Last edited on
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

Month High Low Precipitation
Last edited on
closed account (48T7M4Gy)
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.

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
66
67
68
69
70
71
#include <iostream>
#include <fstream>

const int NO_MONTHS = 12; // JUST TO MAKE LIFE EASY

using namespace 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
Last edited on
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. :)
Topic archived. No new replies allowed.