Using C++, write a program that implments a rudimentary movie database. It inter
ID: 3679028 • Letter: U
Question
Using C++, write a program that implments a rudimentary movie database. It interacts with the user to get and display information. The data will be stored in a file so that it will persist between invocations of the program. The information that must be held is the movie's title, release year, 3 actors/actresses, the genre, and a list of scores ( from 1 to 10). When executing, the program should query the user for the name of the file form which the data is to be obtained, then present the user with a menu of options.
enter new movie info
view info of existing movie
update info of existing movie
display average/min/max/median score for a movie
output titles of all movies with a certain actor
output titles of all movies made in a certain year
Before exiting, the program must write the information about all the movies to the file.
Below is what I have managed to do so far. There is also a header file and another source code that show the prototypes for the function and execute the program, respectivley. I was given a very basic list of all the needed functions and their names. All of which should be here, with a few exceptions. At the moment, I am stuck and just kind of lost as to where to go from here.
Any help would be appreciated.
#include "std_lib_facilities.h"
#include "Movie.h"
Movie::Movie(){}
Movie::Movie(string a_title) : title(a_title) {};
void Movie::execute()
{
int option = 0;
display_intro();
while (option != 7 ){
option = display_menu();
switch (option)
{
case 1:
new_movie();
break;
case 2:
display_movie();
break;
case 3:
update_movie();
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
display_exit();
break;
default:
cerr << "Improper file name detected. ";
break;
} // end switch
} // end while
} // end execute
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::display_intro()
{
cout << "Welcome to my movie database. ";
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::display_exit(){
cout << "Thanks for using My Movie Database. "
<< "Please come back again. ";
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int Movie::display_menu(){
int choice = 0;
cout << "Please enter the file you wish to access. "
<< "Enter '1' to enter a new movie. "
<< "'2' to view all the information for an existing movie. "
<< "'3' to update the information of an existing movie. "
<< "'4' to display scoring information for a movie. "
<< "'5; to output the titles of all moves starrring a particular actor. "
<< "'6' to output the titles of all the movies made in a particular year. "
<< "And '7' to exit the database. "
<< ">> ";
cin >> choice;
return choice;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Movie Movie::new_movie()
{
string title;
int year = 0;
vector<string>actors;
vector<string>genre;
vector<double>scores;
int choice = 0;
cout << " "
<< "Welcome to menu option one. "
<< "Here you may enter a new movie into our database. "
<< "Please enter your Movie title here. "
<< ">>";
set_title(title);
cout << "Now please enter the release year of your movie. "
<< ">>";
set_year(year);
cout << "Next you may enter the names of at least three actors for your moive entry. "
<< "Type 'stop' to end your list. "
<< ">>";
set_actors(actors);
cout << "Here you will enter the genre of the moive. "
<< "Type 'stop' to end your list. "
<< ">>";
set_genre(genre);
cout << "Lastly, enter a list of scores that you give or others have given to the movie. "
<< "Keep in mind that these scores are on a 1 to 10 scale and non-whole numbers may be used. "
<< "Type '0' to end your list. "
<< ">>";
set_scores(scores);
cout << "Thank you for entering the new movie data. "
<< "This information will be stored and you will be returned to the main menu. ";
return 0;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int Movie::update_movie()
{
int update_info;
cout << " "
<< "Thank you for choosing menu option three. "
<< "Here you have the opertunity to update or change any information on "
<< "an existing movie. "
<< "Enter '1' to change a movie title. "
<< "'2' to change the release year. "
<< "'3' to change or add actor's names. "
<< "'4' to change the genre of the movie. "
<< "'5' to change or add scores. "
<< "Lastly, you may enter '6' to return to the main menu. ";
while ((cin >> update_info) && (update_info != 6))
switch (update_info)
{
case 1:
get_title();
edit_title();
break;
case 2:
get_year();
edit_year();
break;
case 3:
get_actors();
edit_actor();
break;
case 4:
get_genre();
edit_genre();
break;
case 5:
get_scores();
edit_score();
break;
case 6:
display_menu();
break;
default:
cerr << "Improper option chosen. ";
break;
}
return update_movie();
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::display_update_movie(int choice){}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Movie Movie::display_movie()
{
Movie show_movie;
return display_movie();
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::display_movie_options(int choice){}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::read_title(string caption){}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::read_year(int yr){}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::read_line(string line){}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
string Movie::edit_title()
{
string old_title;
string new_title;
char key;
title = old_title;
while (cin >> new_title)
old_title = new_title;
return edit_title();
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int Movie::edit_year()
{
int old_year = 0;
int new_year = 0;
char key;
year = old_year;
while (cin >> new_year)
old_year = new_year;
return edit_year();
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
string Movie::edit_actor()
{
string old_name;
string new_name;
char key;
string actor;
actor = old_name;
while (cin >> new_name)
old_name = new_name;
return edit_actor();
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
string Movie::edit_genre()
{
string old_genre;
string new_genre;
char key;
return edit_genre();
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
double Movie::edit_score()
{
double old_score;
double new_score;
char key;
return edit_score();
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::build_vec(vector<string> vec, string type){}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::read_score(double rate){}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::read_scores(vector<double> ratings){}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
string Movie::get_title(){
return title;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::set_title(string movie_title)
{
string new_title;
movie_title = new_title;
cin >> new_title;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int Movie::get_year(){
return year;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::set_year(int movie_year)
{
int new_year = 0;
movie_year = new_year;
cin >> new_year;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
vector<string> Movie::get_actors(){
return actors;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::set_actors(vector<string>movie_actors)
{
string actor;
bool found = false;
while ((cin >> actor) && (actor != "stop")) {
if (movie_actors.size() == 0) {
movie_actors.push_back(actor);
}
else {
for (int i = 0; i < movie_actors.size(); ++i)
if (actors[i] == actor) found = true;
if (found == false) {
movie_actors.push_back(actor);
}
else
cout << "Error, duplicate name entered. ";
found = false;
}
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
vector<string> Movie::get_genre(){
return genre;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::set_genre(vector<string>movie_genre){
string gen;
bool found = false;
while ((cin >> gen) && (gen != "stop")) {
if (movie_genre.size() == 0) {
movie_genre.push_back(gen);
}
else {
for (int i = 0; i < gen.size() - 1; ++i)
if (movie_genre[i] == gen) found = true;
if (found == false) {
movie_genre.push_back(gen);
}
else
cerr << "Error, duplicate genre entered. ";
found = false;
}
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
vector<double> Movie::get_scores(){
return scores;
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void Movie::set_scores(vector<double>movie_scores) {
double s = 0;
double sum = 0;
double max_s = 0;
double min_s = 0;
bool found = false;
while ((cin >> s) && (s != 0)) {
if (movie_scores.size() == 0) {
movie_scores.push_back(s);
}
else {
for (int i = 0; i < movie_scores.size() - 1; ++i)
if (movie_scores[i] == s) found = true;
if (found == false) {
movie_scores.push_back(s);
}
for (int x : movie_scores)
{
if (x > max_s) max_s = x;
if (x < min_s) min_s = x;
sum += x;
}
}
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Movie_stream::Movie_stream() {}
vector<Movie> get_db() {
return get_db();
}
void set_db(vector<Movie>db) {}
Movie get_movie(int ms_index, int kind, string key) {
return get_movie(ms_index, kind, key);
}
void set_movie(Movie add_my_movie) {}
void update_movies(Movie updated_movie, int ms_index) {}
void list_movies() {}
void display_titles() {}
void initilize_db(string db_file) {}
void terminate_db(string db_file) {}
Explanation / Answer
#include<stdio.h>
#include <stdlib.h>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<string>
using namespace std;
void mainmenu(void);
void addmovie(void);
void delmovie(void);
void editmovie(void);
void searchmovie(void);
void viewmovie(void);
void ratemovie(void);
int getdata();
void addreview(void);
FILE *fp,*ft;
//list of global variable
std::string password ("superman");
struct date
{
int mm,dd,yy;
};
struct movie
{
char cast[20];
char name[20];
char director[20];
int duration;
char rev[200];
float rate;
float rsum;
float ctr;
float ctr1;
struct date d;
};
struct movie a;
int main()
{
char ch,pass[25];
cout << "This system is Password Protected. " << "Enter Password:";
cin.getline(pass, 25);
#if 0
while(ch!= 13)
{
ch=getchar();
if(ch!=13 && ch!=9)
{
#if 0
putchar('*');
#endif
pass[i] = ch;
i++;
}
}
#endif
if (password.compare(pass) == 0)
{
cout<<"Password match";
// cout<<"Press any key to countinue.....";
mainmenu();
}
{
cout<<" Warning!! Incorrect Password. Calling main menu. ";
mainmenu();
}
return 0;
}
void mainmenu()
{
unsigned int choice;
cout<<" 1. Add Movie ";
cout<<"2. Delete Movie ";
cout<<"3. Search Movie ";
cout<<"4. Display Movie List ";
cout<<"5. Rate Movie ";
cout<<"6. Add Review ";
cout<<"7. Close Application ";
cout<<"Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
addmovie();
break;
case 2:
delmovie();
break;
case 3:
searchmovie();
break;
case 4:
viewmovie();
break;
case 5:
ratemovie();
break;
case 6:
addreview();
break;
case 7:
{
exit(0);
}
default:
{
cout<<" Wrong Entry!!Please re-enter correct option";
}
void addmovie(void)
{
char opt;
fp=fopen("imdb.dat","ab+");
if(getdata()==1)
{
fseek(fp,0,SEEK_END);
fwrite(&a,sizeof(a),1,fp);
fclose(fp);
cout<<"The record is sucessfully saved";
cout<<"Save any more?(Y / N):";
cin >> opt;
if ((opt =='n') || (opt == 'N'))
{
mainmenu();
}
else
{
addmovie();
}
}
else
{
mainmenu();
}
}
void delmovie()
{
char s[25];
int flag=0;
char another='y';
char opt;
while(another=='y')
{
cout<<"Enter the Movie to delete:";
cin.ignore();
cin.getline(s,25);
fp=fopen("imdb.dat","rb+");
// rewind(fp);
while(fread(&a,sizeof(a),1,fp)==1)
{
if(strcmp(a.name,s)==0)
{
cout<<"The movie record is available ";
cout<<"Movie name is"<<a.name;
flag=1;
}
}
if(flag==1 )
{
cout<<"Do you want to delete it?(Y/N):";
cin >> opt;
if ((opt == 'y') || (opt == 'Y'))
{
ft=fopen("test.dat","wb+"); //temporary file for delete
rewind(fp);
while(fread(&a,sizeof(a),1,fp)==1)
{
if((strcmp(s,a.name))!=0)
{
fseek(fp,0,SEEK_CUR);
fwrite(&a,sizeof(a),1,ft);
}
}
fclose(fp);
fclose(ft);
remove("imdb.dat");
rename("test.dat","imdb.dat");
fp=fopen("imdb.dat","rb+");
cout<<"The record is sucessfully deleted";
cout<<"Delete another record?(Y/N)";
if((another=getchar())=='y')
{
delmovie();
}
else
{
mainmenu();
}
}
else
{
mainmenu();
}
}
else
{
cout<<"Movie doesn't exist!!! ";
mainmenu();
}
} // While
}
void searchmovie()
{
char opt;
//struct movie a;
cout<<"*****************************Search Movie*********************************";
fp=fopen("imdb.dat","rb+");
rewind(fp); //move pointer at the begining of file
{
char s[25];
int flag=0;
cout<<"Enter Movie Name:";
cin.ignore();
cin.getline(s,25);
while(fread(&a,sizeof(a),1,fp)==1)
{
if(strcmp(s,a.name)==0) //checks whether a.name is equal to s or not
{
cout<<"Movie is present";
cout<<" Name:"<<a.name;
cout<<" Director:"<<a.director;
cout<<" Cast:"<<a.cast;
cout<<" Duration:"<<a.duration;
//cout<<" Review:"<<a.review;
//cout<<" Rating:"<<a.rate;
flag = 1;
}
}
if(flag == 0)
{
cout<<" No Record Found";
}
cout<<"Try another search?(Y/N)";
cin >> opt;
if ((opt == 'y') || (opt == 'Y'))
{
searchmovie();
}
else
{
mainmenu();
}
fclose(fp);
}
}
void viewmovie(void)
{
//struct movie a;
cout<<"*********************************Movie List*****************************";
fp=fopen("imdb.dat","rb");
while(fread(&a,sizeof(a),1,fp)==1)
{
cout<<" Name:"<<a.name;
cout<<" Director:"<<a.director;
cout<<" Cast:"<<a.cast;
cout<<" Duration:"<<a.duration;
cout<<" Date of Release:"<<a.d.dd<<"/"<<a.d.mm<<"/"<<a.d.yy;
if(a.ctr1 > 0)
{
cout<<" Review:"<<a.rev;
}
if(a.ctr > 0)
{
cout<<" Ratings:"<<a.rate;
cout<<" No_of_Ratings:"<<a.ctr;
cout<<" Sum of ratings:"<<a.rsum;
}
cout<<" ";
}
fclose(fp);
mainmenu();
}
int getdata()
{
cout<<"Movie Name:";
cin.ignore();
cin.getline(a.name, 20);
cout<<"Director:";
cin.getline(a.director, 20);
cout<<"Cast:";
cin.getline(a.cast, 50);
cout<<"Duration:";
cin>>a.duration;
cout<<"Enter date of release(dd/mm/yyyy):";
cin>>a.d.dd>>a.d.mm>>a.d.yy;
if((a.d.mm < 1 || a.d.mm > 12) || (a.d.dd < 1 || a.d.dd >31))
{
cout<<"Invalid date...........Try again!!! ";
cout<<"Enter date of release(dd/mm/yyyy):";
cin>>a.d.dd>>a.d.mm>>a.d.yy;
}
return 1;
}
void ratemovie()
{
char opt;
fp=fopen("imdb.dat","ab+");
rewind(fp); //move pointer at the begining of file
{
char s[25];
int ctr=1,flag=0;
float rsum=0,r,rate;
cout<<"Enter the name of movie you want to rate ";
cin.ignore();
cin.getline(s, 25);
while(fread(&a,sizeof(a),1,fp)==1)
{
if(strcmp(s,a.name)==0) //checks whether a.name is equal to s or not
{
cout<<"Movie is present"<<endl;
cout<<"Rate this movie:";
cin>>r;
rsum += r;
a.rsum = rsum;
a.rate = rsum/ctr;
a.ctr = ctr;
ctr++;
flag = 1;
//fp=fopen("imdb.dat","ab+");
fseek(fp,0,SEEK_END);
fwrite(&a,sizeof(a),1,fp);
//fclose(fp)
}
}
if(flag==0)
{
cout<<"Movie is not present! ";
}
cout<<"Try another movie to rate?(Y/N)";
cin >> opt;
if ((opt == 'y') || (opt == 'Y'))
{
ratemovie();
}
else
{
mainmenu();
}
fclose(fp);
}
}
void addreview()
{
fp=fopen("imdb.dat","ab+");
rewind(fp); //move pointer at the begining of file
{
char s[25],opt;
int ctr1=0,flag=0;
cout<<"Enter the name of movie you want to rate ";
cin.ignore();
cin.getline(s, 25);
while(fread(&a,sizeof(a),1,fp)==1)
{
if(strcmp(s,a.name)==0) //checks whether a.name is equal to s or not
{
cout<<"Movie is present"<<endl;
cout<<"Rate this movie:";
cout<<"Enter the review:";
cin.ignore();
cin.getline(a.rev, 200);
ctr1++;
a.ctr1 = ctr1;
flag = 1;
fseek(fp,0,SEEK_END);
fwrite(&a,sizeof(a),1,fp);
}
}
if(flag==0)
{
cout<<"Movie is not present! ";
}
cout<<"Try another movie to review?(Y/N)";
cin >> opt;
if ((opt == 'y') || (opt == 'Y'))
{
addreview();
}
else
{
mainmenu();
}
fclose(fp);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.