You are to write a program which reads an ASCII text file named \"movies.txt\" w
ID: 3808933 • Letter: Y
Question
You are to write a program which reads an ASCII text file named "movies.txt" with movie information, and then allows the user to query various information about the movie(s). File format input: The file will consist of one or more pairs of lines in the following format: Theatre number (int), movie name (string), movie rating (string), movie length in minutes (int) Start time 1, start time 2, start time 3, start time N Each start times will be a string in 24 hour time format (i.e 8:00 or 21:30 for 8am or 9:30pm). Start times are separated by commas. It is up to you whether you want to read the start times as strings or Date Time, and whether to convert them to DateTime. Theatre number may not begin with 1, may not be contiguous, and may not be in any order. There may be any number of theatres, and any number of start times (a list might be a good data structure to consider). Once the file is read, the user must be able to enter any of the following commands and have the program perform the required action: A- Print a list of all movies and corresponding theatre numbers M Print information about a specific movie. Prompt user to enter theatre number, and then print all information about that movie (theatre number, title. and rating) T- Print time information about a specific movie. Prompt user to enter theatre number, and then print the movie name and the start and end times for each showing F- Find all movies that begin within a specified time frame. The program should prompt the user for a time and the number of minutes. Then print a list of all movies that begin within the specified number of minutes from the specified time (either before or after). Be sure to consider/test cases when a movie begins at 23:00 and lasts for 120 minutes (a day boundary is crossed). X- Exits the program. You must create and use a Movie class to implement this program. The movie class shall have a List within it to store the start times of the movie. Your main program should have a List or type Movie. Which shall store all of the movie information.Explanation / Answer
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include <string>
using namespace std;
//Class Movie definition
class Movie
{
//Data member
int theaterNumber;
string movieName;
string movieRating;
int movieLength;
string startTime;
public:
//Member function
int readFile();
void displayAllMovie();
void displayMovieTheaterNo();
void displayMovieStartingTime();
void displayMovieSpecificTime();
}myMovie[100];
int len = 0;
//Display movie based on specific time
void Movie::displayMovieSpecificTime()
{
string st;
int movieLen;
//Accept data from user
cout<<" Enter Start time: ";
cin>>st;
cout<<" Enter number of minutes: ";
cin>>movieLen;
//Moves till end of record
for(int x = 0; x < len; x++)
//Checks if the starting time of movie is equal to time entered by the user
//and movie length is equal to the length entered by the user then display movie information
if(myMovie[x].startTime == st && myMovie[x].movieLength == movieLen)
cout<<" Theater Number: "<<myMovie[x].theaterNumber<<" Movie Name: "<<myMovie[x].movieName<<" Movie Rating: "
<<myMovie[x].movieRating<<" Movie Length: "<<myMovie[x].movieLength<<" Start Time: "<<myMovie[x].startTime;
}//End of function
//To calculate end time
string endTime(string st, int l)
{
int hour = 0, min = 0;
string end = "";
for(int x = 0; x < st.length(); x++)
{
if(isdigit( st[ x ] ))
{
if(x < 2)
hour = (hour *10 +st[x]-48);
else
min = (min *10 +st[x]-48);
}
}
min += l;
int h = min / 60;
int m = min % 60;
hour = hour + h;
min = m;
char ho[10];
char mi[10];
if(hour >= 24)
hour = 0;
cout<<" Hour: "<<hour<<" Minute: "<<min;
if(hour > 12)
end = end + itoa (hour,ho,10) + ":" + itoa (min,mi,10) + "pm";
else
end = end + itoa (hour,ho,10) + ":" + itoa (min,mi,10) + "am";
return end;
}
void Movie::displayMovieStartingTime()
{
printf(" Enter theater number: ");
int no;
cin>>no;
for(int x = 0; x < len; x++)
if(myMovie[x].theaterNumber == no)
{
cout<<" Theater Number: "<<myMovie[x].theaterNumber<<" Movie Name: "<<myMovie[x].movieName<<" Movie Rating: "
<<myMovie[x].movieRating<<" Movie Length: "<<myMovie[x].movieLength<<" Start Time: "<<myMovie[x].startTime
<<" Ending Time: "<<endTime(myMovie[x].startTime, myMovie[x].movieLength);
}
}//End of function
void Movie::displayMovieTheaterNo()
{
printf(" Enter theater number: ");
int no;
cin>>no;
for(int x = 0; x < len; x++)
if(myMovie[x].theaterNumber == no)
cout<<" Theater Number: "<<myMovie[x].theaterNumber<<" Movie Name: "<<myMovie[x].movieName<<" Movie Rating: "
<<myMovie[x].movieRating<<" Movie Length: "<<myMovie[x].movieLength<<" Start Time: "<<myMovie[x].startTime;
}//End of function
void menu()
{
cout<<" A - Print a list of all movies and corresponding theater numbers.";
cout<<" M - Print information about specific movie.";
cout<<" T - Print time information about specific movie";
cout<<" F - Find all movies that begins with a specified time frame.";
cout<<" X - Exit Program.";
}//End of function
void Movie::displayAllMovie()
{
for(int x = 0; x < len; x++)
{
cout<<" Theater Number: "<<myMovie[x].theaterNumber<<" Movie Name: "<<myMovie[x].movieName<<" Movie Rating: "
<<myMovie[x].movieRating<<" Movie Length: "<<myMovie[x].movieLength<<" Start Time: "<<myMovie[x].startTime;
}//End of for
}//End of function
//Read data from file and stores it in class data member
int Movie::readFile()
{
//Creates an object of ifstream
ifstream readf;
//Opens the file for reading
readf.open ("Movie.txt");
//Loops till end of file
while(!readf.eof())
{
//Reads data and stores in respective array
readf>>myMovie[len].theaterNumber;
readf>>myMovie[len].movieName;
readf>>myMovie[len].movieRating;
readf>>myMovie[len].movieLength;
readf>>myMovie[len].startTime;
//Increase the record counter
len++;
}//End of while
//Close file
readf.close();
}//End of function
//Main function
int main()
{
myMovie[0].readFile();
char ch;
do
{
menu();
cout<<" Enter your choice: ";
cin>>ch;
switch(ch)
{
case 'A': case 'a':
myMovie[0].displayAllMovie();
break;
case 'M': case 'm':
myMovie[0].displayMovieTheaterNo();
break;
case 'T': case 't':
myMovie[0].displayMovieStartingTime();
break;
case 'F': case 'f':
myMovie[0].displayMovieSpecificTime();
break;
case 'X': case 'x':
exit(0);
default:
cout<<" Invalid choice!";
}//End of switch
}while(1);
}
Output:
A - Print a list of all movies and corresponding theater numbers.
M - Print information about specific movie.
T - Print time information about specific movie
F - Find all movies that begins with a specified time frame.
X - Exit Program.
Enter your choice: a
Theater Number: 1
Movie Name: Rang
Movie Rating: 12
Movie Length: 120
Start Time: 8:00am
Theater Number: 2
Movie Name: Dil
Movie Rating: 5
Movie Length: 180
Start Time: 10:20am
Theater Number: 3
Movie Name: Danger
Movie Rating: 5
Movie Length: 150
Start Time: 21:00pm
Theater Number: 4
Movie Name: Fire
Movie Rating: 7
Movie Length: 80
Start Time: 17:00pm
A - Print a list of all movies and corresponding theater numbers.
M - Print information about specific movie.
T - Print time information about specific movie
F - Find all movies that begins with a specified time frame.
X - Exit Program.
Enter your choice: t
Enter theater number: 2
Hour: 13 Minute: 20
Theater Number: 2
Movie Name: Dil
Movie Rating: 5
Movie Length: 180
Start Time: 10:20am
Ending Time: 13:20pm
A - Print a list of all movies and corresponding theater numbers.
M - Print information about specific movie.
T - Print time information about specific movie
F - Find all movies that begins with a specified time frame.
X - Exit Program.
Enter your choice: m
Enter theater number: 2
Theater Number: 2
Movie Name: Dil
Movie Rating: 5
Movie Length: 180
Start Time: 10:20am
A - Print a list of all movies and corresponding theater numbers.
M - Print information about specific movie.
T - Print time information about specific movie
F - Find all movies that begins with a specified time frame.
X - Exit Program.
Enter your choice: f
Enter Start time: 10:20am
Enter number of minutes: 180
Theater Number: 2
Movie Name: Dil
Movie Rating: 5
Movie Length: 180
Start Time: 10:20am
A - Print a list of all movies and corresponding theater numbers.
M - Print information about specific movie.
T - Print time information about specific movie
F - Find all movies that begins with a specified time frame.
X - Exit Program.
Enter your choice: x
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.