How to call a struct function in main(if possible)

So ive been stuck on my project for quite some time now. My professor said that everything needs to be initialized in the structure, but i did it all in the main and linked it to the structure... now im stuck, is it possible to call a structure function in the main? this is all i have so far.

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  #include<iostream>
#include<cmath>
#include<string>
#include<istream>

using namespace std;

struct Cargo
{
	string label;
	double weight;
	int height, width, length;
	int cargo[100];
	void initCargo(Cargo cargo[], int height, int width, int length,
	double weight, string label)
	{
		
	}
	double cargoWeight(Cargo cargo)
	{
	
	}
	int cargoVolume(Cargo cargo)
	{

	}
	void printCargo(Cargo cargo)
	{
		
	}
};
void loadPlane(Cargo allCargo[],int size, double maxWeight, double usedWeight, int
maxVolume, int usedVolume );
void printWeight( double maxWeight, double usedWeight);
void printVolume( int maxVolume, int usedVolume);
void printHeaviest(Cargo allCargo);
void printLargest(Cargo allCargo);
void printCargo(Cargo allCargo);

void main()
{
	Cargo allCargo[100];
	double maxWeight=50000;
	int usedWeight=0;
	int maxVolume=3000000;
	int usedVolume=0;
	int size;
	for(int i=0;i<100;i++)
	{
	cout<<"Input label: ";
	cin>>allCargo[i].label;
	if(allCargo[i].label=="quit")
		break;
	cout<<"Input weight: ";
	cin>>allCargo[i].weight;
	cout<<"Input height: ";
	cin>>allCargo[i].height;
	cout<<"Input width: ";
	cin>>allCargo[i].width;
	cout<<"Input length: ";
	cin>>allCargo[i].length;
	cout<<endl;
	size=i+1;
	usedWeight=usedWeight+allCargo[i].weight;

	}
	cout<<endl;
	loadPlane(allCargo,size,maxWeight,usedWeight,maxVolume,usedVolume);
	printWeight(maxWeight,usedWeight);
	printVolume(maxVolume,usedVolume);
	printHeaviest(allCargo);
	printLargest(allCargo);
	printCargo(allCargo);
	
	
	
}

void loadPlane(Cargo allCargo[],int size, double maxWeight, double usedWeight, int
maxVolume, int usedVolume)
{
	
}
void printWeight( double maxWeight, double usedWeight)
{
	double unusedWeight;
	cout<<"Maximum Cargo Weight: "<<maxWeight<<endl;
	unusedWeight=maxWeight-usedWeight;
	cout<<"Unused Cargo Weight: "<<unusedWeight<<endl;
}
void printVolume( int maxVolume, int usedVolume)
{
	
}
void printHeaviest(Cargo allCargo[])
{
	
}
void printLargest(Cargo allCargo[])
{

}
void printCargo(Cargo allCargo[])
{

} 
is it possible to call a structure function in the main?

Absolutely.

Lines 19,23,27: You don't want to pass a Cargo instance as a parameter. With a struct/class function call, you have implicit access to the member variables of the instance.

Assuming cargoWeight is meant to be a getter, then you want something like this:
1
2
3
double cargoWeight () const
{  return weight;  // refers to member variable
}


Likewise, for line 14, you don't want to pass the array to initCargo. initCargo should initialize one instance.

line 13: What's the point of this array?

Line 32: Not clear if some of these parameters should be passed by reference. Do you intend to modify them>
Last edited on
with lines 19,23,and 27 my assignment states that those functions needs to be in the structure.
Include the functions below into the Cargo structure.
1. void initCargo( Cargo cargo, int height, int width, int length,
double weight, string label) -- initializes the data members of the cargo
struct with the height, width, length, weight and label parameters.
o cargo -- a Cargo struct to be initialized
o height, width, length, weight, label -- the data values
2. double cargoWeight( Cargo cargo ) -- returns the weight of the cargo
container
3. int cargoVolume( Cargo cargo ) -- returns the volume of the cargo container
4. void printCargo( Cargo cargo ) -- prints the data of the cargo container in a
format suitable for the table of cargo data

with line 13 i figured that since everything needs to be initialized in the structure i was going to send the array to the structure which im also unclear about.
And i will modify the parameters soon.
If you need to see my whole assignment for better understanding, tell me and ill post it.
I honestly can do the assignment without the structure functions but my prof said it has to be in there.

Topic archived. No new replies allowed.