Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #inclu

ID: 3865672 • Letter: M

Question

moviestruct.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;

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.

Explanation / Answer

Catalog.h

====

#ifndef CATALOG_H

#define CATALOG_H

#include "Movie.h"

class Catalog {

private:

Movie movies[];

int count;

};

#endif // CATALOG_H

Movie.h

====

#ifndef MOVIE_H

#define MOVIE_H

class Movie {

int id;

char title[250];

int year;

char rating[6];

int totalCopies;

int rentedCopies;

};

#endif // MOVIE_H

Makefile

====

output: Movie.o Catalog.o main.o

g++ Movie.o Catalog.o main.o -o output

Movie.o: Movie.cpp Movie.h

g++ -c Movie.cpp

Catalog.o: Catalog.cpp Catalog.h

g++ -c Catalog.cpp

main.o: main.cpp

g++ -c main.cpp

clean:

rm *.o output