Colin Powers
Colin Powers

Reputation: 13

No Names returned in search function

So I cant get my search function to return to the actual title of the movies with the instantiated array with a movie title, director, and actor. I create an array of objects, instantiate it, run a do while loop to get the position, and run a search algorithm to get the title, director, and actor of the movie. But for the life of me I cant get the code to return the actual title, director or actor. It will say the name is in the list, but returns an empty space. I linked a picture below to show the return. When I add a movie to the array it says the movie does not exist in the array.

The movie Class:

class Movies{
private:

    // variables
    string titleCode;
    string directorCode;
    string actorCode;

public:

    // constructors
    Movies()        // default constructor, allows no arguments.
    {
        //titleCode = "Home Movie"; directorCode = "Colin Powers"; actorCode = "Colin Powers";
    }
    Movies(string t, string d, string a) // constructor
    {
        titleCode = t; directorCode = d; actorCode = a;
    }

    // getter
    string getTitle() const
    {
        string title = titleCode;
        return title;
    }
    string getDirector() const
    {
        string director = directorCode;
        return director;
    }
    string getActors() const
    {
        string actors = actorCode;
        return actors;
    }

    // setters
    void setTitle(string t) // cout/cin were giving random "ambigious" error so i added std:: till they all stopped giving it.
    {
        std::cout << "Enter the title of the Movie: " << endl;
        std::cin >> t;
        titleCode = t;
    }
    void setDirector(string d)
    {
        std::cout << "Enter the Director of the Movie: " << endl;
        std::cin >> d;
        directorCode = d;
    }
    void setActors(string a)
    {
        std::cout << "Enter the main protagonist: " << endl;
        std::cin >> a;
        actorCode = a;
    }};

Movies Array:

Movies moviesArr[ARR_SIZE];

Function prototype for the search function

int searchMovies(const Movies[], int, string);

Instantiated array of objects to search through

Movies hollywood[ARR_SIZE] =
{ // title, director, actor
    Movies("Avatar", "James", "James"),
    Movies("Terminator", "John", "John"),
    Movies("Predator", "Michael", "Michael")
};

Do while to get returned position and names:

do
                {
                    // get the movie title
                    cout << "Enter the movie title to search: " << endl;
                    cin >> title;

                    // search for the object
                    pos = searchMovies(hollywood, ARR_SIZE, title);

                    // if pos = -1 the title was not found
                    if (pos == -1)
                        cout << "That title does not exit in the list.\n";
                    else
                    {
                        // the object was found so use the get pos to get the description
                        cout << "The movie: " << moviesArr[pos].getTitle() << " is in the list. " << endl;
                        cout << "It was Directed by: " << moviesArr[pos].getDirector() << endl;
                        cout << "It also stars: " << moviesArr[pos].getActors() << endl;
                    }

                    // does the user want to look up another movie?
                    cout << "\nLook up another movie? (Y/N) ";
                    cin >> doAgain;

                } while (doAgain == 'Y' || doAgain == 'y');

Search function to search array:

int searchMovies(const Movies object[], int ARR_SIZE, string value){
int index = 0;          
int position = -1;      
bool found = false;     

while (index < ARR_SIZE && !found)
{
    if (object[index].getTitle() == value)  // if the title is found
    {
        found = true;       // set the flag.
        position = index;   // record the values subscript
    }
    index++;                // go to the next element.
}
return position;            // return the position or -1;}

output looks like: Nondescrip return, no names, no director, no title.

Upvotes: 0

Views: 39

Answers (1)

cigien
cigien

Reputation: 60238

This line:

Movies moviesArr[ARR_SIZE];

defines an empty, uninitialized array of Movies. You are searching for a particular Movie title in hollywood, which finds the movie. However, you are then indexing moviesArr:

 cout << "The movie: " << moviesArr[pos].getTitle() << " is in the list. " << endl;

which is undefined behaviour (and printed nothing for the title in your case).

Replace that with:

 cout << "The movie: " << hollywood[pos].getTitle() << " is in the list. " << endl;

Upvotes: 1

Related Questions