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!

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

Hi tarheelfan_08 :-)

You have implemented you stack as a linked list, but when you push a new element you forget to add the current 'top' to the new element. That is why you only have the last pushed element on the stack.

Your current push:

void Inventory::push(string description, double PurchasePrice, double SalePrice)
{
   top = new StackNode(description, PurchasePrice, SalePrice);
}

add the current top to the constructor call:

void Inventory::push(string description, double PurchasePrice, double SalePrice)
{
   top = new StackNode(description, PurchasePrice, SalePrice, top);
}

Edited 6 Years Ago by mbulow: n/a

This article has been dead for over six months. Start a new discussion instead.