Question: I need help with my c++ program: the instructions are: Write a program to read a file containing movie data (Tile, Director, Year Released, Running
I need help with my c++ program:
the instructions are:
Write a program to read a file containing movie data (Tile, Director, Year Released, Running Time) into a vector of structures.
After the program reads the data from the file, have your program ask the user how he/she wants to see the data displayed on the screen.
Options are: sorted by title, director, year released or running time. You may choose to have a "menu of options" function to display the menu.
Use the sort function (#include ) to sort the requested data. You will need different comparison functions to pass to sort() depending on what field the data will be sorted on.
This is an example of a call to sort() assuming the vector name is myMovies and the user wants to sort by title: sort (myMovies.begin(), myMovies.end(), compareByTitle);
The compareByTitle function will sort the elements in the vector. Since the elements are structures, that is what should be passed to it as parameters: compareByTitle(Movie m1, Movie m2) The body of the compare functions will differ depending on what field within the structure needs to be compared (title, director, year released or duration)
After sorting, you should display all the information of each movie:
Title Director Year Released Duration
This is what I have so far and i dont know how to bring in my file and i also need to add in my vectors... if someone can help me please.
#include
#include
#include
using namespace std;
int main()
{
ifstream inputFile;
string line;
struct Movie {
string title;
string director;
string year;
string duration;
} m;
inputFile.open("Movie_entries.txt");
while (getline(inputFile, line)) // reads a line from the file
{
cout << line << endl;
stringstream lineStream(line); // transforms the line into a stream
// get fields from the string stream; fields are separated by comma
getline(lineStream, m.title, ',');
getline(lineStream, m.director, ',');
getline(lineStream, m.year, ',');
getline(lineStream, m.duration, ',');
cout << "\tTitle: " << m.title << endl;
cout << "\tDirector: " << m.director << endl;
cout << "\tyear: " << m.year << endl;
cout << "\tduration: " << m.duration << endl << endl;
}
inputFile.close();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
