Stuck again. My program now runs but the title, year, and length is being overwr
ID: 3798271 • Letter: S
Question
Stuck again. My program now runs but the title, year, and length is being overwritten when a new movie is added. The actors and characters seem to be working okay but no data is saved when program is closed than reopened.
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.
Header file
#ifndef DVD_COLLECTION_H
#define DVD_COLLECTION_H
#include <string>
#include <vector>
using namespace std;
class DVDCollection
{
private:
string title; //Holds title of dvd
int year; //Year made
int length; //Holds length of movie
public:
DVDCollection(); // Default Constructor
DVDCollection(string, int, int); // Constructor
vector<string> Actor; //Actors and Actresses
vector<string> CharName; //Character names
// Mutator functions
void setTitle(string);
void setLength(int);
void setYear(int);
void setCharInfo(string, string);
// Accessor functions
string getTitle() const;
int getLength() const;
int getYear() const;
string getCharInfo() const;
};
#endif
DVDCollection. Cpp
#include <iostream>
#include "DVDCollection.h"
#include <vector>
#include <string>
DVDCollection::DVDCollection()
{
title = "";
year = 0;
length = 0;
}
DVDCollection::DVDCollection(string t, int y, int l)
{
title = t;
year = y;
length = l;
}
void DVDCollection::setTitle(string t)
{
title = t;
}
void DVDCollection::setLength(int l)
{
length = l;
}
void DVDCollection::setYear(int y)
{
year = y;
}
void DVDCollection::setCharInfo(string aName, string cName)
{
Actor.push_back(aName);
CharName.push_back(cName);
}
string DVDCollection::getTitle() const
{
return title;
}
int DVDCollection::getYear() const
{
return year;
}
int DVDCollection::getLength() const
{
return length;
}
string DVDCollection::getCharInfo() const
{
string temp;
for (auto iter = CharName.begin(); iter != CharName.end(); iter++)
{
temp.append(*iter);
temp.append(" ");
}
return temp;
}
Main.cpp
#include "DVDCollection.h"
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <map>
using namespace std;
map<string, DVDCollection> collection;
void removeDVD(string);
void updateDVD(string);
void showDVDs(vector<string>&, vector<string>&, DVDCollection*);
int main()
{
DVDCollection myDVDs;
int selection;
int numActors;
string aName;
string cName;
string title;
int length;
int year;
cout << "This program will allow you to keep track of all your favorite ";
cout << "DVDs. ";
cout << endl;
do
{
cout << " " << "1 = Add a DVD ";
cout << " " << "2 = Remove a DVD ";
cout << " " << "3 = Update a DVD ";
cout << " " << "4 = Show all DVDs ";
cout << " " << "5 = Exit program ";
cout << endl;
cout << "What would you like to do? ";
cin >> selection;
while (selection < 1 || selection > 5)
{
cout << "Please enter the number of your choice. ";
cin >> selection;
}
switch (selection)
{
case 1:
cout << "You have choosen to Add a DVD. ";
cout << "I will need the following information: ";
cin.ignore();
cout << "Movie title: ";
getline(cin, title);
cout << "Year movie was released: ";
cin >> year;
cout << "Length of movie (in minutes): ";
cin >> length;
cout << "Number of major Actors/Actresses in the movie: ";
cin >> numActors;
cin.ignore();
myDVDs.setTitle(title);
myDVDs.setYear(year);
myDVDs.setLength(length);
collection.insert(make_pair(title, myDVDs));
for (int count = 0; count < numActors; count++)
{
cout << "Actor/Actress #" << (count + 1) << "'s name: ";
getline(cin, aName);
cout << "Name of their character: ";
getline(cin, cName);
myDVDs.setCharInfo(aName, cName);
}
break;
case 2:
cout << "You have chosen to remove a DVD. ";
//removeDVD();
break;
case 3:
cout << "You have chosen to update a DVD. ";
// updateDVD();
break;
case 4:
cout << "You have chosen to view all DVDs in the collection. ";
showDVDs(myDVDs.Actor, myDVDs.CharName, &myDVDs);
break;
}
} while (selection != 5);
cout << endl;
cout << "See you at the movies! ";
system("pause");
return 0;
}
void showDVDs(vector<string> &vectorActor, vector<string> &vectorCharName, DVDCollection *myDVDs)
{
cout << setw(10) << " Movie Title: " << setw(10) << "Year Released: "
<< setw(10) << "Length: " << setw(10) << "Actors/Actresses: "
<< setw(10) << "Characters: ";
cout << "-----------------------------------------------------------";
cout << endl;
for (auto count = 0; count < vectorActor.size(); count++)
{
if (count < 1)
{
if (myDVDs[count].getLength() != 0)
{
cout << setw(10) << right << myDVDs[count].getTitle() << setw(10)
<< myDVDs[count].getYear() << setw(10) << myDVDs[count].getLength();
}
else
{
cout << setw(10) << right << myDVDs[count].getTitle();
}
}
if (count < 1)
{
cout << " " << vectorActor[count] << " " <<
vectorCharName[count] << endl;
}
else
{
cout << " " << vectorActor[count] << " " <<
vectorCharName[count] << endl;
}
}
}
Explanation / Answer
Well that's expected .. You have created a single object "myDVDs" having a single title, year and length variable . Inside the "Do while" loop in main() everytime you are add a new DVD, it overrides the name , year and length.
I would suggest you create an array of objects in main() , something like this :
DVDCollection myDVDs[200];
instead of
DVDCollection myDVDs;
main() should look something like this :
int main()
{
DVDCollection myDVDs[200]; //Since you have fixed the size , you cannot enter more than 200 DVD's info.
int selection;
int numActors;
string aName;
string cName;
string title;
int length;
int year;
int count =0, j = 0;
cout << "This program will allow you to keep track of all your favorite ";
cout << "DVDs. ";
cout << endl;
do
{
cout << " " << "1 = Add a DVD ";
cout << " " << "2 = Remove a DVD ";
cout << " " << "3 = Update a DVD ";
cout << " " << "4 = Show all DVDs ";
cout << " " << "5 = Exit program ";
cout << endl;
cout << "What would you like to do? ";
cin >> selection;
while (selection < 1 || selection > 5)
{
cout << "Please enter the number of your choice. ";
cin >> selection;
}
switch (selection)
{
case 1:
cout << "You have choosen to Add a DVD. ";
cout << "I will need the following information: ";
cin.ignore();
cout << "Movie title: ";
getline(cin, title);
cout << "Year movie was released: ";
cin >> year;
cout << "Length of movie (in minutes): ";
cin >> length;
cout << "Number of major Actors/Actresses in the movie: ";
cin >> numActors;
cin.ignore();
count++;
myDVDs[count - 1].setTitle(title);
myDVDs[count - 1].setYear(year);
myDVDs[count - 1].setLength(length);
collection.insert(make_pair(title, myDVDs)); // Not sure why this is needed
for (int count = 0; count < numActors; count++)
{
cout << "Actor/Actress #" << (count + 1) << "'s name: ";
getline(cin, aName);
cout << "Name of their character: ";
getline(cin, cName);
myDVDs[count - 1].setCharInfo(aName, cName);
}
break;
case 2:
cout << "You have chosen to remove a DVD. ";
//removeDVD();
break;
case 3:
cout << "You have chosen to update a DVD. ";
// updateDVD();
break;
case 4:
cout << "You have chosen to view all DVDs in the collection. ";
for(j=0; j<count ; j++){
showDVDs(myDVDs[i].Actor, myDVDs[i].CharName, &myDVDs[i]);
}
break;
}
} while (selection != 5);
cout << endl;
cout << "See you at the movies! ";
system("pause");
return 0;
}
Now you need to change the showDVDs() too .
void showDVDs(vector<string> &vectorActor, vector<string> &vectorCharName, DVDCollection *myDVDs)
{
cout << setw(10) << " Movie Title: " << setw(10) << "Year Released: "
<< setw(10) << "Length: " << setw(10) << "Actors/Actresses: "
<< setw(10) << "Characters: ";
cout << "-----------------------------------------------------------";
cout << endl;
cout << setw(10) << right << myDVDs->getTitle() << setw(10) << myDVDs->getYear() << setw(10) << myDVDs->getLength() << setw(10);
for (auto count = 0; count < vectorActor.size(); count++)
{
cout<< vectorActor[count];
}
cout << setw(10);
for (auto count = 0; count < vectorCharName.size(); count++)
{
cout<< vectorCharName[count];
}
cout << endl;
}
Also programs store the data into the main memory and not the secondary memory(HDD) hence it is expected that you will lose all the DVD information once the program ends. If you want the program to store the data permanently , then it needs to be written to a file and should be read everytime the program starts, but i don't think that is needed here.
Please try the above suggested changes and comment if you need further help.