how to get multiple results in a linear search

hi, my c++ prof gave an exercise to make a movie database of 10 movies including movie title, year, director and genre, now I manage to do all that but im having trouble getting multiple results from a linear search that searches on the topic the user desires,I manage to get 1 result but I need all the result in said topic like if 2 movies have the same director I need to return all the info on both movies but I only manage to get 1 any help would be appreciated.(sorry for my english) This is where Im at right now:


#include <iostream>
#include <iomanip>
#include <cctype>

using namespace std;
const int size2=80;

int movieSearch( char movie[][size2], char key[], int size );
bool isEqualNoCase(char str1[],char str2[]);
int yearSearch( int movie[], int key, int size);

int main(){
const int size1=10;
int x=0,option,year2;
char movie[size2],director2[size2],genre2[size2];

char title[size1][size2]={"Robin Hood","Terra Nova", "The Matrix","Star Trek", "Taken", "Blade","Super 8", "Superman: Returns","Gotham Knight", "The Lord of the Rings"};

int year[size1]= {2010,2011,1999,2009,2008,1998,2011,2006,2008,2001};

char director[size1][size2]={"Ridley Scott","Steven Spielberg","Andy Wachowski","JJ Abrams","Pierre Morel","Stephen Norrington","JJ Abrams","Bryan Singer","Yashuro Aoki","Peter Jackson"};

char genre[size1][size2]={"Action","Adventure","SciFi","SciFi","Thriller","Action","Drama","Fantasy","Animated","Adventure"};

cout<<"Search Option\n";
cout<<"-------------\n\n";
cout<<"1.Title"<<endl;
cout<<"2.Year"<<endl;
cout<<"3.Director"<<endl;
cout<<"4.Genre\n"<<endl;
cout<<"Select an Option: ";
cin>>option;
cin.ignore(256, '\n');
cout<<endl;

if (option==1){

cout<<"Enter a title: ";
cin.getline(movie,size2-1);
cout<<endl;

x=movieSearch (title,movie,size1);

if (x>0){
cout<<"Results\n\n";
cout<<"Title: "<<title[x]<<endl;
cout<<"Year: "<<year[x]<<endl;
cout<<"Director:"<<director[x]<<endl;
cout<<"Genre: "<<genre[x]<<endl;
}
else
cout<<"Movie is not in the database\n";
}
else if (option==2){

cout<<"Enter a year: ";
cin>>year2;
cout<<endl;

x=yearSearch (year,year2,size1);

if (x>0){
cout<<"Results\n\n";
cout<<"Title: "<<title[x]<<endl;
cout<<"Year: "<<year[x]<<endl;
cout<<"Director:"<<director[x]<<endl;
cout<<"Genre: "<<genre[x]<<endl;
}
else
cout<<"Movie is not in the database\n";
}

else if (option==3){

cout<<"Enter enter the director's full name: ";
cin.getline(director2,size2-1);
cout<<endl;

x=movieSearch (director,director2,size1);

if (x>0){
cout<<"Results\n\n";
cout<<"Title: "<<title[x]<<endl;
cout<<"Year: "<<year[x]<<endl;
cout<<"Director:"<<director[x]<<endl;
cout<<"Genre: "<<genre[x]<<endl;
}
else
cout<<"Movie is not in the database\n";
}

else if (option==4){

cout<<"Enter a Genre: ";
cin.getline(genre2,size2-1);
cout<<endl;

x=movieSearch (genre,genre2,size1);

if (x>0){
cout<<"Results\n\n";
cout<<"Title: "<<title[x]<<endl;
cout<<"Year: "<<year[x]<<endl;
cout<<"Director:"<<director[x]<<endl;
cout<<"Genre: "<<genre[x]<<endl;
}
else
cout<<"Movie is not in the database\n";
}



return 0;

}


int movieSearch( char movie[][size2], char key[], int size )
{
for ( int j = 0; j < size; j++ )
if (isEqualNoCase(movie[j],key)) // Searches location
return j; // return location of key

return -1; // key not found
}



int yearSearch( int movie[], int key, int size)//searches year
{
for ( int j = 0; j < size; j++ )
if (movie[j]==key) // if found,
return j; // return location of key

return -1; // key not found
}



bool isEqualNoCase(char str1[],char str2[]){//compares 2 strings to check if they are exactly the same

int counter=0,counter2=0,k=0;

for(int i=0;str1[i]!='\0';i++)
counter=i;

for (int j=0;str2[j]!='\0';j++)
counter2=j;

for(int k=0;str1[k]!='\0'&&str2[k]!='\0';k++){
k=k;

if(toupper(str1[k])!= toupper(str2[k]))
return false;
if(k==counter && k==counter2)
return true;
}
}
Last edited on
hi Jampy151

first, i need to tell you something, but if you'd not like to read, well skip the next paragraph then.

we have code tags here for a reason. do learn to use it :) next thing is, don't just paste your whole code here and expect us to fix it for you. its part of the learning process to fix your own program bugs. tell us whats wrong, etc, we'll figure it together. besides that, what if your classmates are in this forum here as well? they can plagiarize your code then.
--

it will be quite simple to determine which movie corresponds with the director in such way :/ in logical terms, say... i'm looking for movies made by the director "abc"

1
2
3
4
5
6
7
8
//so we look into the directors list you have
for (i=0; i<last_limit_of_array; i++))
{
  //if director=="abc"
  if (director[i]=="abc")
    //display movie corresponding with director
    printf(title[i]);
}


1
2
3
4
5
6
7
char title[size1][size2]={"Robin Hood","Terra Nova", "The Matrix","Star Trek", "Taken", "Blade","Super 8", "Superman: Returns","Gotham Knight", "The Lord of the Rings"};

int year[size1]= {2010,2011,1999,2009,2008,1998,2011,2006,2008,2001};

char director[size1][size2]={"Ridley Scott","Steven Spielberg","Andy Wachowski","JJ Abrams","Pierre Morel","Stephen Norrington","JJ Abrams","Bryan Singer","Yashuro Aoki","Peter Jackson"};

char genre[size1][size2]={"Action","Adventure","SciFi","SciFi","Thriller","Action","Drama","Fantasy","Animated","Adventure"};
this is unfavorable... and this is also the cause of the problem. considering how everything is stored in arrays, you not only make your program bulky, but also inefficient. have you considered storing all the info in a text file?
sorry about about all pasting all that stuff I didnt know about the tag :(, im new in this forums next time I wont fail on that matter and thanks for the insight on the code Im gonna make it better now.
its okay ^_^ everyone's gotta start somewhere. i glad i did help you, and do enjoy your time here in the cplusplus forum.
Topic archived. No new replies allowed.