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

You could try something like this:

void check_string(const std::string str) { std::cout << "Got->" << str << std::endl; }

void print_actors_movies(treePtr root, const std::string name)
{
  if(root == NULL) return;

  if (root->left) print_actors_movies(root->left, name);

  std::for_each(root->actors.begin(), root->actors.end(), check_string);

  if (root->right) print_actors_movies(root->right, name);
}

I'll leave it to you to check if the name matches.

Hey thanks for replying. So I tried your code, and I just wanted to see if you could help me out a bit further. My program liked the functions alright, and I called them like this in main()"

case 2:
            {
                string actor;
                cout << "Enter the name of the actor: " << endl;
                cin >> actor;
                getline(cin, actor);
                print_actors_movies(root, actor);
                break;
            }

Firstly, did I call that like I should have?

Secondly, here is the output that the program gives me after the program asks the user to enter a name and I enter Clint Eastwood:

Got->Dan Ferro
Got->John Marshall Jones
Got->Brian Leckner
Got->John Ortiz
Got->Pamela Adlon
Got->Mitchell Whitfield
Got->Austin Pendleton
Got->Chris Rock
Got->Jack Nicholson
Got->Glenn Close
Got->Annette Bening
Got->Pierce Brosnan
Got->Danny DeVito
Got->Martin Short
Got->Sarah Jessica Parker
Got->Michael J. Fox
Got->Rod Steiger
Got->Tom Jones
Got->Jim Brown
Got->Lukas Haas
Got->Natalie Portman
Got->Pam Grier
Got->Lisa Marie
Got->Rutger Hauer
Got->Mark Dacascos
Got->Yvonne Sciƾ
Got->Patrick Dreikauss
Got->Randall William Cook
Got->Michael Mehlmann
Got->Ildikà Szƺcs
Got->IstvÆ°n Kanizsay
Got->Gabor Peter Vincze
Got->Attila Arpa
Got->Jak Osmond
Got->Bruce Willis
Got->Gary Oldman
Got->Ian Holm
Got->Milla Jovovich
Got->Chris Tucker
Got->Luke Perry
Got->Brion James
Got->Tom 'Tiny' Lister Jr.
Got->Lee Evans
Got->Charlie Creed-Miles
Got->John Neville
Got->Mike Myers
Got->Elizabeth Hurley
Got->Michael York
Got->Mimi Rogers
Got->Robert Wagner
Got->Seth Green
Got->Fabiana Udenio
Got->Mindy Sterling
Got->Paul Dillon
Got->Charles Napier
Got->Will Ferrell
Got->Joann Richter
Got->Anastasia Sakelaris
Got->Afifi Alaouie
Got->Matt Damon
Got->Robin Williams
Got->Ben Affleck
Got->Stellan Skarsgard
Got->Minnie Driver
Got->Casey Affleck
Got->Cole Hauser
Got->Mel Gibson
Got->Danny Glover
Got->Joe Pesci
Got->Rene Russo
Got->Chris Rock
Got->Jet Li
Got->Adam Sandler
Got->Drew Barrymore
Got->Christine Taylor
Got->Allen Covert
Got->Matthew Glave
Got->Ellen Albertini Dow
Got->Angela Featherstone
Got->Alexis Arquette
Got->Christina Pickles
Got->Jodi Thelen
Got->Frank Sivero
Got->Patrick McTavish
Got->Gemini Barnett
Got->Teddy Castellucci
Got->Randy Razz
Got->Billy Idol
Got->Ron Livingston
Got->Jennifer Aniston
Got->Ajay Naidu
Got->David Herman
Got->Gary Cole
Got->Stephen Root
Got->Richard Riehle
Got->Alexandra Wentworth
Got->Joe Bays
Got->John C. McGinley
Got->Diedrich Bader
Got->Paul Willson
Got->Kinna McInroe
Got->Russell Crowe
Got->Joaquin Phoenix
Got->Connie Nielsen
Got->Oliver Reed
Got->Richard Harris
Got->Clint Eastwood
Got->Tommy Lee Jones
Got->Donald Sutherland
Got->James Garner
Got->James Cromwell
Got->Marcia Gay Harden
Got->William Devane
Got->Loren Dean
Got->Courtney B. Vance
Got->Rade Serbedzija
Got->Barbara Babcock
Got->Blair Brown
Got->Jay Leno
Got->Russell Crowe
Got->Ed Harris
Got->Jennifer Connelly
Got->Christopher Plummer
Got->Paul Bettany
Got->Adam Goldberg
Got->Josh Lucas
Got->Anthony Rapp
Got->Jason Gray-Stanford
Got->Judd Hirsch
Got->Corbin Bernsen
Got->Tracy Nelson
Got->Whip Hubley
Got->Katie Stuart
Got->Lukas Behnken
Got->Michael Gregory
Got->Mark L. Taylor
Got->Scott Davidson
Got->Tony Longo
Got->Nicole Clendenen
Got->Corina Marie
Got->Jason Statham
Got->Qi Shu
Got->Matt Schulze
Got->Francois Berleand
Got->Ric Young
Got->Matt Damon
Got->Franka Potente
Got->Chris Cooper
Got->Clive Owen
Got->Brian Cox
Got->Adewale Akinnuoye-Agbaje
Got->Gabriel Mann
Got->Walton Goggins
Got->Josh Hamilton
Got->Julia Stiles
Got->Paul Walker
Got->Tyrese Gibson
Got->Eva Mendes
Got->Cole Hauser
Got->Ludacris
Got->Mark Wahlberg
Got->Charlize Theron
Got->Donald Sutherland
Got->Jason Statham
Got->Seth Green
Got->Mos Def
Got->Edward Norton
Got->Arnold Schwarzenegger
Got->Nick Stahl
Got->Claire Danes
Got->Kristanna Loken
Got->David Andrews
Got->Mark Famiglietti
Got->Earl Boen
Got->Moira Harris
Got->Chopper Bernet
Got->Christopher Lawford
Got->Rochelle Ballard
Got->Shawn Barron
Got->Layne Beachley
Got->Jesse Brad Billauer
Got->Taj Burrow
Got->Ken Collins
Got->Ami DiCamillo
Got->Darrick Doerner
Got->Brad Gerlach
Got->Laird John Hamilton
Got->Dave Kalama
Got->Keala Kennelly
Got->Alex Knost
Got->Jim Knost
Got->Gerry Lopez
Got->Rob Machado
Got->Chris Malloy
Got->Dan Malloy
Got->Keith Malloy
Got->Peter Mel
Got->Mike Parsons
Got->Kelly Slater
Got->Matt Damon
Got->Franka Potente
Got->Brian Cox
Got->Julia Stiles
Got->Karl Urban
Got->Gabriel Mann
Got->Joan Allen
Got->
Got->The Bourne Ultimatum (2007)
Got->Matt Damon
Got->Julia Stiles
Got->David Strathairn
Got->Scott Glenn
Got->Paddy Considine
Got->Edgar Ramirez
Got->Albert Finney
Got->Joan Allen
Got->Jason Statham
Got->Alessandro Gassman
Got->Amber Valletta
Got->Kate Nauta
Got->Matthew Modine
Got->Shannon Briggs
Got->Francois Berleand
Got->Jason Statham
Got->Amy Smart
Got->Jose Pablo Cantillo
Got->Efren Ramirez
Got->Dwight Yoakam
Got->Jack Nicholson
Got->Morgan Freeman
Got->Sean Hayes
Got->Beverly Todd
Got->Rob Morrow
Got->Clint Eastwood
Got->Christopher Carley
Got->Bee Vang
Got->Ahney Her
Got->Brian Haley
Got->Geraldine Hughes
Got->Dreama Walker
Got->Brian Howe
Got->John Carroll Lynch
Got->William Hill
Got->Brooke Chia Thao
Got->Chee Thao
Got->Choua Kue
Got->Scott Eastwood
Got->Jason Statham
Got->Natalya Rudakova
Got->Francois Berleand
Got->Robert Knepper
Got->Jeroen Krabbe
Got->Morgan Freeman
Got->Matt Damon
Got->Tony Kgoroge
Got->Patrick Mofokeng
Got->Matt Stern
Got->Julian Lewis Jones

Is this the kind of output you meant for the function to put out, or am I doing something wrong?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.