Question: C++ Assignment I need to make sure I have a two dimenstional array in the code which appears below. The other problem is that when
C++ Assignment I need to make sure I have a two dimenstional array in the code which appears below. The other problem is that when I enter a movie title that does not exist in the database the error message "This title cannot be found. It appears that you do not have this in your selection." only shows up when there is no data in the program. Once there are items in the program, the error message does not appear. Here is my code.
#include
#include
using namespace std;
int index = 0; //Variable to show how many Media Items are entered.
struct Media { // Media struct
string title;
string starring;
string format;
string genre;
string yearProduced;
string comments;
};
int displayMenu();
Media getMedia();
void showMedia(Media);
void allMedia(Media[]);
void sortMedia(Media[]);
void findMedia(Media[], int);
int main()
{
Media med[1000]; // Declare array of Media struct
while (true)
{
int choice = displayMenu();
switch (choice)
{
case 1:
med[index] = getMedia();
index++;
break;
case 2:
allMedia(med);
break;
case 3:
sortMedia(med);
break;
case 4:
findMedia(med, index);
break;
case 5:
cout << "You have selected to exit the program. Thank you." << endl;
return 0;
break;
default:
cout << "That is not a valid choice. Please try again." << endl;
}
cout << endl;
}
system("pause");
return 0;
}
int displayMenu() //Display menu to user
{
cout << "The Greely Family Media Library" << endl;
cout << endl;
cout << "Please select from the below menu." << endl;
cout << endl;
cout << "1. Enter New Media Item." << endl;
cout << "2. Display all Media Items." << endl;
cout << "3. Sort all Media Items." << endl;
cout << "4. Display a particular Media Item." << endl;
cout << "5. Exit the program." << endl;
cout << endl;
int choice; //User selects what they want to do.
cout << "Enter choice: ";
cin >> choice;
cin.ignore();
cout << endl;
return choice;
}
Media getMedia()
{
Media a;
cout << "Enter Title: ";
getline(cin, a.title);
cout << endl;
cout << "If Media Item is a music CD, enter the singer or group. If a PC Game, enter N/A. " << endl;
cout << "Enter Starring: ";
getline(cin, a.starring);
cout << endl;
cout << "Format can be DVD, VHS, CD, Blue-Ray or PC Game." << endl;
cout << "Enter format: ";
getline(cin, a.format);
cout << endl;
cout << "Genre can be Main theme of item followed by TV Show, Movie, Music or PC Game." << endl ;
cout << "Example of Genre: Childen's Comedy TV Show " << endl;
cout << "Enter genre: ";
getline(cin, a.genre);
cout << "Enter Year Produced:";
getline(cin, a.yearProduced);
cout << endl;
cout << "Comments are exactly that. A way to refresh your memory what the Media Item is about." << endl;
cout << "Example, Daktari was a show that I watched on Sundays when growing up. " << endl;
cout << endl;
cout << "Enter Comments: ";
getline(cin, a.comments);
return a;
}
void showMedia(Media c)
{ //Display Media Item Details
cout << "Title: " << c.title << endl;
cout << "Starring: " << c.starring << endl;
cout << "Format: " << c.format << endl;
cout << "Genre: " << c.genre << endl;
cout << "Year Produced: " << c.yearProduced << endl;
cout << "Comments: " << c.comments << endl;
}
void allMedia(Media med[])
{
for (int i = 0; i < index; i++)
{
showMedia(med[i]);
cout << endl;
}
}
void sortMedia(Media *med) //Sort by format and then title
{
for (int i = 0; i < index; i++)
{
for (int j = 0; j < index; j++)
{
if (med[i].format == med[j].format) //Checking to see if formats are equal
{
if (med[i].title < med[j].title) //Formats are equal then compare titles
{
Media t = med[i]; //Swapping addresses of elements
med[i] = med[j];
med[j] = t;
}
}
else if (med[i].format < med[j].format) //Checking with formats
{
Media t = med[i];
med[i] = med[j];
med[j] = t;
}
}
}
cout << "After sorting by Format and Title. " << endl; //Display Lists
allMedia(med);
}
void findMedia(Media med[], int size) //Ask User to enter a title.
{
string title;
cout << "Enter Title: ";
getline(cin, title);
for (int i = 0; i < size; i++)
{
if(med[i].title!= "")
{
showMedia(med[i]);
return ;
}
}
cout << "This title cannot be found. It appears that you do not have this in your selection." << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
