need a movies.h and movies.cpp program fomr a structure program converted to a c
ID: 3746367 • Letter: N
Question
need a movies.h and movies.cpp program fomr a structure program converted to a class program titled moviesh and movies.cpp
here is the .h structure file
#ifndef MOVIES_H
#define MOVIES_H
#include "Movie.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct Movies
{
Movie** moviesArray; //an array of pointers - each pointer points to a single Movie
int maxMovies; //maximum number of elements in the array
int numMovies; //current number of movies in the array
};
/*
Function name: createMovies
Parameters: An integer containing the maximum size of the movie library
Returns: A pointer to a new Movies structure
Purpose: This function should be called when the user needs to create a library
of movies. The function will dynamically allocate a movies array based
on the maximum size and will also set the current number of movies to zero.
*/
Movies* createMovies(int);
/*
Function name: addMovieToArray
Parameters: 1) The movies structure (which contains the movie library)
Returns: none
Purpose: This function should be called when you need to add a single movie to the
movie library.
*/
void addMovieToArray(Movies*);
/*
Function name: editMovieInArray
Parameters: The movies structure (which contains the movie library)
Returns: none
Purpose: This function should be called when you need to edit a movie in the array
*/
void editMovieInArray(Movies*);
/*
Function name: destroyMovies
Parameters: 1) The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function should be called when you need to remove all the single
movies in the movie library as well as the movie library. This releases
all the dynamically allocated space in memory.
*/
void destroyMovies(Movies*);
/*
Function name: displayMovies
Parameters: 1) The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function should be called when the user wants to have all the movies
in the library printed to the screen.
*/
void displayMovies(Movies*);
/*
Function name: displayMovieTitles
Parameters: 1) The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function should be called when you want to print only the movie titles
out of the movie library
*/
void displayMovieTitles(Movies*);
/*
Function name: readMoviesFromFile
Parameters: 1) A pointer to a character (c-string or string literal argument) containing the filename
2) The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function should be called when the user wants to read movie data from a file
and add the movies to the movie library. The file must have data in the following order:
title, length, year, genre, rating, num oscars won, star rating
*/
void readMoviesFromFile(char* filename, Movies* myMovies);
/*
Function name: removeMovieFromArray
Parameters: The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function should be called when the user wants to remove one single movie
from the movie library. The function will list all the movie names and allow
the user to select the movie that they wish to remove. Then this function removes the movie.
*/
void removeMovieFromArray(Movies* myMovies);
/*
Function name: saveToFile
Parameters: 1) A pointer to a character (c-string or string literal argument) containing the filename
2) The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function should be called when the user wants to print all the movie data
from the movie library to a file. The data is printed in the following order (one piece
of data per line):
title, length, year, genre, rating, num oscars won, star rating
*/
void saveToFile(char *filename, Movies* myMovies);
/*
Function name: resizeMovieArray
Parameters: The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function is called by addMovieToArray when the array size is not big enough
to hold a new movie that needs to be added. The function makes the array twice
as big as it currently is and then moves all the movie pointers to this new array.
*/
void resizeMovieArray(Movies* myMovies);
#endif
here is the structure .cpp file
#include "Movies.h"
#include "Movie.h"
Movies* createMovies(int max)
{
//dynamically create a new Movies structure
Movies* myMovies = new Movies;
myMovies->maxMovies = max;
myMovies->numMovies = 0;
//dynamically create the array that will hold the movies
myMovies->moviesArray = new Movie*[max];
return myMovies;
}
void resizeMovieArray(Movies* myMovies)
{
int max = myMovies->maxMovies * 2; //increase size by 2
//make an array that is twice as big as the one I've currently got
Movie** newMoviesArray = new Movie*[max];
for(int x = 0; x < myMovies->numMovies; x++)
{
newMoviesArray[x] = myMovies->moviesArray[x];
}
//delete the original array from memory
delete [] myMovies->moviesArray;
myMovies->moviesArray = newMoviesArray;
myMovies->maxMovies = max;
}
void addMovieToArray(Movies* myMovies)
{
char tempString[100];
int length, year, numOscars;
double numStars;
Text* title;
Text* genre;
Text* rating;
//get movie data from the user
cin.ignore(); //remove the from the keyboard buffer
cout << " MOVIE TITLE: ";
cin.getline(tempString, 100);
title = createText(tempString);
cout << " MOVIE LENGTH (in minutes): ";
cin >> length;
cout << " MOVIE YEAR: ";
cin >> year;
cin.ignore();
cout << " MOVIE GENRE: ";
cin.getline(tempString, 100);
genre = createText(tempString);
cout << " MOVIE RATING: ";
cin.getline(tempString, 100);
rating = createText(tempString);
cout << " NUMBER OF OSCARS WON: ";
cin >> numOscars;
cout << " STAR RATING (out of 10): ";
cin >> numStars;
//create the movie
Movie* length, year, genre, rating, numOscars, numStars);
//add the movie to the library
if(myMovies->numMovies == myMovies->maxMovies)
resizeMovieArray(myMovies); //increase size by 2
myMovies->moviesArray[myMovies->numMovies] = oneMovie;
(myMovies->numMovies)++;
}
void removeMovieFromArray(Movies* myMovies)
{
int movieChoice;
cout << myMovies->numMovies << endl << endl;
//delete a movie if there is more than one movie in the library.
if((myMovies->numMovies) <= 1)
{
cout << endl << "There must always be at least one movie in your library. You can't";
cout << " remove any movies right now or you will have no movies in your library. ";
}
else
{
cout << " Choose from the following movies to remove: ";
displayMovieTitles(myMovies);
cout << " Choose a movie to remove between 1 & " << myMovies->numMovies << ": ";
cin >> movieChoice;
while(movieChoice < 1 || movieChoice > myMovies->numMovies)
{
cout << " Oops! You must enter a number between 1 & " << myMovies->numMovies << ": ";
cin >> movieChoice;
}
int movieIndexToBeRemoved = movieChoice-1;
Text* movieTitle;
movieTitle = myMovies->moviesArray[movieIndexToBeRemoved]->movieTitle;
//destroy this movie
destroyMovie(myMovies->moviesArray[movieIndexToBeRemoved]);
int numElementsToMoveBack = (myMovies->numMovies) - 1;
for(int x = movieIndexToBeRemoved; x < numElementsToMoveBack; x++)
{
myMovies->moviesArray[x] = myMovies->moviesArray[x+1]; //move array elements!
}
//set the last movie to a null pointer
myMovies->moviesArray[numElementsToMoveBack] = NULL;
//decrement the current number of movies
(myMovies->numMovies)--;
cout << " The movie "";
displayText(movieTitle);
cout << "" has been successfully deleted. ";
}
}
void editMovieInArray(Movies* myMovies)
{
int movieChoice;
cout << " Choose from the following movies to edit: ";
displayMovieTitles(myMovies);
cout << " Choose a movie to remove between 1 & " << myMovies->numMovies << ": ";
cin >> movieChoice;
while(movieChoice < 1 || movieChoice > myMovies->numMovies)
{
cout << " Oops! You must enter a number between 1 & " << myMovies->numMovies << ": ";
cin >> movieChoice;
}
Movie*> editMovie(oneMovie);
}
void destroyMovies(Movies* myMovies)
{
//delete each movie
for(int x=0; x< myMovies->numMovies; x++)
{
//delete myMovies->moviesArray[x];
destroyMovie(myMovies->moviesArray[x]);
}
//delete movies array
delete [] myMovies->moviesArray;
//delete myMovies
delete myMovies;
}
void displayMovies(Movies* myMovies)
{
if(myMovies->numMovies > 0)
{
for(int x=0; x < (myMovies->numMovies); x++)
{
cout << endl << right << setw(50) << "----------MOVIE " << (x+1) << "----------";
printMovieDetails(myMovies->moviesArray[x]); //function is in Movie.cpp
}
}
else
cout << " There are no movies in your library yet.";
}
void displayMovieTitles(Movies* myMovies)
{
Text* movieTitle;
for(int x=0; x < (myMovies->numMovies); x++)
{
cout << " MOVIE " << (x+1) <<": ";
movieTitle = myMovies->moviesArray[x]->movieTitle;
displayText(movieTitle);
}
}
void readMoviesFromFile(char *filename, Movies* myMovies)
{
int numMoviesReadFromFile = 0;
ifstream inFile;
char temp[100];
Text* title;
Text* genre;
Text* rating;
Movie* oneMovie;
int movieLength; //length of movie in minutes
int movieYear; //year released
int movieOscars; //number of oscars won
float movieNumStars; //from IMDB out of 10 stars
inFile.open(filename);
if(inFile.good())
{
inFile.getline(temp, 100);
while(!inFile.eof())
{
title = createText(temp);//create a text for the movie title
inFile >> movieLength;
inFile >> movieYear;
inFile.ignore(); //get rid of in the inFile buffer
inFile.getline(temp, 100); //read in genre
genre = createText(temp); //create a text for genre
inFile.getline(temp, 100); //read in rating
rating = createText(temp); //create a text for rating
inFile >> movieOscars;
inFile >> movieNumStars;
inFile.ignore(); //get rid of in the inFile buffer
//one movie has been read from the file. Now create a movie object
oneMovie = createMovie(title, movieLength, movieYear, genre, rating, movieOscars, movieNumStars);
//now add this movie to the library
if(myMovies->numMovies == myMovies->maxMovies)
resizeMovieArray(myMovies); //increase size by 2
myMovies->moviesArray[myMovies->numMovies] = oneMovie;
(myMovies->numMovies)++;
//confirm addition to the user
cout << endl;
displayText(title);
cout << " was added to the movie library! ";
inFile.getline(temp, 100); //read in the next movie title if there is one
numMoviesReadFromFile++;
}
cout << " " << numMoviesReadFromFile << " movies were read from the file and added to your movie library. ";
}
else
{
cout << " Sorry, I was unable to open the file. ";
}
}
void saveToFile(char *filename, Movies* myMovies)
{
ofstream outFile;
outFile.open(filename);
for(int x=0; x < (myMovies->numMovies); x++)
{
printMovieDetailsToFile(myMovies->moviesArray[x], outFile); //function in Movies.cpp
}
outFile.close();
cout << " All movies in your library have been printed to " << filename << endl;
}
Explanation / Answer
If you have any doubts/errors, please give me comment...
movies.h
#ifndef MOVIES_H
#define MOVIES_H
#include "Movie.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class Movies
{
public:
Movie **moviesArray; // an array of pointers - each pointer points to a single
// Movie
int maxMovies; // maximum number of elements in the array
int numMovies; // current number of movies in the array
/*
Function name: createMovies
Parameters: An integer containing the maximum size of the movie library
Returns: A pointer to a new Movies structure
Purpose: This function should be called when the user needs to create a
library
of movies. The function will dynamically allocate a movies array based
on the maximum size and will also set the current number of movies to
zero.
*/
Movies *createMovies(int);
/*
Function name: addMovieToArray
Parameters: 1) The movies structure (which contains the movie library)
Returns: none
Purpose: This function should be called when you need to add a single movie
to the
movie library.
*/
void addMovieToArray();
/*
Function name: editMovieInArray
Parameters: The movies structure (which contains the movie library)
Returns: none
Purpose: This function should be called when you need to edit a movie in the
array
*/
void editMovieInArray();
/*
Function name: destroyMovies
Parameters: 1) The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function should be called when you need to remove all the
single
movies in the movie library as well as the movie library. This releases
all the dynamically allocated space in memory.
*/
void destroyMovies();
/*
Function name: displayMovies
Parameters: 1) The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function should be called when the user wants to have all the
movies
in the library printed to the screen.
*/
void displayMovies();
/*
Function name: displayMovieTitles
Parameters: 1) The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function should be called when you want to print only the
movie titles
out of the movie library
*/
void displayMovieTitles();
/*
Function name: readMoviesFromFile
Parameters: 1) A pointer to a character (c-string or string literal argument)
containing the filename
2) The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function should be called when the user wants to read movie
data from a file
and add the movies to the movie library. The file must have data in the
following order:
title, length, year, genre, rating, num oscars won, star rating
*/
void readMoviesFromFile(char *filename);
/*
Function name: removeMovieFromArray
Parameters: The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function should be called when the user wants to remove one
single movie
from the movie library. The function will list all the movie names and
allow
the user to select the movie that they wish to remove. Then this function
removes the movie.
*/
void removeMovieFromArray();
/*
Function name: saveToFile
Parameters: 1) A pointer to a character (c-string or string literal argument)
containing the filename
2) The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function should be called when the user wants to print all the
movie data
from the movie library to a file. The data is printed in the following
order (one piece
of data per line):
title, length, year, genre, rating, num oscars won, star rating
*/
void saveToFile(char *filename);
/*
Function name: resizeMovieArray
Parameters: The movies structure (which contains the movie library)
Returns: none (void)
Purpose: This function is called by addMovieToArray when the array size is
not big enough
to hold a new movie that needs to be added. The function makes the array
twice
as big as it currently is and then moves all the movie pointers to this
new array.
*/
void resizeMovieArray();
};
#endif
movies.cpp
#include "movies.h"
#include "Movie.h"
Movies *Movies::createMovies(int max)
{
//dynamically create a new Movies structure
Movies *myMovies = new Movies;
myMovies->maxMovies = max;
myMovies->numMovies = 0;
//dynamically create the array that will hold the movies
myMovies->moviesArray = new Movie *[max];
return myMovies;
}
void Movies::resizeMovieArray()
{
int max = maxMovies * 2; //increase size by 2
//make an array that is twice as big as the one I've currently got
Movie **newMoviesArray = new Movie *[max];
for (int x = 0; x < numMovies; x++)
{
newMoviesArray[x] = moviesArray[x];
}
//delete the original array from memory
delete[] moviesArray;
moviesArray = newMoviesArray;
maxMovies = max;
}
void Movies::addMovieToArray()
{
char tempString[100];
int length, year, numOscars;
double numStars;
Text *title;
Text *genre;
Text *rating;
//get movie data from the user
cin.ignore(); //remove the from the keyboard buffer
cout << " MOVIE TITLE: ";
cin.getline(tempString, 100);
title = createText(tempString);
cout << " MOVIE LENGTH (in minutes): ";
cin >> length;
cout << " MOVIE YEAR: ";
cin >> year;
cin.ignore();
cout << " MOVIE GENRE: ";
cin.getline(tempString, 100);
genre = createText(tempString);
cout << " MOVIE RATING: ";
cin.getline(tempString, 100);
rating = createText(tempString);
cout << " NUMBER OF OSCARS WON: ";
cin >> numOscars;
cout << " STAR RATING (out of 10): ";
cin >> numStars;
//create the movie
Movie *oneMovie = createMovie(title, length, year, genre, rating, numOscars, numStars);
//add the movie to the library
if (numMovies == maxMovies)
resizeMovieArray(); //increase size by 2
moviesArray[numMovies] = oneMovie;
numMovies++;
}
void Movies::removeMovieFromArray()
{
int movieChoice;
cout << numMovies << endl
<< endl;
//delete a movie if there is more than one movie in the library.
if (numMovies <= 1)
{
cout << endl
<< "There must always be at least one movie in your library. You can't";
cout << " remove any movies right now or you will have no movies in your library. ";
}
else
{
cout << " Choose from the following movies to remove: ";
displayMovieTitles();
cout << " Choose a movie to remove between 1 & " << numMovies << ": ";
cin >> movieChoice;
while (movieChoice < 1 || movieChoice > numMovies)
{
cout << " Oops! You must enter a number between 1 & " << numMovies << ": ";
cin >> movieChoice;
}
int movieIndexToBeRemoved = movieChoice - 1;
Text *movieTitle;
movieTitle = moviesArray[movieIndexToBeRemoved]->movieTitle;
//destroy this movie
destroyMovie(moviesArray[movieIndexToBeRemoved]);
int numElementsToMoveBack = numMovies - 1;
for (int x = movieIndexToBeRemoved; x < numElementsToMoveBack; x++)
{
moviesArray[x] = moviesArray[x + 1]; //move array elements!
}
//set the last movie to a null pointer
moviesArray[numElementsToMoveBack] = NULL;
//decrement the current number of movies
(numMovies)--;
cout << " The movie "";
displayText(movieTitle);
cout << "" has been successfully deleted. ";
}
}
void Movies::editMovieInArray()
{
int movieChoice;
cout << " Choose from the following movies to edit: ";
displayMovieTitles();
cout << " Choose a movie to remove between 1 & " << numMovies << ": ";
cin >> movieChoice;
while (movieChoice < 1 || movieChoice > numMovies)
{
cout << " Oops! You must enter a number between 1 & " << numMovies << ": ";
cin >> movieChoice;
}
Movie *oneMovie = moviesArray[movieChoice - 1];
editMovie();
}
void Movies::destroyMovies()
{
//delete each movie
for (int x = 0; x < numMovies; x++)
{
//delete myMovies->moviesArray[x];
destroyMovie(moviesArray[x]);
}
//delete movies array
delete[] moviesArray;
//delete myMovies
delete myMovies;
}
void Movies::displayMovies()
{
if (numMovies > 0)
{
for (int x = 0; x < (numMovies); x++)
{
cout << endl
<< right << setw(50) << "----------MOVIE " << (x + 1) << "----------";
printMovieDetails(moviesArray[x]); //function is in Movie.cpp
}
}
else
cout << " There are no movies in your library yet.";
}
void Movies::displayMovieTitles()
{
Text *movieTitle;
for (int x = 0; x < (numMovies); x++)
{
cout << " MOVIE " << (x + 1) << ": ";
movieTitle = moviesArray[x]->movieTitle;
displayText(movieTitle);
}
}
void Movies::readMoviesFromFile(char *filename)
{
int numMoviesReadFromFile = 0;
ifstream inFile;
char temp[100];
Text *title;
Text *genre;
Text *rating;
Movie *oneMovie;
int movieLength; //length of movie in minutes
int movieYear; //year released
int movieOscars; //number of oscars won
float movieNumStars; //from IMDB out of 10 stars
inFile.open(filename);
if (inFile.good())
{
inFile.getline(temp, 100);
while (!inFile.eof())
{
title = createText(temp); //create a text for the movie title
inFile >> movieLength;
inFile >> movieYear;
inFile.ignore(); //get rid of in the inFile buffer
inFile.getline(temp, 100); //read in genre
genre = createText(temp); //create a text for genre
inFile.getline(temp, 100); //read in rating
rating = createText(temp); //create a text for rating
inFile >> movieOscars;
inFile >> movieNumStars;
inFile.ignore(); //get rid of in the inFile buffer
//one movie has been read from the file. Now create a movie object
oneMovie = createMovie(title, movieLength, movieYear, genre, rating, movieOscars, movieNumStars);
//now add this movie to the library
if (numMovies == maxMovies)
resizeMovieArray(); //increase size by 2
moviesArray[numMovies] = oneMovie;
numMovies++;
//confirm addition to the user
cout << endl;
displayText(title);
cout << " was added to the movie library! ";
inFile.getline(temp, 100); //read in the next movie title if there is one
numMoviesReadFromFile++;
}
cout << " "
<< numMoviesReadFromFile << " movies were read from the file and added to your movie library. ";
}
else
{
cout << " Sorry, I was unable to open the file. ";
}
}
void Movies::saveToFile(char *filename)
{
ofstream outFile;
outFile.open(filename);
for (int x = 0; x < (numMovies); x++)
{
printMovieDetailsToFile(moviesArray[x], outFile); //function in Movies.cpp
}
outFile.close();
cout << " All movies in your library have been printed to " << filename << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.