Feb 20, 2014 at 9:31am UTC
Hey yall I was hoping to get help with this program I have written. This is to create DVD objects and prompt the user for DVD information. This information is then stored in a vector. The issue I am having is trying to change the data of a ptr in the vector. I can call a global function, input the information but when I display the vector again it shows the original data inputted. If anyone could please help that would be great, I have been stuck on this for awhile.
// DVD claas declaration w/include guards
//DVD.h
#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <iomanip>
using namespace std;
#ifndef DVD_H
#define DVD_H
class DVD
{
public:
DVD();
void LoadInformation();
void SetTitle(string NewTitle);
string GetTitle();
void SetType(string NewType);
string GetType();
void SetCost(float NewCost);
float GetCost();
private:
string Title;
string CDType;
float Cost = 0.00;
void ListValidDVDTypes()
{
cout << "Select DVD Type from menu below.\n";
cout << "1. Game" << endl;
cout << "2. Word" << endl;
cout << "3. Compiler" << endl;
cout << "4. Spreadsheet" << endl;
cout << "5. DBase" << endl;
cout << "6. Presentation" << endl;
}
};
#endif
// Implementations of DVD class member functions
//DVD.cpp
#include"DVD.h"
DVD::DVD()
{
Title = "No Title";
CDType = "Game";
Cost = 0.00;
}
void DVD::LoadInformation()
{
int choice = 0;
cout << "DVD object created." << endl;
cout << "Enter DVD Title: ";
cin >> Title;
ListValidDVDTypes();
cout << "Enter number that corresponds to DVD type (1-6): ";
cin >> choice;
switch (choice)
{
case 1:
choice = 1;
CDType = "Game";
break;
case 2:
choice = 2;
CDType = "Word";
break;
case 3:
choice = 3;
CDType = "Compiler";
break;
case 4:
choice = 4;
CDType = "Spreadsheet";
break;
case 5:
choice = 5;
CDType = "DBase";
break;
case 6:
choice = 6;
CDType = "Presentation";
break;
}
cout << "Please enter the cost of the DVD: $";
cin >> Cost;
cout << "\n";
}
void DVD::SetTitle(string NewTitle)
{
Title = NewTitle;
}
string DVD::GetTitle()
{
return Title;
}
void DVD::SetType(string NewType)
{
CDType = NewType;
}
string DVD::GetType()
{
return CDType;
}
void DVD::SetCost(float NewCost)
{
Cost = NewCost;
}
float DVD::GetCost()
{
return Cost;
}
//Global function prototype
//DisplayGlobalFunctions.h
#include "DVD.h"
void Display_DVD_Information(vector<unique_ptr<DVD>> & DVDs);
void DisplayTotalAndAverageCosts(vector<unique_ptr<DVD>> &DVDs);
void ChangeDVDData(unique_ptr<DVD> &aDVD);
//Implementation of Global Functions
//Display_Global_Functions.cpp
#include "DisplayGlobalFunctions.h"
void Display_DVD_Information(vector<unique_ptr<DVD>> & DVDs)
{
for (int i = 0; i < DVDs.size() - 2; ++i)
{
cout << "DVD 1 Information" << endl;
cout << "Title: " << DVDs[0]->GetTitle() << endl;
cout << "Type: " << DVDs[0]->GetType() << endl;
cout << "Cost: $" << DVDs[0]->GetCost() << "\n" << endl;
cout << "DVD 2 Information" << endl;
cout << "Title: " << DVDs[1]->GetTitle() << endl;
cout << "Type: " << DVDs[1]->GetType() << endl;
cout << "Cost: $" << DVDs[1]->GetCost() << "\n" << endl;
cout << "DVD 3 Information" << endl;
cout << "Title: " << DVDs[2]->GetTitle() << endl;
cout << "Type: " << DVDs[2]->GetType() << endl;
cout << "Cost: $" << DVDs[2]->GetCost() << endl;
}
}
void DisplayTotalAndAverageCosts(vector<unique_ptr<DVD>> &DVDs)
{
float total = DVDs[0]->GetCost() + DVDs[1]->GetCost() + DVDs[2]->GetCost();
float average = (DVDs[0]->GetCost() + DVDs[1]->GetCost() + DVDs[2]->GetCost()) / 3;
cout << "\n";
cout << "DVD1 Cost: $" << DVDs[0]->GetCost() << endl;
cout << "DVD2 Cost: $" << DVDs[1]->GetCost() << endl;
cout << "DVD2 Cost: $" << DVDs[2]->GetCost() << endl;
cout << "\n";
cout << "The total cost of all 3 DVDs are: $" << total << "\n" << endl;
cout << "The average of cost of all 3 DVDs are: $" << average << "\n" << endl;
}
void ChangeDVDData(unique_ptr<DVD> &aDVD)
{
int changeDVD = 0;
cin >> changeDVD;
switch (changeDVD)
{
case 1:
{
changeDVD = 1;
cout << "Enter new DVD Information" << endl;
aDVD.release();
aDVD.reset(new DVD);
aDVD->LoadInformation();
break;
}
case 2:
{
changeDVD = 2;
cout << "You have made no changes!\n" << endl;
break;
}
}
}
//Test driver for DVD class
//Lab1.cpp
#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <iomanip>
#include "DVD.h"
#include "DisplayGlobalFunctions.h"
using namespace std;
void Display_DVD_Information(vector<unique_ptr<DVD>> & DVDs);
void DisplayTotalAndAverageCosts(vector<unique_ptr<DVD>> &DVDs);
void ChangeDVDData(unique_ptr<DVD> &aDVD);
int main()
{
cout << "Welcome to the DVD program!" << endl;
DVD::DVD();
vector<unique_ptr<DVD>> DVD_Container;
unique_ptr<DVD> pDVD1(new DVD);
assert(pDVD1 != NULL);
unique_ptr<DVD> pDVD2(new DVD);
assert(pDVD2 != NULL);
unique_ptr<DVD> pDVD3(new DVD);
assert(pDVD3 != NULL);
pDVD1->LoadInformation();
cout << "Title: " << pDVD1->GetTitle() << endl;
cout << "Type: " << pDVD1->GetType() << endl;
cout << "Cost: $" << pDVD1->GetCost() << endl;
DVD_Container.push_back(std::move(pDVD1));
cout << "DVD1 Object Created!\n" << endl;
pDVD2->LoadInformation();
cout << "Title: " << pDVD2->GetTitle() << endl;
cout << "Type: " << pDVD2->GetType() << endl;
cout << "Cost: $" << pDVD2->GetCost() << endl;
DVD_Container.push_back(std::move(pDVD2));
cout << "DVD2 Object Created!\n" << endl;
pDVD3->LoadInformation();
cout << "Title: " << pDVD3->GetTitle() << endl;
cout << "Type: " << pDVD3->GetType() << endl;
cout << "Cost: $" << pDVD3->GetCost() << endl;
DVD_Container.push_back(std::move(pDVD3));
cout << "DVD3 Object Created!\n" << endl;
Display_DVD_Information(DVD_Container);
DisplayTotalAndAverageCosts(DVD_Container);
cout << "Current DVD1 Information" << endl;
cout << "Title: " << DVD_Container[0]->GetTitle() << endl;
cout << "Type: " << DVD_Container[0]->GetType() << endl;
cout << "Cost: " << DVD_Container[0]->GetCost() << endl;
cout << "Would you like to change DVD1 data?(1 = Yes, 2 = No): ";
ChangeDVDData(pDVD1);
cout << "Current DVD2 Information" << endl;
cout << "Title: " << DVD_Container[1]->GetTitle() << endl;
cout << "Type: " << DVD_Container[1]->GetType() << endl;
cout << "Cost: " << DVD_Container[1]->GetCost() << endl;
cout << "Would you like to change DVD2 data?(1 = Yes, 2 = No): ";
ChangeDVDData(pDVD2);
cout << "Current DVD3 Information" << endl;
cout << "Title: " << DVD_Container[2]->GetTitle() << endl;
cout << "Type: " << DVD_Container[2]->GetType() << endl;
cout << "Cost: " << DVD_Container[2]->GetCost() << endl;
cout << "Would you like to change DVD3 data?(1 = Yes, 2 = No): ";
ChangeDVDData(pDVD3);
Display_DVD_Information(DVD_Container);
DisplayTotalAndAverageCosts(DVD_Container);
return 0;
}
Feb 20, 2014 at 10:36am UTC
LOL thankyou so much, I was stuck for a minute. I had actually tried using a for loop in main using reset, erase and release. Hard to find examples to a specific situation. So just to make sure I understand, bc I think this is where I get a bit confused with pointers. I am not passing the unique_ptr pDVD1 down to ChangeDVDData, but passing the body of the global function up to main, does that sound about right?
Feb 20, 2014 at 11:25am UTC
Oh ok I got ya, appreciate the illustration too. Thanks again you've been a big help.