PLEASE HELP ME! I need help with my c++ program: the instructions are: Write a p
ID: 3670010 • Letter: P
Question
PLEASE HELP ME!
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 <iostream>
#include <fstream>
#include <sstream>
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 << " Title: " << m.title << endl;
cout << " Director: " << m.director << endl;
cout << " year: " << m.year << endl;
cout << " duration: " << m.duration << endl << endl;
}
inputFile.close();
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
struct Movie {
string title;
string director;
string year;
string duration;
};
bool compareTitle(Movie const &lhs,Movie const &rhs){
return (lhs.title).compare(rhs.title);
}
bool compareDirector(Movie const &lhs,Movie const &rhs){
return (lhs.director).compare(rhs.director);
}
bool compareYear(Movie const &lhs,Movie const &rhs){
return (lhs.year).compare(rhs.year);
}
bool compareDuration(Movie const &lhs,Movie const &rhs){
return (lhs.duration).compare(rhs.duration);
}
void printVector(vector<Movie> v){
for ( unsigned int i = 0; i < v.size(); i++) {
cout << v[i].title << " "<<v[i].director << " "<<v[i].year << " "<<v[i].duration <<endl;
}
cout << endl;
}
int main()
{
ifstream inputFile;
string line;
inputFile.open("Movie_entries.txt");
std::vector<Movie> st;
Movie m;
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, ',');
st.push_back(m);
cout << " Title: " << m.title << endl;
cout << " Director: " << m.director << endl;
cout << " year: " << m.year << endl;
cout << " duration: " << m.duration << endl << endl;
}
inputFile.close();
cout<<"Chhose your option- how you want to see info"<<endl;
cout<<"1. Sorted by Title."<<endl;
cout<<"2. Sorted by Director."<<endl;
cout<<"3. Sorted by Year."<<endl;
cout<<"4. Sorted by Duration."<<endl;
int x;
cin>>x;
switch(x){
case 1:
sort(st.begin(), st.end(), &compareTitle);
printVector(st);
break;
case 2:
sort(st.begin(), st.end(), &compareDirector);
printVector(st);
break;
case 3:
sort(st.begin(), st.end(), &compareYear);
printVector(st);
break;
case 4:
sort(st.begin(), st.end(), &compareDuration);
printVector(st);
break;
default:
cout<<"Invalid option"<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.