I am having troubles with my DVD collection assignment. I can get it to do most everything, but when I add any more than two DVDs, it wont save the extras. For example, I inputted four classes into my vector(list) with titles as "lalaland", "frosty", "adorable puppies", and "alpha". When I go to display the contents (menu item 1) it lists:
1. lalaland
2. frosty
3. lalaland
4. frosty
Is there a way to create a new class every time addDVD is called?
Also my selection sort does nothing. cant figure that one out either. Any recommendations would be very helpful.
#include "DVD.h"
int displayMenu();
void selectionSort(vector<DVD> &);
void addDVD(DVD&);
bool again = true; //see the menu again
char over; //would you like to run throught he case again?
bool OVER = false;
int selection, index, c = 0;
vector<DVD> list(0); //create the library starting with empty
DVD movie1;
void main()
{
cout << "Hello. Welcome to your DVD collection. Please enter a selection.\n";
do
{ //displays menu till user enters EXIT(5)
switch (displayMenu()) //switch statement pg 202
{
//DISPLAY ALL MOVIES IN THE LIST
case 1:
if (!list.empty()) //If there is anything in the list vector, it will show it
{
selectionSort(list); //make the vector alphabetical **not working**
cout << "Enter the movie number to view more details." << endl;
for (int i = 0; i < static_cast<int>(list.size()); i++)
{
cout << i + 1 << ". " << list[i].getTitle() << endl;
}
cin >> selection;
cout << endl;
while (selection <1 || selection > static_cast<int>(list.size()))
{
cout << "Invalid selection. Enter the number associated with the movie.\n";
cin >> selection;
}
movie1.displayContents(list[selection - 1]);
}
else
{
cout << "There are no movies in this collection.\n\n"; //If nothing is in the vector called list, it will tell the user that it is empty
}
break;
//ADD A MOVIE TO THE LIST
case 2:
do {
OVER = false;
++c; //counter with how many movies
DVD newMovie; //create a new object
addDVD(newMovie); //add the information into myMovie1
list.push_back(newMovie); //make a new spot for the object
cout << "Would you like to add another movie? (Y or N) ";
cin >> over;
while (toupper(over) != 'Y' && toupper(over) != 'N')
{
cout << "Please enter Y or N. ";
cin >> over;
}
if (toupper(over) == 'Y')
OVER = true;
} while (OVER == true);
break;
//DELETE A MOVIE FROM THE LIST
case 3:
do {
OVER = false;
selectionSort(list);
movie1.deleteDVD(list);
cout << "Would you like to delete another movie? (Y or N) ";
cin >> over;
while (toupper(over) != 'Y' && toupper(over) != 'N')
{
cout << "Please enter Y or N. ";
cin >> over;
}
if (toupper(over) == 'Y')
OVER = true;
} while (OVER == true);
break;
//UPDATE A MOVIE'S DETAILS
case 4:
do {
OVER = false;
movie1.updateDVD(list);
cout << "Would you like to update another movie's details? (Y or N) ";
cin >> over;
while (toupper(over) != 'Y' && toupper(over) != 'N')
{
cout << "Please enter Y or N. ";
cin >> over;
}
if (toupper(over) == 'Y')
OVER = true;
} while (OVER == true);
break;
//EXIT
case 5:
cout << "Goodbye.\n";
again = false;
break;
//ANYTHING ELSE. Should never output this since choice is validated in displayMenu()
default: cout << "ERROR: Invalid input.";
}
} while (again == true); //displays the menu again
system("pause");
}
int displayMenu()
{
int choice;
cout << "1. Display all movies\n2. Add a movie\n3. Delete a movie\n4. Update a movie's details\n5. Exit\n";
cin >> choice;
while (choice < 1 || choice >5) //validate choice is between 1 and 5
{
cout << "Enter a selection between 1 and 5. ";
cin >> choice;
}
return choice;
}
void selectionSort(vector<DVD> &list)
{
int startScan, minIndex;
string minValue;
int listLength = static_cast<int>(list.size());
for (startScan = 0; startScan < listLength-1; startScan++)
{
minIndex = startScan;
minValue = list[startScan].getTitle();
for (int index = startScan + 1; index < listLength; index++)
{
if (list[index].getTitle() < minValue)
{
minValue = list[index].getTitle();
minIndex = index;
}
}
list[minIndex] = list[startScan];
list[startScan].getTitle() = minValue;
}
}
void addDVD(DVD &myMovie)
{
string title, actor, character;
double length;
int year;
double min;
int hr, amt;
//GET TITLE
cout << "DVD Title: ";
cin.ignore();
getline(cin, title);
myMovie.setTitle(title);
//GET LENGTH
do
{
cout << "Length of the DVD(HH.MM): ";
cin >> length;
//validate the input
hr = static_cast<int>(length);
min = (length - hr);
length = min + hr;
if (length < 0 || (min * 100) >= 60)
cout << "Invalid input! ";
} while (length < 0 || (min * 100) >= 60);
myMovie.setLength(length);
//GET YEAR
cout << "Release year: ";
cin >> year;
//validate input
while (year > 2018 || year < 1890) //can not be after todays date or before movies were released (yr 1890)
{
cout << "Not a valid input. Try again: ";
cin >> year;
}
myMovie.setYear(year);
//GET ACTORS/CHARACTERS
cout << "Amount of actors/characters to input: ";
cin >> amt;
cin.ignore();
for (int i = 0; i < amt; i++)
{
cout << "Actor/ Actress number " << i + 1 << ": ";
getline(cin, actor);
myMovie.setActor(actor);
cout << "Plays what character: ";
getline(cin, character);
myMovie.Character.push_back(character);
}
cout << endl;
}