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
#include<iostream>
#include<string>
#include<fstream>
usingnamespace std;
constint 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;
}
elseif(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;
}
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 ==========
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.
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.
#include<iostream>
#include<string>
#include<fstream>
usingnamespace std;
constint 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;
}
elseif(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;
}*/