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;
}

Recommended Answers

All 2 Replies

It looks like all your errors/problems are in "cdrom.cpp".

In the function CDROM::setName() you have getline(cin, name) being printed out in your cout statement. The function getline() returns *this which is a pointer to the istream object being used (I think, either way this is not what you want). Also you have cin.ignore() being used which is pointless because getline() reads the '\n'. cin.ignore() should be used after reading in a number (int, float, double) because when reading in a number you only read in digits, decimals and signs (+/-), which results in characters typed in after to be left in the input buffer (ie "22char\n" results in "char\n" being left in the input buffer).

The next function, CDROM::setType(), has the exact same problem as CDROM::setName() so get rid of the cin.ignore() and do not output getline().

In CDROM::setCost() you want to clear the input buffer after you input the number because it leaves characters in the buffer, but you do this after you get the input not before. In order to use the line cin.ignore(numeric_limits<streamsize>::max(), '\n'); you need to include <limits> at the top because numeric_limits<streamsize>::max() is defined within it. If you do not clear the input buffer then when this loops for the next CDROM input you will not see text pop up until you type in something.

Here is what I did to correct the errors.

void CDROM::setName()
{
    cout << "Please enter name : ";
    getline(cin, name);
}

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);
        if (cdType != "Game" && cdType != "Word" && cdType !="Compiler" && cdType !="Spreadsheet" && cdType !="DBase" && cdType !="Presentation")
        {
            cout << "Invalid Type" << endl;
        }
        else
        {
            break;
        }
    }
}

void CDROM::setCost()
//Sets CDROM cost
{
    while (true)
    {
        cout << "Please Enter Cost: $";
        cin >> cost;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        if (cost <= 0 || cin.fail() )
        {
            cout << "Invalid cost." << endl;
            cin.clear();
            cin.ignore();
        }
        else
        {
            break;
        }
    }
}

Thanks that did solve my problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.