How to save classes into a vector

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.

Here is my code;
DVD.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>	//for input/output
#include <vector>	//for vec tor usage	pg429
#include <iomanip>	//to display the output in table format	 pg663
#include <string>
#include <cctype>	//toupper pg551
using namespace std;

class DVD
{

public:					//for all to see

	void setTitle(string t);
	void setLength(double l);
	void setYear(int y);
	void setActor(string a);
	void setCharacter(string c);
	string getTitle() const;
	double getLength() const;
	int getYear() const;
	string getActor() const;
	string getCharacter() const;
	string actor, character;
	vector<string> Actor;
	vector<string> Character;
	void deleteDVD(vector<DVD> list);
	void updateDVD(vector<DVD> list);
	void displayContents(DVD show);

	DVD();

private:						//internal
	string title;				//fits titles up to 100 characters
	double length;				//fits length of movie in hh.mm
	int year;

};



DVD.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "DVD.h"

DVD::DVD()
{
	length = 0.0;
	year = 0;
}

void DVD::displayContents(DVD show)
{
	cout << left<< setw(20) << "TITLE" 
		<< setw(7) << "LENGTH"
		<< setw(7) << "YEAR"
		<< setw(25) << "ACTOR/ACTRESS"
		<< setw(25) << "Character" << endl;
	cout << "----------------------------------------------------------------------------------\n";
	cout << left << setw(20) << show.getTitle() <<
		setw(7) << show.getLength() <<
		setw(7) << show.getYear();
		for (int i = 0; i < show.Actor.size(); i++)
		{
			if (i < 1)
				cout << left << setw(25) << show.Actor[i] << setw(25) << show.Character[i] << endl;
			else
				cout << left << setw(34) << " " << setw(25) << show.Actor[i] << setw(25) << show.Character[i] << endl;
		}
}


void DVD::deleteDVD(vector<DVD> list)
{
	//display list, make selection, delete
}

void DVD::updateDVD(vector<DVD> list)
{
	//display list, make selection, modify
}

void DVD::setTitle(string t)		{title = t; }
void DVD::setLength(double l)		{length = l; }
void DVD::setYear(int y)		{year = y; }
void DVD::setActor(string a)		{ actor = a; Actor.push_back(actor); }					//adds actor name to the last spor in vector Actor
void DVD::setCharacter(string c)	{ character = c; Character.push_back(character); }			//adds character name to the last spot in vector Character
string DVD::getTitle() const		{return title;}
double DVD::getLength() const		{return length; }
int DVD::getYear() const		{return year; }




MAIN.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#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;
}

Topic archived. No new replies allowed.