3 Replies - 627 Views - Last Post: 11 May 2013 - 10:00 PM Rate Topic: -----

#1 Lilspree  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-May 13

DVD Collection using classes (returning parallel vectors)

Posted 11 May 2013 - 11:05 AM

I'm suppose to use a class to store DVD information such as Movie Title, length, year, actors, actors' characters
example,

Catwoman, 120:34, 2004, Halle Berry, Catwoman
------------------------------Benjamin Bratt, Tom Lone
------------------------------Alex Borstein, Sally

The actors and actors' character need to run in parallel.

lines 68-77 I'm trying to return the parallel vectors but I get the error shown on 73-77.
lines 126 I believe I need to use getline(cin,name[index]) but I can't without quantity being initialized to a constant
lines 119 I want the user to enter how many characters so that index will only go that far.
lines 139 I'm having trouble storing users input in addActor

Any suggestions?


#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 32

	

	


int main()
{
	int quantity;				// To hold number of 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
	
	// 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;

	// How can I make this work with addActor 
	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);
	mydvd.addActor(name, charName); // Is this right?


Is This A Good Question/Topic? 0
  • +

Replies To: DVD Collection using classes (returning parallel vectors)

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: DVD Collection using classes (returning parallel vectors)

Posted 11 May 2013 - 12:48 PM

Regarding your error, does this definition

string getActor() const;


match this implementation?

string DVD::getActor(unsigned int index)

Was This Post Helpful? 1
  • +
  • -

#3 Lilspree  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-May 13

Re: DVD Collection using classes (returning parallel vectors)

Posted 11 May 2013 - 01:30 PM

View PostJackOfAllTrades, on 11 May 2013 - 12:48 PM, said:

Regarding your error, does this definition

string getActor() const;


match this implementation?

string DVD::getActor(unsigned int index)

Makes since thanks. I changed it to

string getActor(int);

string DVD getActor::(int index)
{
   if(index >= name.size()) //Error name is undefined
	return "no data";
	return name[index];
}

but why is name undefined if it's in vector? Sorry.. Extremely new
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,165
  • Joined: 05-May 12

Re: DVD Collection using classes (returning parallel vectors)

Posted 11 May 2013 - 10:00 PM

Where is name inside the vector? Show us which line?

There are a vector of strings called charName and actor, but the compiler isn't going to read your mind and infer that name actually refers to the vector of strings actor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1