Please see that attached code. My output is as follows
Please enter name : dog
0x7fff71771e60
Please Enter Type: Valid Choses Are (Game, Word, Compiler, Spreadsheet, DBase, Presentation : Game
0x7fff71771e60
Please Enter Cost: $
20
Its like either the pointer or the getline is not clearing out memory properly. At least I think based on the fact it looks like I am getting memory address's.
CS215Lab1.cpp
#include <iostream>
#include "cdrom.h"
#include "cdromsupport.h"
using namespace std;
int main ()
{
// Declares 3 CDROM Pointers
CDROM *pCDROM1;
CDROM *pCDROM2;
CDROM *pCDROM3;
//Check For Dynamic Memory Allocation
pCDROM1 = new CDROM;
if (pCDROM1 == NULL)
{
cout << "Dynamic Memory Allocation FAILED" << endl;
}
pCDROM2 = new CDROM;
if (pCDROM2 == NULL)
{
cout << "Dynamic Memory Allocation FAILED" << endl;
}
pCDROM3 = new CDROM;
if (pCDROM3 == NULL)
{
cout << "Dynamic Memory Allocation FAILED" << endl;
}
// Loads each CDROM with data
pCDROM1->loadInfo();
pCDROM2->loadInfo();
pCDROM3->loadInfo();
pCDROM1->displayInfo();
pCDROM2->displayInfo();
pCDROM3->displayInfo();
// calls global function show cost
// showcost();
// Changes data on CDROM 1 and CDROM 2
pCDROM1->changeData();
pCDROM2->changeData();
pCDROM3->changeData();
// Redisplay CDROM with New Data
pCDROM1->displayInfo();
pCDROM2->displayInfo();
pCDROM3->displayInfo();
// Call giveMemoryBack function
giveMemoryBack(pCDROM1, pCDROM2, pCDROM3);
return 0;
}
CDROM.CPP
#include <iostream>
#include "cdrom.h"
#include <iomanip>
using namespace std;
CDROM::CDROM(): cost(0.0), cdType("Presentation"), name()
{
}
void CDROM::setName()
{
cout << "Please enter name : " <<
getline(cin, name);
// test code
cin.ignore(1000, '\n');
}
void CDROM::setCost()
//Sets CDROM cost
{
while (true)
{
cout << "Please Enter Cost: $" << endl;
// cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> cost;
//if (cin || cin.gcount() !=1 || cin.fail() )
if (cost <= 0 || cin.fail() )
{
cout << "Invalid cost." << endl;
cin.clear();
cin.ignore();
}
else
{
break;
}
}
}
void CDROM::setType()
// Sets the CDROM Type
{
while (true)
{
cout << "Please Enter Type: Valid Choses Are (Game, Word, Compiler, Spreadsheet, DBase, Presentation : " <<
getline(cin, cdType);
cin.ignore(1000, '\n');
if (cdType != "Game" && cdType != "Word" && cdType !="Compiler" && cdType !="Spreadsheet" && cdType !="DBase" && cdType !="Presentation")
{
cout << "Invalid Type" << endl;
}
else
{
break;
}
}
}
float CDROM::returnCost()
// Fucntion to return cost
const
{
return cost;
}
void CDROM::displayInfo() //Function to display current CDROM Info
{
cout << "Current CDROM listings are as follows:" << endl;
cout << "Name: " << name << endl;
cout << "Type: " << cdType << endl;
cout << "Cost: " << setprecision(2) << "$ " << cost << endl;
}
void CDROM::loadInfo()
{
// Calls function to set the name, CDROM Type, and Cost.
setName();
setType();
setCost();
}
void CDROM::changeData()
// Function used to change data "Name, Type, and Cost"
{
cout << "Change Name: (Y/N)";
string change;
cin >> change;
cin.ignore();
if ((change == "Y" || (change == "y")))
{
setName();
}
cout << "Change Type: (Y/N)";
cin >> change;
cin.ignore();
if ((change == "Y" || (change == "y")))
{
setType();
}
cout << "Change Cost: (Y/N)";
cin >> change;
cin.ignore();
if ((change == "Y" || (change == "y")))
{
setCost();
}
}
CDROM.h
#include <string>
using namespace std;
// Class declaration for CDROM
class CDROM
{
public: // available outside of class
CDROM(); // constructor hard code initilized data members
// Member Functions
void loadInfo(); // Prompts For Input, Validates and set all data members
void displayInfo(); // Display To The screen the values in the data members in a neatly organized way
void changeData(); // Interactively change the data in the calling object similar to lad information exept the old information is being selectively over written
float returnCost() const; // Retun the value in the cost member data
private: // Only available inside class
void setName(); // Used to set the name
void setCost(); // Used to set the cost
void setType(); // Sets CDROM Type
string name; // Name of The CDROM i.e "Myst"
string cdType; // Game, Word, Compiler, Spreadsheet, DBase, Presentation
float cost; // sets variable cost as a float
};
cdromsupport.h
int giveMemoryBack (CDROM *pCD1, CDROM *pCD2, CDROM *pCD3);
cdromsupport.cpp
#include "cdrom.h"
#include "cdromsupport.h"
int giveMemoryBack (CDROM *pCD1, CDROM *pCD2, CDROM *pCD3)
{
delete pCD1;
delete pCD2;
delete pCD3;
return 0;
}