233k views
3 votes
Your Program Write a program that reads information about movies from a file named movies.txt. Each line of this file contains the name of a movie, the year the movie was released, and the revenue for that movie. Your program reads this file and stores the movie data into a list of movies. It then sorts the movies by title and prints the sorted list. Finally, your program asks the user to input a title and the program searches the list of movies and prints the information about that movie. To represent the movie data you have to create a struct named Movie with three members: title, yearReleased, and revenue. To handle the list of movies you have to create an array of Movie structs named topMovies capable of storing up to 20 movies. Your solution must be implemented using the following functions: storeMoviesArray(ifstream inFile, Movies topMovies[], const int SIZE) // This function receives the input file, the movies array, and the size of the // array. // It reads from the file the movie data and stores it in the array. // Once all the data has been read in, it returns the array with the information // of the movies. sortMoviesTitle(Movie topMovies[], const int SIZE)

// This function receives the movies array and the size of the array and sorts // the array by title. It returns the sorted array. printMoviesArray(Movie topMovies[], const int SIZE) // This function receives the movies array and the size of the array and prints // the list of movies. find MovieTitle(Movie topMovies[],const int SIZE, string title) // This function receives the movies array, the size of the array, and the title // of the movie to be searched for. // It returns the index of the array where the title was found. If the title was // not found, it returns - 1. It must use binary search to find the movie. main() // Takes care of the file (opens and closes it). // Calls the functions and asks whether to continue.

1 Answer

6 votes

Answer:

To read information from a file and sort it by title and search for a specific movie in C++, you need to implement the functions storeMoviesArray, sortMoviesTitle, printMoviesArray, and findMovieTitle. The main function takes care of opening and closing the file, and calling the other functions.

Here is an example implementation:

#include <iostream>

#include <fstream>

#include <string>

#include <algorithm>

using namespace std;

struct Movie {

string title;

int yearReleased;

double revenue;

};

void storeMoviesArray(ifstream& inFile, Movie topMovies[], const int SIZE) {

int count = 0;

while (inFile >> topMovies[count].title >> topMovies[count].yearReleased >> topMovies[count].revenue) {

count++;

if (count == SIZE) {

break;

}

}

}

void sortMoviesTitle(Movie topMovies[], const int SIZE) {

sort(topMovies, topMovies + SIZE, [](const Movie& a, const Movie& b) {

return a.title < b.title;

});

}

void printMoviesArray(Movie topMovies[], const int SIZE) {

for (int i = 0; i < SIZE; i++) {

cout << topMovies[i].title << " (" << topMovies[i].yearReleased << "), " << "$" << topMovies[i].revenue << endl;

}

}

int findMovieTitle(Movie topMovies[],const int SIZE, string title) {

int start = 0;

int end = SIZE - 1;

while (start <= end) {

int mid = start + (end - start) / 2;

if (topMovies[mid].title == title) {

return mid;

}

else if (topMovies[mid].title < title) {

start = mid + 1;

}

else {

end = mid - 1;

}

}

return -1;

}

int main() {

const int SIZE = 20;

Movie topMovies[SIZE];

ifstream inFile("movies.txt");

if (!inFile) {

cerr << "Cannot open file.\\";

return 1;

}

storeMoviesArray(inFile, topMovies, SIZE);

inFile.close();

sortMoviesTitle(topMovies, SIZE);

printMoviesArray(topMovies, SIZE);

cout << endl;

string title;

cout << "

Step-by-step explanation:

User Rogier
by
9.1k points

No related questions found