I am currently working on a project for class that reads in from a .txt file the names of a film, the year it was made, and the list of actors in said film, and sorts the info in a binary search tree. In the tree nodes, I have the actors organized in a singly linked list. So far my program can read everything in from the file, store it into the tree, it can print out all the titles of the movies and only the titles, and it can ask a user to input a year and it will print out all the movies released before that year from the list.
What I'm currently trying to do is make it so the user can enter the name of an actor, and the program will go through the linked list in each node, and if the name is found, it will print out the title of that movie. I've never used the Standard C++ Library before because my instructors chose not to teach it.
Could anyone give me some ideas as to how I can accomplish this? Any help would be appreciated.
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
typedef struct treeNode{
int year;
string title;
list<string> actors;
treeNode* left;
treeNode* right;
}* treePtr;
treePtr root = NULL;
treePtr fillTree(treePtr p, int y, string t, list<string> a);
void print_titles(treePtr root);
void print_before_year(treePtr root, int key);
void print_actors_movies();
int main(){
ifstream inFile ("movies.txt");
string x;
treePtr root = NULL;
int count = 0;
if(inFile.is_open()){
while(getline (inFile, x)){
if(x == ""){
continue;
}
count++;
int index = x.find_last_of(" ");
string title = x.substr(0, index);
string year = x.substr(index + 2, x.length() - index - 3);
list<string> actor;
int counter = 0;
while(getline(inFile, x)){
if(x == ""){
break;
}
else{
actor.push_back(x);
}
}
root = fillTree(root, atoi(year.c_str()), title, actor);
}
}
int choice;
do{
cout <<"\nWelcome to the Movie Store. Would you like to: \n(1) See what movies are available? \n(2) Search for an actor? \n(3) Search for a year? \n(4) Search for a movie? \n(0) Exit the Store" << endl;
cin >> choice;
switch(choice){
case 0:
cout << "Thank you come again." << endl;
break;
case 1:
print_titles(root);
break;
case 2:
case 3:
int year;
cout << "Please enter the year you wish to search for: " << endl;
cin >> year;
cout << endl;
cout << "Films made before " << year << ":" << endl;
print_before_year(root, year);
break;
case 4:
default:
cout << "Try again." << endl;
}}while(choice != 0);
return 0;
}
treePtr fillTree(treePtr p, int y, string t, list<string> a){
treePtr n = new treeNode;
n->year = y;
n->title = t;
n->actors = a;
n->left = NULL;
n->right = NULL;
if(p == NULL){
p = n;
}
else{
treePtr prev = p;
treePtr curr = p;
while(curr != NULL){
if(curr->year > y){
prev = curr;
curr = curr->left;
}
else{
prev = curr;
curr = curr->right;
}
}
if(prev->year > y){
prev->left = n;
}
else{
prev->right = n;
}
}
return p;
}
void print_titles(treePtr root){
if(root == NULL) return;
if(root->left) print_titles(root->left);
cout<<root->title<<endl;
if(root->right) print_titles(root->right);
}
void print_before_year(treePtr root, int key){
if(root == NULL) return;
if(root->left) print_before_year(root->left, key);
if(root->year < key){
cout << root->title << endl;
}
else return;
if(root->right) print_before_year(root->right, key);
}
void print_actors_movies(){
}
And here is the .txt file just in case: http://www2.cs.uidaho.edu/~bruceb/cs121/Assignments/movies.txt