Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ programming. I am getting error please help In this program you will use the stack class you created in Assignment 1. First you will

C++ programming. I am getting error please help

In this program you will use the stack class you created in Assignment 1. First you will create a class called InventoryItem. This class will have its class declaration in InventoryItem.h and its implementation in InventoryItem.cpp. It will have three private data members, an integer serialNum which holds the parts serial number, manufactDate which should be a string that holds the date the item was manufactured, then lotNum which will be an integer that holds the parts lot number. The program should then create a stack with a data type of InventoryItem (stack). The program should loop asking the user to enter in new items to the inventory stack or to remove an item from the inventory stack. The loop should continue until the user indicates they are done. This should be menu driven. When adding an item, the program should ask the user for the information it needs for the 3 data members of the InventoryItem class and add a new item to the stack. When removing an item from the stack, the program should display all of the information in the InventoryItem object that was popped off the stack. When the program ends, it should pop all of the remaining items off the stack and display the data that is in the Inventory items as it pops them off. There should be 3 utility functions that main uses.

void popItem(DynStack* stack) // pops the item off the stack and displays it.

void pushItem(DynStack* stack) // pushes the item onto the stack

int menu(); // displays the menu and returns the users choice.

This is what I have. Please help me fix error.

//For Main.cpp

#include "DynStack.h"

#include "InventoryItem.h"

#include

using namespace std;

void popItem(DynStack*);

void pushItem(DynStack*);

int menu(); // displays and returns the choice of the user.

int main()

{

DynStack* invStack;

invStack = new DynStack;

// Holds the menu selection from menu()

int menuChoice = 0;

// Variable for menu loop

bool valid = true;

// Values for switch statement

const int ADDITEM = 1;

const int REMITEM = 2;

const int EXIT = 3;

// Used when printing out contents upon program exit

InventoryItem temp;

while (valid)

{

menuChoice = menu();

switch (menuChoice)

{

case ADDITEM: // to add items to the stack

pushItem(invStack);

valid = true;

break;

case REMITEM: // to remove items from the stack and display their contents

popItem(invStack);

valid = true;

break;

case EXIT:

// On exit, any inventory must be popped off the stack and

// printed, in sequence. Otherwise the program simply ends.

if (invStack->isEmpty() != true)

{

cout << "Here are the remaining items in inventory: ";

while (invStack->pop(temp) == true) // Loop runs until stack is empty

{

cout << "Lot Number: " << temp.getLotNum() << " ";

cout << "Serial Number: " << temp.getSerNum() << " ";

cout << "Manufacturing Date: " << temp.getManDate() << " ";

}

// Display msg once all nodes are popped off

if (invStack->isEmpty() == true)

cout << "No further inventory. ";

}

else

cout << "No inventory to display; exiting program. ";

return 0;

}

}

return 0;

}

// For popItem

void popItem(DynStack* stack)

{

InventoryItem temp;

char repeat = 'y'; // to control the loop

while (repeat == 'y' || repeat == 'Y')

{

if (stack->pop(temp) == true)

{

cout << " Remove Item ";

cout << "Lot Number: " << temp.getLotNum() << " ";

cout << "Serial Number: " << temp.getSerNum() << " ";

cout << "Manufacturing Date: " << temp.getManDate() << " ";

cout << "Remove and display another item y/n? ";

cin >> repeat;

}

else // If stack is empty, display error message

{

cout << "No items in Remaining. ";

repeat = 'n'; // Set repeat to n to return to main menu

}

}

}

// For pushItem

void pushItem(DynStack* stack)

{

InventoryItem temp;

int lNum, sNum;

string manDate;

char repeat = 'y'; // to control the loop

while (repeat == 'y' || repeat == 'Y')

{

cout << "| Add Item | ";

cout << "Enter lot number: ";

// Verifing the input

cin >> lNum;

while (lNum < 0)

{

cout << "Lot number must be a non-negative number. ";

cout << "Enter lot number: ";

cin >> lNum;

}

cout << "Enter serial number: ";

cin >> sNum;

while (sNum < 0)

{

cout << "Serial number must be a non-negative number. ";

cout << "Enter serial number: ";

cin >> sNum;

}

cout << "Enter manufacturing date in MM/DD/YYYY format: ";

cin >> manDate;

temp.setLotNum(lNum);

temp.setSerNum(sNum);

temp.setManDate(manDate);

stack->push(temp);

cout << "Add another item to inventory y/n? ";

cin >> repeat;

}

}

int menu() // displays the menu and returns the users choice.

{

int choice;

cout << "------------------------- ";

cout << "| Inventory item Menu | ";

cout << "------------------------- ";

cout << "1) Add item to the Inventory ";

cout << "2) Remove item from Inventory ";

cout << "3) Exit program ";

cout << "Selection number: ";

cin >> choice;

// Check that it's a valid choice

while (choice <= 0 || choice > 3)

{

cout << "Invalid choice. Pick 1, 2, or 3: ";

cin >> choice;

}

return choice;

}

//For DynStack.h

#pragma once

// Declaration file for the DynStack class. This class is a template version of

// a dynamic stack class.

#ifndef DYNSTACK_H

#define DYNSTACK_H

template

class DynStack

{

private:

// Struct for stack nodes

struct StackNode {

T value; // holds the node's value

StackNode *next; // ptr to next node

};

StackNode *top; // ptr to stack top

public:

// Constructor

DynStack();

// Destructor

~DynStack();

// Stack operations

void push(T);

bool pop(T &);

bool isEmpty();

void displayStack() const;

};

#endif

//For DynStack.cpp

/************ DynStack.h ***********/

/* This file contains the decleration of DynStack class */

// Declaration file for the DynStack class. This class is a template version of

// a dynamic stack class.

#ifndef DYNSTACK_H

#define DYNSTACK_H

template

class DynStack

{

private:

// Struct for stack nodes

struct StackNode {

T value; // holds the node's value

StackNode *next; // ptr to next node

};

StackNode *top; // ptr to stack top

public:

// Constructor

DynStack();

// Destructor

~DynStack();

// Stack operations

void push(T);

bool pop(T &);

bool isEmpty();

void displayStack() const;

};

#endif

/************ DynStack.cpp ***********/

// This file contains Implementation for the DynStack class. This class is a template version of

// a dynamic stack class.

#include "DynStack.h"

// Default constructor

template

DynStack::DynStack()

{

top = nullptr;

}

// Destructor - deletes the stack node by node

template

DynStack::~DynStack()

{

StackNode *nodePtr;

StackNode *nextNode;

// aim nodePtr at top of stack

nodePtr = top;

// travel list and delete each node

while (nodePtr != nullptr)

{

nextNode = nodePtr->next;

delete nodePtr;

nodePtr = nextNode;

}

}

///////////////////////

// Stack operations //

/////////////////////

// push() adds the argument onto the stack

template

void DynStack::push(T item)

{

StackNode *newNode = nullptr;

// Creates a new node and stores argument there

newNode = new StackNode;

newNode->value = item;

// If the list is empty, make newNode the first node

if (isEmpty() == true)

{

top = newNode;

newNode->next = nullptr;

}

else

{

newNode->next = top;

top = newNode;

}

}

// If the stack is empty, then pop() simply returns false.

// If a node exists, pop() returns the top item, deletes it from the stack,

// and then returns true.

template

bool DynStack::pop(T &item)

{

StackNode *temp = nullptr;

bool status;

// Check that the stack isn't empty

if (isEmpty() == true)

{

status = false;

}

else // Pop value off top of stack

{

item = top->value;

temp = top->next;

delete top;

top = temp;

status = true;

}

return status;

}

// isEmpty() returns true if stack is empty; otherwise it returns false.

template

bool DynStack::isEmpty()

{

bool status;

if (!top)

status = true;

else

status = false;

return status;

}

// displayStack() simply prints each item in a given stack.

template

void DynStack::displayStack() const

{

StackNode *nodePtr;

nodePtr = top;

if (nodePtr == nullptr)

cout << "Stack is empty.";

else

{

while (nodePtr)

{

cout << nodePtr->value << endl;

nodePtr = nodePtr->next;

}

}

}

//For InventoryItem.h

#pragma once

#ifndef INVENTORYITEM_H

#define INVENTORYITEM_H

#include

using namespace std;

class InventoryItem

{

private:

int serialNum;

string manufactDate;

int lotNum;

public:

// Default Constructor

InventoryItem();

// Parameterized Constructor - serialNum, manufactDate, lotNum

InventoryItem(int, string, int);

// Methods to read the data from user

int getSerNum() const;

string getManDate() const;

int getLotNum() const;

// Methods to set the data

void setSerNum(int);

void setManDate(string);

void setLotNum(int);

};

#endif

//For Inventoryitem.cpp

#include "InventoryItem.h"

// Default constructor

InventoryItem::InventoryItem()

{

}

// Parameterized Constructor - serialNum, manufactDate, lotNum

InventoryItem::InventoryItem(int Snum, string ManD, int Lnum)

{

serialNum = Snum;

manufactDate = ManD;

lotNum = Lnum;

}

// get functions or Accessors

int InventoryItem::getLotNum() const

{

return lotNum;

}

int InventoryItem::getSerNum() const

{

return serialNum;

}

string InventoryItem::getManDate() const

{

return manufactDate;

}

// set functions or Mutators

void InventoryItem::setLotNum(int lNum)

{

lotNum = lNum;

}

void InventoryItem::setSerNum(int sNum)

{

serialNum = sNum;

}

void InventoryItem::setManDate(string manu)

{

manufactDate = manu;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions

Question

please dont use chat gpt 6 7 4 .

Answered: 1 week ago

Question

1. Identify outcomes (e.g., quality, accidents).

Answered: 1 week ago