moviestruct.cpp (project 2 code) #include <iostream> #include <fstream> #include
ID: 3866095 • Letter: M
Question
moviestruct.cpp (project 2 code)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef struct{
int id;
char title[250];
int year;
char rating[6];
int totalCopies;
int rentedCopies;
}movie;
int loadData(ifstream &infile, movie movies[]);
void printAll(movie movies[], int count);
void printRated(movie movies[], int count);
void printTitled(movie movies[], int count);
void addMovie(movie movies[],int &count);
void returnMovie(movie movies[],int count);
void rentMovie(movie movies[],int count);
void saveToFile(movie movies[], int count, char *filename);
void printMovie(movie &m);
int find(movie movies[], int count, int id);
int main(int argc, char *argv[])
{
//open file for catalog.txt
char filename[100] = "catalog.txt";
ifstream infile(filename);
movie movies[200];
int count = 0;
if(infile.is_open())
{
count = loadData(infile, movies);
}
int choice = 0;
while(choice != 7)
{
cout << " ********** MAIN MENU ********** " << endl;
cout << "1 - Print Calalog" << endl;
cout << "2 - Search by Title" << endl;
cout << "3 - Search by Rating" << endl;
cout << "4 - Add Movie" << endl;
cout << "5 - Rent Movie" << endl;
cout << "6 - Return Movie" << endl;
cout << "7 - Quit" << endl;
cout << "Enter choice: ";
cin >> choice; //switch statements for different user choices
switch(choice)
{
case 1:
printAll(movies, count);
break;
case 2:
printTitled(movies, count);
break;
case 3:
printRated(movies, count);
break;
case 4:
addMovie(movies, count);
break;
case 5:
rentMovie(movies, count);
break;
case 6:
returnMovie(movies, count);
break;
case 7:
saveToFile(movies, count, filename);
break;
default:
cout << "Invalid choice. Please re-enter: " << endl;
}
}
return 0;
}
int loadData(ifstream &infile, movie movies[])
{
int count = 0;
while(infile >> movies[count].id)
{
infile.ignore();
infile.getline(movies[count].title, 250);
infile >> movies[count].year;
infile >> movies[count].rating;
infile >> movies[count].totalCopies;
infile >> movies[count].rentedCopies;
count++;
}
infile.close();
return count;
}
void printMovie(movie &m)
{
cout << "---------- ID: " << m.id << " ----------" << endl;
cout << "Title: " << m.title << endl;
cout << "Year: " << m.year << endl;
cout << "Rating: " << m.rating << endl;
cout << "Number of copies: " << m.totalCopies << endl;
cout << "Number rented: " << m.rentedCopies << endl;
cout << "---------------------------------------" << endl;
}
void printAll(movie movies[], int count)
{
cout << "%%%%%%%%%% Movie Catalog %%%%%%%%%%" << endl;
for(int i = 0 ;i < count; i++)
{
printMovie(movies[i]);
}
cout << endl;
}
void printRated(movie movies[], int count)
{
int r = -1;
char rating[5] = "";
cout << "0 - NONE" << endl;
cout << "1 - G" << endl;
cout << "2 - PG" << endl;
cout << "3 - PG13" << endl;
cout << "4 - R" << endl;
cout << "5 - N17" << endl;
while(true)
{
cin >> r;
if(r < 0 || r > 5)
cout << "Invalid choice. Please re-enter: ";
else
break;
}
if(r == 0)
strcpy(rating, "NONE");
else if(r == 1)
strcpy(rating, "G");
else if(r == 2)
strcpy(rating, "PG");
else if(r == 3)
strcpy(rating, "PG13");
else if(r == 4)
strcpy(rating, "R");
else if(r == 5)
strcpy(rating, "N17");
for(int i = 0 ;i < count; i++)
{
if(strcmp(movies[i].rating, rating) == 0)
printMovie(movies[i]);
}
cout << endl;
}
void printTitled(movie movies[], int count)
{
char title[250];
cout << "Title of Movie? " << endl;
cin.ignore(); //flush newline
cin.getline(title, 250);
cout << endl;
for(int i = 0 ;i < count; i++)
{
if(strcmp(movies[i].title, title) == 0)
{
printMovie(movies[i]);
break;
}
}
cout << endl;
}
int find(movie movies[], int count, int id)
{
for(int i = 0; i < count; i++)
{
if(movies[i].id == id)
return i;
}
return -1;
}
void addMovie(movie movies[],int &count) //pass by ref
{
int id;
cout << "Moview ID? (must be unique) " ;
cin >> id;
while(find(movies, count, id) != -1)
{
cout << "That ID is already in use. Please enter another: ";
cin >> id;
}
movies[count].id = id;
cin.ignore();
cout << "Title? ";
cin.getline(movies[count].title, 250);
cout << "Year? " ;
cin >> movies[count].year;
cout << "Movie rating? " << endl;
int r = -1;
cout << "0 - NONE" << endl;
cout << "1 - G" << endl;
cout << "2 - PG" << endl;
cout << "3 - PG13" << endl;
cout << "4 - R" << endl;
cout << "5 - N17" << endl;
while(true)
{
cin >> r;
if(r < 0 || r > 5)
cout << "Invalid choice. Please re-enter: ";
else
break;
}
if(r == 0)
strcpy(movies[count].rating, "NONE");
else if(r == 1)
strcpy(movies[count].rating, "G");
else if(r == 2)
strcpy(movies[count].rating, "PG");
else if(r == 3)
strcpy(movies[count].rating, "PG13");
else if(r == 4)
strcpy(movies[count].rating, "R");
else if(r == 5)
strcpy(movies[count].rating, "N17");
cout << "Copies? " ;
while(true)
{
cin >> movies[count].totalCopies;
if(movies[count].totalCopies <= 0 )
cout << "Copies must not be negative. Please re-enter: " ;
else
break;
}
movies[count].rentedCopies = 0;
count++;
}
void returnMovie(movie movies[],int count)
{
char answer;
cout << "Do you want to see a list of movies first? (y or n) ";
cin >> answer;
if(answer == 'y' || answer == 'Y')
printAll(movies, count);
int id;
cout << "Enter id of movie: ";
while(true)
{
cin >> id;
if(id == -1)
break;
int index = find(movies, count, id);
if(index == -1)
cout << "The movie does not exist. Please re-enter(or enter -1 to quit): " << endl;
else
{
if(movies[index].rentedCopies == 0)
cout << "No copies were rented." << endl;
else
{
movies[index].rentedCopies--;
cout << movies[index].title << " returned successfully. " << endl;
}
break;
}
}
}
void rentMovie(movie movies[],int count)
{
char answer;
cout << "Do you want to see a list of movies first? (y or n) ";
cin >> answer;
if(answer == 'y' || answer == 'Y')
printAll(movies, count);
int id;
cout << "Enter id of movie: ";
while(true)
{
cin >> id;
if(id == -1)
break;
int index = find(movies, count, id);
if(index == -1)
cout << "The movie does not exist. Please re-enter(or enter -1 to quit): " << endl;
else
{
if(movies[index].rentedCopies == movies[index].totalCopies)
cout << "There are no copies to rent." << endl;
else
{
movies[index].rentedCopies++;
cout << "One copy of " << movies[index].title << " rented. " << endl;
}
break;
}
}
}
void saveToFile(movie movies[], int count, char *filename)
{
ofstream outfile(filename);
if(!outfile.is_open())
cout << "Error writing to output file " << filename << endl;
else
{
for(int i = 0; i < count; i++)
{
outfile << movies[i].id << endl;
outfile << movies[i].title << endl;
outfile << movies[i].year << endl;
outfile << movies[i].rating << endl;
outfile << movies[i].totalCopies << endl;
outfile << movies[i].rentedCopies << endl;
}
outfile.close();
}
}
catalog.txt
10
Singing in the Rain
1952
NONE
10
1
20
Star Wars
1977
PG
20
7
30
Wonder Woman
2017
PG13
10
0
40
The Mummy
1999
PG13
9
3
50
The Shawshank Redemption
1994
R
3
1
60
The Mummy
2017
PG13
4
0
70
Cars
2006
G
6
0
80
Finding Nemo
2003
G
9
5
90
The moviestruct.cpp already works fine with catalog.txt. Just need to change moviestruct.cpp to meet the requirements and need a catalog class that works with it.
The purpose of this assignment is to use classes when structuring a program. You will be using your project 2 code and restructuring it to take classes instead of structs. You will also be giving the user a way to modify an existing movie in the catalog. What To Code Make the Movie struct a Movie class with private member variables for the ID, title, etc. Then make a Catalog class for the movie array and count as member variables of the class. Make both the movie array and count private. In your main function, you will create one Catalog object and use public member functions within the Catalog class to perform tasks on the movie array (add, modify, read in, write out to a file, find by title/ratings, print all, rent and return movies) Don't pass the array into function calls or return it from functions; you should have access to it within member functions of the Catalog class where all tasks performed on the array are done. Note that this restriction applies to the movie array not the Catalog object: you will need to pass that into main.cpp functions (not Catalog functions). Pass it by reference so that it can keep modifications between tasks. If it's not too hard to do, try to do most of the user input from main.cpp file instead of the two classes. Also, do the reading from and writing to the file in the Catalog class where the movie array resides. Keep in mind that the Movie class is holding information for just one movie. So if your function needs to access the movie array, don't put the function in the Movie class; put it in the Catalog class. Keep in mind that the Movie class is a 'data class' that basically stores information. It might do some tasks that deal with only one movie (such as printing the fields of one movie) but it shouldn't do much else. The Catalog class is the 'powerhouse in your program. That's where you'll do most of the tasks the user asks for from the main file. Movie Rental Tasks The program should start by reading in the catalog from 'catalog.txt file. The format is the same as in project 2. When program is done, the catalog should be written out to the same file. The task are the same as project 2 with one added task: 1. Modify a movie: Ask a user for a movie ID (like in rent/return task in project 2). Then present a menu of possible fields to change. Let the user pick a field to change; don't ust force the user to change all fields just to change one value. Make sure you check the user's input: ID's should be unique, valid movie ratings and no negative numbers.Explanation / Answer
Catalog.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef struct{
int id;
char title[250];
int year;
char rating[6];
int totalCopies;
int rentedCopies;
}movie;
class Catalog {
public:
movie movies[200];
int count = 0;
//member functions declaring
int loadData(ifstream &infile);
void printAll();
void printRated();
void printTitled();
void addMovie();
void returnMovie();
void rentMovie();
void saveToFile( char *filename);
void printMovie(movie &m);
int find(int id);
};
int Catalog::loadData(ifstream &infile, movie movies[])
{
int count = 0;
while(infile >> movies[count].id)
{
infile.ignore();
infile.getline(movies[count].title, 250);
infile >> movies[count].year;
infile >> movies[count].rating;
infile >> movies[count].totalCopies;
infile >> movies[count].rentedCopies;
count++;
}
infile.close();
return count;
}
void Catalog:: printMovie(movie &m)
{
cout << "---------- ID: " << m.id << " ----------" << endl;
cout << "Title: " << m.title << endl;
cout << "Year: " << m.year << endl;
cout << "Rating: " << m.rating << endl;
cout << "Number of copies: " << m.totalCopies << endl;
cout << "Number rented: " << m.rentedCopies << endl;
cout << "---------------------------------------" << endl;
}
void Catalog::printAll()
{
cout << "%%%%%%%%%% Movie Catalog %%%%%%%%%%" << endl;
for(int i = 0 ;i < count; i++)
{
printMovie(movies[i]);
}
cout << endl;
}
void Catalog::printRated()
{
int r = -1;
char rating[5] = "";
cout << "0 - NONE" << endl;
cout << "1 - G" << endl;
cout << "2 - PG" << endl;
cout << "3 - PG13" << endl;
cout << "4 - R" << endl;
cout << "5 - N17" << endl;
while(true)
{
cin >> r;
if(r < 0 || r > 5)
cout << "Invalid choice. Please re-enter: ";
else
break;
}
if(r == 0)
strcpy(rating, "NONE");
else if(r == 1)
strcpy(rating, "G");
else if(r == 2)
strcpy(rating, "PG");
else if(r == 3)
strcpy(rating, "PG13");
else if(r == 4)
strcpy(rating, "R");
else if(r == 5)
strcpy(rating, "N17");
for(int i = 0 ;i < count; i++)
{
if(strcmp(movies[i].rating, rating) == 0)
printMovie(movies[i]);
}
cout << endl;
}
void Catalog::printTitled()
{
char title[250];
cout << "Title of Movie? " << endl;
cin.ignore(); //flush newline
cin.getline(title, 250);
cout << endl;
for(int i = 0 ;i < count; i++)
{
if(strcmp(movies[i].title, title) == 0)
{
printMovie(movies[i]);
break;
}
}
cout << endl;
}
int Catalog::find(movie movies[], int count, int id)
{
for(int i = 0; i < count; i++)
{
if(movies[i].id == id)
return i;
}
return -1;
}
void Catalog::addMovie() //pass by ref
{
int id;
cout << "Moview ID? (must be unique) " ;
cin >> id;
while(find(movies, count, id) != -1)
{
cout << "That ID is already in use. Please enter another: ";
cin >> id;
}
movies[count].id = id;
cin.ignore();
cout << "Title? ";
cin.getline(movies[count].title, 250);
cout << "Year? " ;
cin >> movies[count].year;
cout << "Movie rating? " << endl;
int r = -1;
cout << "0 - NONE" << endl;
cout << "1 - G" << endl;
cout << "2 - PG" << endl;
cout << "3 - PG13" << endl;
cout << "4 - R" << endl;
cout << "5 - N17" << endl;
while(true)
{
cin >> r;
if(r < 0 || r > 5)
cout << "Invalid choice. Please re-enter: ";
else
break;
}
if(r == 0)
strcpy(movies[count].rating, "NONE");
else if(r == 1)
strcpy(movies[count].rating, "G");
else if(r == 2)
strcpy(movies[count].rating, "PG");
else if(r == 3)
strcpy(movies[count].rating, "PG13");
else if(r == 4)
strcpy(movies[count].rating, "R");
else if(r == 5)
strcpy(movies[count].rating, "N17");
cout << "Copies? " ;
while(true)
{
cin >> movies[count].totalCopies;
if(movies[count].totalCopies <= 0 )
cout << "Copies must not be negative. Please re-enter: " ;
else
break;
}
movies[count].rentedCopies = 0;
count++;
}
void Catalog::returnMovie()
{
char answer;
cout << "Do you want to see a list of movies first? (y or n) ";
cin >> answer;
if(answer == 'y' || answer == 'Y')
printAll(movies, count);
int id;
cout << "Enter id of movie: ";
while(true)
{
cin >> id;
if(id == -1)
break;
int index = find(movies, count, id);
if(index == -1)
cout << "The movie does not exist. Please re-enter(or enter -1 to quit): " << endl;
else
{
if(movies[index].rentedCopies == 0)
cout << "No copies were rented." << endl;
else
{
movies[index].rentedCopies--;
cout << movies[index].title << " returned successfully. " << endl;
}
break;
}
}
}
void Catalog::rentMovie()
{
char answer;
cout << "Do you want to see a list of movies first? (y or n) ";
cin >> answer;
if(answer == 'y' || answer == 'Y')
printAll(movies, count);
int id;
cout << "Enter id of movie: ";
while(true)
{
cin >> id;
if(id == -1)
break;
int index = find(movies, count, id);
if(index == -1)
cout << "The movie does not exist. Please re-enter(or enter -1 to quit): " << endl;
else
{
if(movies[index].rentedCopies == movies[index].totalCopies)
cout << "There are no copies to rent." << endl;
else
{
movies[index].rentedCopies++;
cout << "One copy of " << movies[index].title << " rented. " << endl;
}
break;
}
}
}
void Catalog::saveToFile(char *filename)
{
ofstream outfile(filename);
if(!outfile.is_open())
cout << "Error writing to output file " << filename << endl;
else
{
for(int i = 0; i < count; i++)
{
outfile << movies[i].id << endl;
outfile << movies[i].title << endl;
outfile << movies[i].year << endl;
outfile << movies[i].rating << endl;
outfile << movies[i].totalCopies << endl;
outfile << movies[i].rentedCopies << endl;
}
outfile.close();
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
test.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(int argc, char *argv[])
{
Catalog obj; //declaring catalog object
//open file for catalog.txt
char filename[100] = "catalog.txt";
ifstream infile(filename);
if(infile.is_open())
{
obj.loadData(infile);
}
int choice = 0;
while(choice != 7)
{
cout << " ********** MAIN MENU ********** " << endl;
cout << "1 - Print Calalog" << endl;
cout << "2 - Search by Title" << endl;
cout << "3 - Search by Rating" << endl;
cout << "4 - Add Movie" << endl;
cout << "5 - Rent Movie" << endl;
cout << "6 - Return Movie" << endl;
cout << "7 - Quit" << endl;
cout << "Enter choice: ";
cin >> choice; //switch statements for different user choices
switch(choice)
{
case 1:
obj.printAll();
break;
case 2:
obj.printTitled();
break;
case 3:
obj.printRated();
break;
case 4:
obj.addMovie();
break;
case 5:
obj.rentMovie();
break;
case 6:
obj.returnMovie();
break;
case 7:
obj.saveToFile(filename);
break;
default:
cout << "Invalid choice. Please re-enter: " << endl;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.