DVD Collection

C++
/*
This program will allow the user to keep track of a CD or DVD collection. This can only work exclusively with either CDs or DVDs since some of the data is different—your choice. Each CD/DVD in the collection will be represented as a class, so you will have one class that is a single CD/DVD.

The DVD class will have data members for the title of the movie, the length of the movie, and the year the movie was released. The class will have a vector which is used to store the name of the actors and actresses in the movie. Another vector will be used to maintain the character names that the actor/actress played in the movie. These two vectors must work in parallel, meaning the first actor/actress in the list must correspond to the first character in the other vector.

The program will maintain a list of CD/DVDs. This list will be a vector of that class type (CD or DVD). The program must provide methods (functions) to add a CD/DVD, remove a CD/DVD and update a CD/DVD. There should also be a function that displays the entire list of CDs/DVDs. The output must be a table format, with heading.

For the DVDs

Movie Title Length of Movie Year Released Actors/Actresses Characters

NOTE: the movie title, length of movie and year released should only appear once while the actors/actresses and characters will have several lines. So the other columns must be displayed with blanks.

Thanks for the help. I changed it up a bit but realized that i'm not storing the DVDs in a file but rather in the class. I'm supposed to use one class for one DVD. The problems I'm having is that I can't get getActor to return name and charName in parallel so I can display them. This is not all the code. I left some out that I'm not working on yet.


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
// Course Project

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

// Function Prototype
void showList(string, double, int, string, string);
void showMenu();

class DVD
{
private:
	string movie;				// To hold movie title
	double length;				// To hold movie length
	int year;					// To hold release date

public:

	// Constructors

	vector<string> actor;		// To hold actor/actress name
	vector<string> charName;	// To hold name of character
	void setMovie(string);
	void setLength(double);
	void setYear(int);
	void addActor(string, string);
	string getMovie() const;
	double getLength() const;
	int getYear()const;
	string getActor() const;
	


	DVD()
	{
	movie = "";
	length = 0;
	year = 0; }
	 
	DVD(string m, double l, int y)
	{ movie = m; 
	  length = l;
	  year = y; }
};

	// Mutators
	void DVD::setMovie(string m)
	{ movie = m; }
	void DVD::setLength(double l)
	{ length = l; }
	void DVD:: setYear(int y)
	{ year = y; }
	
	void DVD::addActor(string actorName, string characterName)
	{
		actor.push_back(actorName);
		charName.push_back(characterName);
	}
	// Accessors
	string DVD::getMovie() const
	{ return movie; }
	double DVD::getLength() const
	{ return length; }
	int DVD::getYear() const
	{ return year; }
	// I am having trouble here. How do I return both
	// actor and charName in parallel??
	//
	string DVD::getActor(unsigned int index)
	{
		if(index >= name.size()) //Error name is undefined
			return "no data";
		return name[index];
	}//Error: declaration incompatible with
	//std::string DVD::getActor() const declared at line 34

	

	


int main()
{
	int quantity;			// To hold number actors/actresses
	string name;			// To hold actor/actress name
	string charName;			// To hold name of character
	string title;			// To hold movie title
	double movieLength;		// Local variable for length
	int yearReleased;			// Local variable for release date
	

	fstream nameFile;	// Fstream file object
	int choice;			// For menu choice

	// Constants for menu choices
	const int SHOW_LIST = 1,
			  ADD_DVD = 2,
			  REMOVE_DVD = 3,
			  UPDATE_DVD = 4,
			  QUIT_CHOICE = 5;
			 
	// Access Vector
	DVD mydvd;
	
	//I want to ask the user to enter the details
	// of the DVD so the info will be stored in the variables
	
	// Ask the user to enter the details of DVD
	cout << "Enter the movie title, length, and year released.\n"
		<< "Hit [ENTER] after each input: ";
	getline(cin, title);
	cin >> movieLength;
	cin >> yearReleased;

	//Here I would like to ask the user "How many actors
	// Do you wish to enter...
	cout << "How many actors? ";
	cin >> quantity;

	// Here is one of my problems. I only want index to go no higher
         // than quantity. But then I can't use getline(cin,name[index])
         // because name is not name[quantity]. Any suggestions?

	for (int index = 0; index < quantity; index++)	
	{
		cout << "Enter actor #" << (index+1) << ": ";
		getline(cin, name); //Is this right or should it be
		// getline(cin, name[index]);
		cin.ignore();
		cout << "Enter the name of the character portrayed "
			<< "by actor #" << (index+1) << ": ";
		getline(cin, charName); //Same here?
		cin.ignore();
	}
	cout << endl;
	// Store the DVD details 
	mydvd.setMovie(title);
	mydvd.setLength(movieLength);
	mydvd.setYear(yearReleased);
         // I want to store the name of the actors and their titles
         // in parallel but I'm still having this problem
	mydvd.addActor(name, charName); // Is this right? 
Last edited on
How do I store input from the user in vector name and charName from function main?

If you insist on having two separate vectors for actors and characters, add the similar member function:
1
2
3
4
5
6
7
8
/**
 * Adds an actor name and his character name to the DVD object
 */
void DVD::addActor(string actorName, string characterName)
{
    name.push_back(actorName); // push_back method adds next item to a vector
    charName .push_back(characterName);
}


EDIT:
You also need a getter to read the data correctly from main:
1
2
3
4
5
6
string DVD::getActor(unsigned int index)
{
	if(index >= name.size())
		return "no data";
	return name[index];
}

Add one for charName as well.
Then in main you do just this:
1
2
3
4
5
6
7
int main(){
    DVD mydvd;
    mydvd.setMovie("The Godfather");
    mydvd.addActor("Marlon B", "The mumbling guy");
    mydvd.addActor("Al Pacino", "The crybaby");
    cout << mydvd.getActor(0); // outputs "Marlon B" to the console
}

Last edited on
Sorry for not using code tags.. You could have yelled at me.. Thanks for the help. I've changed it up a bit realizing that I don't need to initialize the array with names. I need the user to input the information of the DVD ...i.e.. Movie Title, length, year, actors, character names, and then store these in the class. I'm supposed to use one class for one DVD. Right now i'm trying to figure out how to get getActor to return the input from the user so it can be stored in addActor?
By convention, class functions with names starting from 'get', like getActor(), are supposed to get data stored in class object and return it for processing outside of this object. If I understand what you have written, you wish your getActor() to do just the opposite - take value from outside the class and put it inside. If that's the case, I suggest thinking of better name. Look how you may go about asking user for input:
A sketch of such function may look like this:
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
// This is another member class. Make sure it is public
void DVD ::readInfo() {
    cout << "Movie name: ";
    getline(cin, title);
    cout << "Year: ";
    cin >> year;
    bool moreActors = true;
    while(moreActors) {
        cout << "Add new actor [Y/N]?";
        char c;
        cin >> c;
        if(c == 'y' || c == 'Y')	 {
            string actor, character;
            cout << "Enter actor name: ";
            getline(cin, actor); // get user input
            cout << "Enter character name: ";
            getline(cin, character); // get user input
            // use existing function to add vector entries
            addActor(actor, character); 
            continue;
        }
        if( c == 'n' || c == 'N' ) {
            moreActors = false;
        }
    }
};

1
2
3
4
5
6
7
8
int main(){
    DVD mydvd;	// Create empty dvd object
    mydvd.readInfo();// Ask user for data
	
    // get name of first stored actor 
    // and put it on the screen
    cout << mydvd.getActor(0);
}

Last edited on
By convention, class functions with names starting from 'get', like getActor(), are supposed to get data stored in class object and return it for processing outside of this object



I honestly hate the convention of using Get and Set in your variable names. It's ugly as sin.

This convention is much nicer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Person
{
public:
    void Name(const string& name)  { _name = name; }
    string Name( ) const { return _name; }

private:
      string _name;
};


Person p;

p.Name("Bob");
cout << p.Name() << endl;
Last edited on
@IceThatJaw
Well, I find using capitalized function names ugly. To each his own, I suppose ;P
Can you post the full working code please Lilspree
Topic archived. No new replies allowed.