Trying to pass an array of structures

Write your question here.
I keep getting an error saying missing type specifier, int assumed.
and cannot convert argument 1 from Rainfall to const int. Can someone help?
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
  #include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void userInput(const Rainfall &k);


struct Rainfall
{
	std::string month;
	int amount;
};

int main(){

	
	Rainfall s;
	const int SIZE=12;
	userInput(s[SIZE]);


	return 0;
}

void userInput(const Rainfall &k){
	cout << "Enter the rainfall(in inches) for January:" << endl;
	cout << "Enter the rainfall(in inches) for February : " << endl;
	cout << "Enter the rainfall(in inches) for March : " << endl;
	cout << "Enter the rainfall(in inches) for April : " << endl;
	cout << "Enter the rainfall(in inches) for May : " << endl;
	cout << "Enter the rainfall(in inches) for June : " << endl;
	cout << "Enter the rainfall(in inches) for July : " << endl;
	cout << "Enter the rainfall(in inches) for August : " << endl;
	cout << "Enter the rainfall(in inches) for September : " << endl;
	cout << "Enter the rainfall(in inches) for October : " << endl;
	cout << "Enter the rainfall(in inches) for November : " << endl;
	cout << "Enter the rainfall(in inches) for December :" << endl;

	
}
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
  #include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int SIZE=12;

struct Rainfall{
	std::string month;
	int amount;
};

void userInput( Rainfall k[], int size);

int main(){


	Rainfall s[SIZE];

	userInput(s, SIZE);


	return 0;
}

void userInput(Rainfall k[], int size){
	cout << "Enter the rainfall(in inches) for January:" << endl;
	cout << "Enter the rainfall(in inches) for February : " << endl;
	cout << "Enter the rainfall(in inches) for March : " << endl;
	cout << "Enter the rainfall(in inches) for April : " << endl;
	cout << "Enter the rainfall(in inches) for May : " << endl;
	cout << "Enter the rainfall(in inches) for June : " << endl;
	cout << "Enter the rainfall(in inches) for July : " << endl;
	cout << "Enter the rainfall(in inches) for August : " << endl;
	cout << "Enter the rainfall(in inches) for September : " << endl;
	cout << "Enter the rainfall(in inches) for October : " << endl;
	cout << "Enter the rainfall(in inches) for November : " << endl;
	cout << "Enter the rainfall(in inches) for December :" << endl;


}

Here is working version. Compiler reads line by line. struct was under function declaration so compiler didnt know it existed. If you want to create array you have to point it out at declaration no when you pass it as argument. and when you add const in front of the thing that means you can't change info in it.
Last edited on
Line 7: The compiler has not seen the declaration for Rainfall yet, so it does not know what it is. Move lines 10-12 in front of line 7.

Line 21: s was not declared as an array.

1
2
3
4
    const int SIZE=12;
    Rainfall s[SIZE];

    userInput(s);


Line 27: This takes a single instance of Rainfall, not an array.



oh wow thanks!

if i replaced void userInput(const Rainfall &k) and replace it
void userInput(Rainfall k[])

would that grab the arrays?
Why would you want to do that? When you pass array you already give refference to it.
Topic archived. No new replies allowed.