Help with video rental program using linked lists and arrays

Hi, I am trying to program a video rental checkout that reads in a file of given movies that the user can then search.

**I am not able to use anything outside the C++ standard library.**

I was thinking of reading in the file and creating a Node for each movie entry. I am trying to make it searchable by date of release, director, actors as well as obviously the title, and I was thinking a good way to do this would be by storing the searchable values in separate arrays that, when the user searches something like "Kristen Stewart" it can search through the "actors" array and find all titles corresponding to the actor. My trouble is more conceptual right now, as I'm pretty new to C++.

How could I get the values in the array to point to the correct Node if I went this route?

edit: extra clarification

**we can only use these key-words/includes/directives: class, public, private, new, delete, if, else, switch, case, for, do, while, break, continue, return, true, false, void, int, long, char, float, double, bool, use, namespace, <iostream>, <ifstream>, <string>, <string.h>, #ifndef, #endif, #define, #include.**
Last edited on
This isn't that much different than the Phonebook issue you had. You had some data in a file, and you need to store it in the memory of the program, but this time in order to query it. You could use a similar approach here.

storing the searchable values in separate arrays that
You could do this. It isn't wrong. Separate arrays, often called "structure of arrays" (SOA), may be warranted depending on the types of data you're dealing (may help with cache locality), but since you're just a beginner, I think a simple "array of structures" is easier here.

You'd have your Movie struct:
1
2
3
4
5
6
struct Movie {
    string release_date;
    string director;
    string title;
    string actors;
};


Then, you can have an array with some max size, or vector (dynamic array) of Movie objects.

Then, to search by a certain category, just make a function. If you know iterators, you could return an iterator to the given element. If not, you could return the index it was found at, or just print the information.
1
2
3
4
5
6
7
8
9
10
11
int search_by_title(const std::vector<Movie>& movies, const std::string& title)
{
    for (int i = 0; i < (int)movies.size(); i++)
    {
        if (movies[i].title.find(title) != std::string::npos)
        {
            return i;
        }
    }
    return -1;
}


Edit:
**we can only use these key-words/includes/directives: class, public, private, new, delete, if, else, switch, case, for, do, while, break, continue, return, true, false, void, int, long, char, float, double, bool, use, namespace, <iostream>, <ifstream>, <string>, <string.h>, #ifndef, #endif, #define, #include.**

Okay, then instead of a struct, just do class (same thing, except struct is public by default).
And instead of vector, do an array with some maximum size.

1
2
3
4
5
6
7
class Movie {
  public:
    string release_date;
    string director;
    string title;
    string actors;
};


Movie movies[1000];

1
2
3
4
5
6
7
8
9
10
11
int search_by_title(Movie movies[], int size, const std::string& title)
{
    for (int i = 0; i < size; i++)
    {
        if (movies[i].title.find(title) != std::string::npos)
        {
            return i;
        }
    }
    return -1;
}
Last edited on
@Ganado
this is what I was thinking of doing at first, but my tutor suggested the separate arrays so I was going to see if that would be a better solution and how I would do that. Is the class "Movie" in your example basically a node? or is it different than that?
IMO I'd have an array of class/struct so that when sorting/searching you keep associated data together. It's easy with different arrays to get things 'out of step'.
@seeplus thats my worry as well, I may end up going that route in the end but would like to see how creating the arrays and searching them could work out. Idk
If you want to do separate arrays, it's not a big deal either way; it's just an assignment. Ultimately, do what your instructor tells you to do if that's how you're graded.

If you do "structure of array" approach, you'd have something like:

1
2
3
4
5
6
7
8
const int MaxSize = 1000;

class Movies {
  public:
    string titles[MaxSize];
    string directors[MaxSize];
    // etc.
};


and you can pass an individual member to query.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

int query_titles(string titles[], int num_movies, const string& search_title)
{
    // ...
}

// ...

Movies movies;
// fill in movies from file

if (user chooses to search by title)
{
    int index = query_titles(movies.titles, num_movies, title_to_search_for);
    if (index != -1)
    {
        // movie found.
        
    }
}
Last edited on
Topic archived. No new replies allowed.