Thread: Dynamic Stack Error

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    20

    Dynamic Stack Error

    Hey guys, I am running the following program and when I do it only shows the Gold Skate Board, can someone please tell me what I am doing wrong with my dynamic stack and why this does not work! It looks just like my other program!

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    
    //Inventory Class
    class Inventory 
    {
    private:
    	class StackNode
    	{
    		friend class Inventory;
    		string word;
    		double value;
    		double valueTwo;
    		StackNode *next;
    
    		//Constructor
    		StackNode(string word1, double value1, double value2, StackNode *next1 = NULL)
    		{
    			word = word1;
    			value = value1;
    			valueTwo = value2;
    			next = next1;
    		}
    	};
    	StackNode *top;
    
    	string description;
    	double PurchasePrice;
    	double SalePrice;
    
    public:
    	Inventory() { top = NULL; }
    	void push(string, double, double);
    	void pop(string &, double &, double &);
    	bool isEmpty();
    
    	~Inventory() {delete &nInventory;}
    	int nInventory;
    
    	//mutator and accessor functions
    	Inventory (string, double, double);
    	void setDescription(string);
    	void setPurchasePrice(double);
    	void setSalePrice(double);
    
    	string getDescription();
    	double getPurchasePrice();
    	double getSalePrice();
    
    	void interest_details();
    };
    
    
    Inventory::Inventory(string description, double PurchasePrice, double SalePrice)
    {
    //this pointers to current class
    	this->description = description;
    	this->PurchasePrice = PurchasePrice;
    	this->SalePrice = SalePrice;
    
    }
    
    void Inventory::setDescription(string description)	{
    	Inventory::description = description;
    }
    
    void Inventory::setPurchasePrice(double PurchasePrice)	{
    	Inventory::PurchasePrice = PurchasePrice;
    }
    
    void Inventory::setSalePrice(double SalePrice)	{
    	Inventory::SalePrice = SalePrice;
    }
    
    string Inventory::getDescription() {
    	return description;
    }
    
    double Inventory::getPurchasePrice() {
    	return PurchasePrice;
    }
    
    double Inventory::getSalePrice() {
    	return SalePrice;
    
    }
    
    void Inventory::interest_details() {
    	cout << "The current product it a " << description <<
    		", the purchase price is " << PurchasePrice <<
    		", anda sale price of " << SalePrice << ".\n";
    }
    Code:
    #include "Inventory.h"
    using namespace std;
    
    //Push arguments onto stack
    void Inventory::push(string description, double PurchasePrice, double SalePrice)
    {
    	top = new StackNode(description, PurchasePrice, SalePrice);
    }
    
    //Pop Removed value at top of stack and copies it to variable
    void Inventory::pop(string &description, double &PurchasePrice, double &SalePrice)
    {
    	StackNode *temp;
    	if (isEmpty())
    	{
    		cout << "The stack is empty.\n";
    		exit(1);
    	}
    	else //Pop value off top of stack
    	{
    		description = top->word;
    		PurchasePrice = top->value;
    		SalePrice = top->valueTwo;
    		temp = top;
    		top = top->next;
    		delete temp;
    	}
    }
    
    //Returns true if stack is empty or false otherwise
    bool Inventory::isEmpty()
    {
    	if(!top)
    		return true;
    	else
    		return false;
    }
    
    
    int main() {
    
    	Inventory stack;
    
    	string catchWord;
    	double catchVal;
    	double catchVal2;
    	//Push Information
    	cout << "Pushing first Inventory Item \n";
    	stack.push("Red Skateboard", 50, 85);
    	cout << "Pushing second Inventory Item \n";
    	stack.push("Green Skateboard", 45, 77);
    	cout << "Pushing third Inventory Item \n";
    	stack.push("Blue Skateboard", 35, 76);
    	cout << "Pushing fourth Inventory Item \n";
    	stack.push("Silver Skateboard", 44, 80);
    	cout << "Pushing fifth Inventory Item \n\n";
    	stack.push("Gold Skateboard", 35, 70);
    
    	cout << "Popping...\n\n";
    	stack.pop(catchWord, catchVal, catchVal2);
    	cout << "The current product is a " << catchWord << ", the purchase price is $" << catchVal << ", and a sale price of $" << catchVal2 << ".\n";
    	
    	stack.pop(catchWord, catchVal, catchVal2);
    	cout << "The current product is a " << catchWord << ", the purchase price is $" << catchVal << ", and a sale price of $" << catchVal2 << ".\n";
    	
    	stack.pop(catchWord, catchVal, catchVal2);
    	cout << "The current product is a " << catchWord << ", the purchase price is $" << catchVal << ", and a sale price of $" << catchVal2 << ".\n";
    	
    	stack.pop(catchWord, catchVal, catchVal2);
    	cout << "The current product is a " << catchWord << ", the purchase price is $" << catchVal << ", and a sale price of $" << catchVal2 << ".\n";
    
    	stack.pop(catchWord, catchVal, catchVal2);
    	cout << "The current product is a " << catchWord << ", the purchase price is $" << catchVal << ", and a sale price of $" << catchVal2 << ".\n";
    
    	cout << "\nAttempting to pop again... ";
    	stack.pop(catchWord, catchVal, catchVal2);
    
    		return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Did you forget to finish the Inventory::push function? There's something missing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM