/* Name: Patrick Potts Class: CSCI 1411-001 Description: Movie Listing Lab Partn
ID: 3738792 • Letter: #
Question
/*
Name: Patrick Potts
Class: CSCI 1411-001
Description: Movie Listing
Lab Partner: Gwen
Status: Sucessfully compiled and run on csegrid
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
//Delcaring structure
struct MovieData
{
/*Declaration the variables title as type of string*/
string title;
/*Declaration of releaseYear,runningTime,rating as type of intergers*/
int releaseYear;
int runningTime;
int rating;
};
/*Declaration of readDataFromFile method*/
int readDataFromFile(struct MovieData content[]);
/*Declaration of ListMovies method*/
void ListMovies(struct MovieData content[],int count);
/*Declaration of addMovies method*/
void addMovies(struct MovieData content[],int& count);
int main()
{
int count = 0;
const int MAXSIZE = 100;
int choice;
struct MovieData content[MAXSIZE];
while (true)
{
cout << " 1. Read in Movies" << endl;
cout << "2. Add Movies" << endl;
cout << "3. List Movies" << endl;
cout << "4. Exit" << endl;
cout << "Enter choice :";
cin >> choice;
switch (choice)
{
case 1:
{
count = readDataFromFile(content);
continue;
}
case 2:
{
if (count < MAXSIZE)
addMovies(content, count);
else
cout << "Structure is Full of contents" << endl;
continue;
}
case 3:
{
ListMovies(content, count);
continue;
}
case 4:
{
break;
}
}
break;
}
return 0;
}
/*Implementataion of List movies it will display the movies*/
void ListMovies(struct MovieData content[], int count)
{
cout << ":: Displaying the data ::" << endl;
for (int i = 0; i < count; i++)
{
cout << " Movie Data#" << i + 1 << ":" << endl;
cout << "Title :" << content[i].title << endl;
cout << "Released Year :" << content[i].releaseYear << endl;
cout << "Running time :" << content[i].runningTime << endl;
cout << "Rating :" << content[i].rating << endl;
}
}
/*Implementation of readDataFromFile method which reads data from file*/
int readDataFromFile(struct MovieData content[])
{
/*Declaration of inFile as type of ifstream*/
ifstream inFile;
int count = 0;
string title;
int year;
int runningTime;
int rating;
inFile.open("movieData.txt");
if (inFile.fail())
{
// Displaying statment
cout << "input file did not open please check it ";
}
else
{
while (getline(inFile, title))
{
content[count].title = title;
inFile >> year >> runningTime >> rating;
content[count].releaseYear = year;
content[count].runningTime = runningTime;
content[count].rating = rating;
inFile.ignore();
count++;
}
inFile.close();
}
return count;
}
/*Implementation of addMovies method which adds the movies to the file*/
void addMovies(struct MovieData content[], int& count)
{
string title;
int year;
int runningTime;
int rating;
char choice;
FILE *contentWrite;
contentWrite = fopen("movieData.txt","a");
while (true)
{
cin.ignore();
cout << "Enter Title name:";
getline(cin, title);
cout << "Enter Release Year :";
cin >> year;
cout << "Enter Running time :";
cin >> runningTime;
cout << "Enter Rating :";
cin >> rating;
cin.ignore();
content[count].title = title;
content[count].releaseYear = year;
content[count].runningTime = runningTime;
content[count].rating = rating;
fwrite(&content, sizeof(content), 1, contentWrite);
count++;
// Get the input from the user
cout << " Do you want to add another Movie data (Y/N):";
cin >> choice;
if (choice == 'y' || choice == 'Y')
{
continue;
}
else
{
break;
}
}
}
I need to add and provide a Search and Sort Function to this already existing c++ code. You should be able to Search or Sort by Title.
Explanation / Answer
Explanation:
Below is the C++ code for above problem with proper description provided within comments itself. Below code some sample output screenshots are attached.
If you got any error then please provide me your input file, this should be fine
If you need any other help for this Comment me below. Thanks
C++ code :
/*
Name: Patrick Potts
Class: CSCI 1411-001
Description: Movie Listing
Lab Partner: Gwen
Status: Sucessfully compiled and run on csegrid
*/
#include <iostream>
#include<string.h>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
//Delcaring structure
struct MovieData
{
/*Declaration the variables title as type of string*/
string title;
/*Declaration of releaseYear,runningTime,rating as type of intergers*/
int releaseYear;
int runningTime;
int rating;
};
/*Declaration of readDataFromFile method*/
int readDataFromFile(struct MovieData content[]);
/*Declaration of ListMovies method*/
void ListMovies(struct MovieData content[],int count);
/*Declaration of ListMovies method*/
void SortMovies(struct MovieData content[],int count);
/*Declaration of ListMovies method*/
int SearchMovies(struct MovieData content[],int count,string searchTitle);
/*Declaration of addMovies method*/
void addMovies(struct MovieData content[],int& count);
int main()
{
int count = 0;
const int MAXSIZE = 100;
string title;
int choice;
struct MovieData content[MAXSIZE];
while (true)
{
cout << " 1. Read in Movies" << endl;
cout << "2. Add Movies" << endl;
cout << "3. List Movies" << endl;
cout << "4. Sort Movies" << endl;
cout << "5. Search Movies" << endl;
cout << "6. Exit" << endl;
cout << "Enter choice :";
cin >> choice;
switch (choice)
{
case 1:
{
count = readDataFromFile(content);
continue;
}
case 2:
{
if (count < MAXSIZE)
addMovies(content, count);
else
cout << "Structure is Full of contents" << endl;
continue;
}
case 3:
{
ListMovies(content, count);
continue;
}
case 4:
{
SortMovies(content, count);
continue;
}
case 5:
{
cout<<" Enter Movie tile : ";
cin>>title;
if(SearchMovies(content, count, title)==-1)
cout<<" Movie title not found. ";
else
cout<<" Movie title found at index : "<<SearchMovies(content, count, title)<<endl;
continue;
}
case 6:
{
break;
}
}
break;
}
return 0;
}
/*Implementataion of List movies it will display the movies*/
void ListMovies(struct MovieData content[], int count)
{
cout << ":: Displaying the data ::" << endl;
for (int i = 0; i < count; i++)
{
cout << " Movie Data#" << i + 1 << ":" << endl;
cout << "Title :" << content[i].title << endl;
cout << "Released Year :" << content[i].releaseYear << endl;
cout << "Running time :" << content[i].runningTime << endl;
cout << "Rating :" << content[i].rating << endl;
}
}
/*Declaration of ListMovies method*/
void SortMovies(struct MovieData content[],int count) {
string t;
for(int i=1; i<count; i++)
{
for(int j=1; j<count; j++)
{
if(strcmp(content[j-1].title, content[j].title)>0)
{
strcpy(t, content[j-1].title);
strcpy(content[j-1].title, content[j].title);
strcpy(content[j].title, t);
}
}
}
}
/*Declaration of ListMovies method*/
int SearchMovies(struct MovieData content[],int count,string searchTitle) {
for (int i = 0; i < count; i++)
{
if(content[i].title == searchTitle) {
return i;
}
}
return -1;
}
/*Implementation of readDataFromFile method which reads data from file*/
int readDataFromFile(struct MovieData content[])
{
/*Declaration of inFile as type of ifstream*/
ifstream inFile;
int count = 0;
string title;
int year;
int runningTime;
int rating;
inFile.open("movieData.txt");
if (inFile.fail())
{
// Displaying statment
cout << "input file did not open please check it ";
}
else
{
while (getline(inFile, title))
{
content[count].title = title;
inFile >> year >> runningTime >> rating;
content[count].releaseYear = year;
content[count].runningTime = runningTime;
content[count].rating = rating;
inFile.ignore();
count++;
}
inFile.close();
}
return count;
}
/*Implementation of addMovies method which adds the movies to the file*/
void addMovies(struct MovieData content[], int& count)
{
string title;
int year;
int runningTime;
int rating;
char choice;
FILE *contentWrite;
contentWrite = fopen("movieData.txt","a");
while (true)
{
cin.ignore();
cout << "Enter Title name:";
getline(cin, title);
cout << "Enter Release Year :";
cin >> year;
cout << "Enter Running time :";
cin >> runningTime;
cout << "Enter Rating :";
cin >> rating;
cin.ignore();
content[count].title = title;
content[count].releaseYear = year;
content[count].runningTime = runningTime;
content[count].rating = rating;
fwrite(&content, sizeof(content), 1, contentWrite);
count++;
// Get the input from the user
cout << " Do you want to add another Movie data (Y/N):";
cin >> choice;
if (choice == 'y' || choice == 'Y')
{
continue;
}
else
{
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.