using struct array as parameter error

Been trying to figure out why I cannot pass the structure array as a parameter in the function call within main. Error says movieR is undeclared, but it is declared and even used to store info from file. Attempt to find a quotient using struct information and storing it away from stuct. These are then compared to other 103 members of array and returns which value is the lowest. Been stuck and probably starring at this too much, but I cant see where to start fixing this

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

const int movieRecords = 103;


struct Movie{
	std::string name;
	double budget;
	double domestic_gross;
	double global_gross;
};



int cost_effectiveDomestic(Movie movieR[]){  // function for finding cost effectiveness for domestic
	int y,tempx = 0;
	double temp = 1000;//high value expected to be larger than any value by default
	double cost_effective[102];

	while(y < (movieRecords-1)){ // populates array with cost effective result

		cost_effective[y] = (movieR->domestic_gross/movieR->budget);
		y++;

	}
	for(int k = 0; k < 102; k++)
	{
	if(cost_effective[k]< temp)
		temp = cost_effective[k];
		tempx = k;
		}

	return tempx;
}

int cost_effectiveGlobal(Movie movieR[]){  // function for finding cost effectiveness for domestic
	int y,tempx = 0;
	double temp = 1000;//high value expected to be larger than any value by default
	double cost_effective2[102];

	while(y < (movieRecords-1)){ // populates array with cost effective result

		cost_effective2[y] = (movieR->global_gross/movieR->budget);
		y++;

	}
	for(int k = 0; k < 102; k++)
	{
	if(cost_effective2[k]< temp)
		temp = cost_effective2[k];
		tempx = k;
		}

	return tempx;
}

void display_costEffective(Movie ,int );


int main (){

	int cost_effectiveResult = 0;
	int keyEnter = 0; // error until entered a value

	ifstream dataFile;

	dataFile.open("movie_data.txt", ios::in); // open file for input and check for error

	if(dataFile.fail()){
	cout<< " Error: Cannot open the movie_data file..." <<endl;
	}
	else{
		Movie movieR[movieRecords]; //initialize array of 103 structure types movie for MovieR

		for(int x = 0; x < movieRecords-1; x++) {//populate array from list
		dataFile>>movieR[x].name>>movieR[x].budget>>movieR[x].domestic_gross>>movieR[x].global_gross;
		}

	}

	cout<< " Please enter 1 for lowest domestic gross or 2 for global gross : " << endl;
	cin >> keyEnter;

	if(keyEnter == 1){ // displays infromation for domestic or gross based on eR

		cost_effectiveResult=cost_effectiveDomestic(movieR);

		display_costEffective(movieR, cost_effectiveResult);
		cout<<" effective cost of :" <<endl; 
		return movieR[cost_effectiveResult].domestic_gross/movieR[cost_effectiveResult].budget;
	}
	else if(keyEnter ==2)
	{

		cost_effectiveResult=cost_effectiveGlobal(movieR);

		display_costEffective(movieR, cost_effectiveResult);
		cout<<" effective cost of :" <<endl; 
		return movieR[cost_effectiveResult].global_gross/movieR[cost_effectiveResult].budget;
	}




	system("pause");
	return 0;

}

void display_costEffective(Movie movieR[],int cost_effectiveResult){
	cout<< " The cost effective result from selected chose is: " << endl;
	cout<<"name :"<<movieR[cost_effectiveResult].name<< endl;
	cout<<"budget :"<<movieR[cost_effectiveResult].budget<<endl;
	cout<<"domestic gross:"<<movieR[cost_effectiveResult].domestic_gross<<endl;
	cout<<"global gross :"<<movieR[cost_effectiveResult].global_gross<<endl;
	

}
Last edited on
this is the error list I am getting, most or all involve movieR, not sure if my declaration is wrong. Would creating the array instead of heap be better? Feel kinda lost
1>c:\users\thepl_000\documents\visual studio 2010\projects\projec2\projec2\moviedataanalysis.cpp(95): error C2065: 'movieR' : undeclared identifier
1>c:\users\thepl_000\documents\visual studio 2010\projects\projec2\projec2\moviedataanalysis.cpp(97): error C2065: 'movieR' : undeclared identifier
1>c:\users\thepl_000\documents\visual studio 2010\projects\projec2\projec2\moviedataanalysis.cpp(99): error C2065: 'movieR' : undeclared identifier
1>c:\users\thepl_000\documents\visual studio 2010\projects\projec2\projec2\moviedataanalysis.cpp(99): error C2228: left of '.domestic_gross' must have class/struct/union
1>c:\users\thepl_000\documents\visual studio 2010\projects\projec2\projec2\moviedataanalysis.cpp(99): error C2065: 'movieR' : undeclared identifier
1>c:\users\thepl_000\documents\visual studio 2010\projects\projec2\projec2\moviedataanalysis.cpp(99): error C2228: left of '.budget' must have class/struct/union
1>c:\users\thepl_000\documents\visual studio 2010\projects\projec2\projec2\moviedataanalysis.cpp(104): error C2065: 'movieR' : undeclared identifier
1>c:\users\thepl_000\documents\visual studio 2010\projects\projec2\projec2\moviedataanalysis.cpp(106): error C2065: 'movieR' : undeclared identifier
1>c:\users\thepl_000\documents\visual studio 2010\projects\projec2\projec2\moviedataanalysis.cpp(108): error C2065: 'movieR' : undeclared identifier
1>c:\users\thepl_000\documents\visual studio 2010\projects\projec2\projec2\moviedataanalysis.cpp(108): error C2228: left of '.global_gross' must have class/struct/union
1>c:\users\thepl_000\documents\visual studio 2010\projects\projec2\projec2\moviedataanalysis.cpp(108): error C2065: 'movieR' : undeclared identifier
1>c:\users\thepl_000\documents\visual studio 2010\projects\projec2\projec2\moviedataanalysis.cpp(108): error C2228: left of '.budget' must have class/struct/union
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Hi,

MovieR is declared on line 78, but it goes out of scope because it is inside an else compound statement. In C type languages any brace {} introduces a new scope.

Also, on lines 95 and 104 you return a value to the OS as the program ends with the return statement from main. I am pretty sure that's not what you want, as the OS will only accept an int (int main())- you are returning a double.

Hope all goes well :+)

Edit:

It would be good in future if the line numbers in the code you posted matched those in the compiler errors.
Last edited on
Thank you so much, ended up fixing the issues and stopped using a void function to display the results as I was getting an error of converting Movie[103] type into Movie on lines 93 and 102.

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

const int movieRecords = 103;


struct Movie{
	std::string name;
	double budget;
	double domestic_gross;
	double global_gross;
};



int cost_effectiveDomestic(Movie movieR[]){  // function for finding cost effectiveness for domestic
	int y = 0,tempx = 0;
	double temp = 1000;//high value expected to be larger than any value by default
	double cost_effective[102];

	while(y < (movieRecords-1)){ // populates array with cost effective result

		cost_effective[y] = (movieR->domestic_gross/movieR->budget);
		y++;

	}
	for(int k = 0; k < 102; k++)
	{
	if(cost_effective[k]< temp)
		temp = cost_effective[k];
		tempx = k;
		}

	return tempx;
}

int cost_effectiveGlobal(Movie movieR[]){  // function for finding cost effectiveness for domestic
	int y = 0,tempx = 0;
	double temp = 1000;//high value expected to be larger than any value by default
	double cost_effective2[102];

	while(y < (movieRecords-1)){ // populates array with cost effective result

		cost_effective2[y] = (movieR->global_gross/movieR->budget);
		y++;

	}
	for(int k = 0; k < 102; k++)
	{
	if(cost_effective2[k]< temp)
		temp = cost_effective2[k];
		tempx = k;
		}

	return tempx;
}

/* trash would not work due to Movie[103] converting to Movie type not possible::void display_costEffective(Movie ,int );*/


int main (){

	int y,cost_effectiveResult = 0;
	int keyEnter = 0; // error until entered a value
	double result = 0.0;
	ifstream dataFile;

	dataFile.open("movie_data.txt", ios::in); // open file for input and check for error
	Movie movieR[movieRecords]; //initialize array of 103 structure types movie for MovieR

	if(dataFile.fail()){
	cout<< " Error: Cannot open the movie_data file..." <<endl;
	}
	else{
		
		for(int x = 0; x < movieRecords-1; x++) {//populate array from list
		dataFile>>movieR[x].name>>movieR[x].budget>>movieR[x].domestic_gross>>movieR[x].global_gross;
		}

	}

	cout<< " Please enter 1 for lowest domestic gross or 2 for global gross : " << endl;
	cin >> keyEnter;

	if(keyEnter == 1){ // displays infromation for domestic or gross based on eR

		cost_effectiveResult=cost_effectiveDomestic(movieR);

		cout<< " The cost effective result from selected chose is: " << endl;
		cout<<"name :"<<movieR[cost_effectiveResult].name<< endl;
		cout<<"budget :"<<movieR[cost_effectiveResult].budget<<endl;
		cout<<"domestic gross:"<<movieR[cost_effectiveResult].domestic_gross<<endl;
		cout<<"global gross :"<<movieR[cost_effectiveResult].global_gross<<endl;

		cout<<" effective cost of :" <<endl; 
		result=movieR[cost_effectiveResult].domestic_gross/movieR[cost_effectiveResult].budget;
		cout<<result;
	}
	else if(keyEnter ==2)
	{

		cost_effectiveResult=cost_effectiveGlobal(movieR);
		
		cout<< " The cost effective result from selected chose is: " << endl;
		cout<<"name :"<<movieR[cost_effectiveResult].name<< endl;
		cout<<"budget :"<<movieR[cost_effectiveResult].budget<<endl;
		cout<<"domestic gross:"<<movieR[cost_effectiveResult].domestic_gross<<endl;
		cout<<"global gross :"<<movieR[cost_effectiveResult].global_gross<<endl;
		

		cout<<" effective cost of :" <<endl; 
		result=movieR[cost_effectiveResult].global_gross/movieR[cost_effectiveResult].budget;
		cout<<result;
	}




	system("pause");
	return 0;

}

/*function would not work properly ::void display_costEffective(Movie movieR[],int cost_effectiveResult){
	cout<< " The cost effective result from selected chose is: " << endl;
	cout<<"name :"<<movieR[cost_effectiveResult].name<< endl;
	cout<<"budget :"<<movieR[cost_effectiveResult].budget<<endl;
	cout<<"domestic gross:"<<movieR[cost_effectiveResult].domestic_gross<<endl;
	cout<<"global gross :"<<movieR[cost_effectiveResult].global_gross<<endl;
	

}*/ 
Hi,

Can you please post your compile errors exactly as they appear?

I probably won't have time tonight to reply, but there are plenty of others, way more knowledgeable than me, that can.

Hope all goes well :+)


PS

Have you investigated using the debugger in your system? This is the quickest way to fix problems - as long as the code compiles.
Topic archived. No new replies allowed.