Prompt the user for two years. Print all Comedy movies that were released between those two years.

this code works just in case anyone has this assignment. i figured it out yesterday. please help me out on my other assignment.. you should find it on todays date.

#include <iostream> //cout, cin, endl,getline
#include <fstream> //ifstream
#include <cstdlib> // exit
using namespace std;

const int NUM_MOVIES = 116792;

struct Movie
{
string name;
string year;
string genre;
}; //dont forget semi colon

//this function takes a blank array and fills that array up with movie data
void load_movies(Movie blank_array[])
{
//step 1 : stream variable
ifstream data_store;

//step 2 : open file
data_store.open("movie_database.txt");

//step 3: processing the file
if(!data_store)
{
cout << "movie_database.txt is not in this directory" << endl;
exit(0); // end the program no matter where you are
}

for (int counter = 0; counter < NUM_MOVIES; counter++)
{
string name, year, genre;
getline(data_store, name);
getline(data_store, year);
getline(data_store, genre);

blank_array[counter].name = name;
blank_array[counter].year = year;
blank_array[counter].genre = genre;
}

//step 4: close file
data_store.close();
}

int main()
{
Movie movie_array[NUM_MOVIES];
load_movies(movie_array);

string year_1, year_2;
string genre = "Comedy";
cout << "Comedy Movie Finder" << endl;
cout << "Enter beginning year" << endl;
getline(cin, year_1);
cout << "Enter ending year" << endl;
getline(cin, year_2);


//search for movies that correspond
//this loop visit all 116792 movies
for(int counter = 0; counter < NUM_MOVIES; counter++)
{

if (year_1 <= movie_array[counter].year
&& year_2 >= movie_array[counter]. year && genre == movie_array[counter].genre)
{
//display the movie
cout << movie_array[counter].name << " " << movie_array[counter].year << endl;
}
}

return 0;
}
Last edited on
Topic archived. No new replies allowed.