希望这将是我暂时的最后一个问题。
我有点困惑为什么我的功能不起作用。这是我的代码(我稍后会解释一下):
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
class Inventory {
public:
void SetSumInv(int prcInDllrs, int individualquantity) {
priceInDollars = priceInDollars + (prcInDllrs * individualquantity);
totalInvPriceInDollars = totalInvPriceInDollars + priceInDollars;
}
void SetItemPrice(int whatever) {
itemsPrice = whatever;
}
void SetName(string nm)
{
name = nm;
};
void SetQuantity(int qnty)
{
quantity = qnty;
};
void SetAuthor(string athr) {
author = athr;
}
void SetExpiration(string expir)
{
expiration = expir;
};
virtual void Print(){
cout << name << " x" << quantity << " for: $" << itemsPrice; //" (Expires: " << expiration << ")";
if (expiration.size() != 0) {
cout << " (Expires: " << expiration << ")" << endl;
}
else {
cout << " (Author: " << author << ")" << endl;
}
}
void PrintInventory(vector<Inventory*> inventory) {
unsigned int i = 0;
if (inventory.size() == 0) {
cout << "No items to print." << endl;
}
else {
for (i = 0; i<inventory.size(); ++i) {
cout << i << " - ";
inventory.at(i)->Print();
}
cout << "Total inventory value: " << priceInDollars;
}
return;
}
void AddItemToInventory()
{
}
vector<Inventory*> AddProduceToInventory(vector<Inventory*> inventory)
{
Inventory* prdc;
string usrInptName = "";
string usrInptQntyStr = "";
istringstream inSS;
istringstream inDD;
int usrInptQnty = 0;
string usrInptExpr = "";
string usrInptPrcStr = "";
int usrInptPrc = 0;
int ItemCost = 0;
cout << "Enter name of new produce: ";
getline(cin, usrInptName);
SetName(usrInptName);
cout << "Enter quantity: ";
getline(cin, usrInptQntyStr);
inSS.str(usrInptQntyStr);
inSS >> usrInptQnty;
inSS.clear();
SetQuantity(usrInptQnty);
cout << "Enter expiration date: ";
getline(cin, usrInptExpr);
SetExpiration(usrInptExpr);
cout << "Enter the price per item: $";
getline(cin, usrInptPrcStr);
inDD.str(usrInptPrcStr);
inDD >> usrInptPrc;
inDD.clear();
SetItemPrice(usrInptPrc);
ItemCost = (usrInptPrc * usrInptQnty);
prdc = new Inventory;
prdc->SetName(usrInptName);
prdc->SetQuantity(usrInptQnty);
prdc->SetExpiration(usrInptExpr);
prdc->SetSumInv(usrInptPrc, usrInptQnty);
prdc->SetItemPrice(usrInptPrc);
inventory.push_back(prdc);
return inventory;
}
void AddBookToInventory()
{
}
vector<Inventory*> AddBookToInventory(vector<Inventory*> inventory) {
Inventory* prdct;
string usrInptName = "";
string usrInptQntyStr = "";
istringstream inSS;
int usrInptQnty = 0;
string usrInptAthr = "";
string usrInptPrcStr = "";
int usrInptPrc = 0;
istringstream inDD;
int sum = 0;
int ItemCost = 0;
cout << "Enter name of new book: ";
getline(cin, usrInptName);
cout << "Enter quantity: ";
getline(cin, usrInptQntyStr);
inSS.str(usrInptQntyStr);
inSS >> usrInptQnty;
inSS.clear();
cout << "Enter author: ";
getline(cin, usrInptAthr);
cout << "Enter the price per item: $";
getline(cin, usrInptPrcStr);
inDD.str(usrInptPrcStr);
inDD >> usrInptPrc;
inDD.clear();
ItemCost = (usrInptPrc * usrInptQnty);
prdct = new Inventory;
prdct->SetName(usrInptName);
prdct->SetQuantity(usrInptQnty);
prdct->SetSumInv(usrInptPrc, usrInptQnty);
prdct->SetAuthor(usrInptAthr);
prdct->SetItemPrice(usrInptPrc);
inventory.push_back(prdct);
return inventory;
}
void UpdateItemQtyInventory()
{}
//This is the update function in which we can change how many items a certain purchase has
vector<Inventory*> UpdateItemQtyInInventory(vector<Inventory*> inventory) {
string usrIndexChoiceStr = "";
unsigned int usrIndexChoice = 0;
istringstream inSS;
string usrInptQntyStr = "";
int usrInptQnty = 0;
if (inventory.size() == 0) {
cout << "No items to update." << endl;
}
else {
PrintInventory(inventory);
do {
cout << "Update which item #: ";
getline(cin, usrIndexChoiceStr);
inSS.str(usrIndexChoiceStr);
inSS >> usrIndexChoice;
inSS.clear();
} while (!(usrIndexChoice < inventory.size()));
cout << "Enter new quantity: ";
getline(cin, usrInptQntyStr);
inSS.str(usrInptQntyStr);
inSS >> usrInptQnty;
inSS.clear();
inventory.at(usrIndexChoice)->SetQuantity(usrInptQnty);
}
return inventory;
}
void RemoveItemFromInventory()
{}
//Here we will be removing an entire item from the inventory
vector<Inventory*> RemoveItemFromInventory(vector<Inventory*> inventory) {
istringstream inSS;
string usrIndexChoiceStr = "";
unsigned int usrIndexChoice = 0;
string usrInptQntyStr = "";
if (inventory.size() == 0) {
cout << "No items to remove." << endl;
}
else {
PrintInventory(inventory);
do {
cout << "Remove which item #: ";
getline(cin, usrIndexChoiceStr);
inSS.str(usrIndexChoiceStr);
inSS >> usrIndexChoice;
inSS.clear();
} while (!(usrIndexChoice < inventory.size()));
inventory.erase(inventory.begin() + usrIndexChoice);
}
return inventory;
}
void GetTotalValueAsPrice()
{
}
protected:
string name;
int quantity = 0;
int priceInDollars = 0;
int totalCost = 0;
int itemsPrice = 0;
string expiration;
string author;
private:
int totalInvPriceInDollars = 0;
};
int main() {
vector<Inventory*> INVENTORY;
string usrInptOptn = "default";
string usrInptOptn2 = "default";
Inventory update;
while (true) {
// Get user choice
cout << "\nEnter (p)rint, (a)dd, (u)pdate, (r)emove, or (q)uit: ";
getline(cin, usrInptOptn);
// Process user choice
if (usrInptOptn.size() == 0) {
continue;
}
else if (usrInptOptn.at(0) == 'p') {
update.PrintInventory(INVENTORY); //Different!
}
else if (usrInptOptn.at(0) == 'a') {///I don't know what the difference is between the three slashes and the two, but they are different!
cout << "\nEnter (b)ook or (p)roduce: ";
getline(cin, usrInptOptn2);
if (usrInptOptn2.at(0) == 'b') {
INVENTORY = update.AddBookToInventory(INVENTORY); //Supposed to look like: INV = AddItem...(INV);
}
else if (usrInptOptn2.at(0) == 'p') {
INVENTORY = update.AddProduceToInventory(INVENTORY);
}
else
{
continue;
}
}
else if (usrInptOptn.at(0) == 'u') {
INVENTORY = update.UpdateItemQtyInInventory(INVENTORY);
}
else if (usrInptOptn.at(0) == 'r') {
INVENTORY = update.RemoveItemFromInventory(INVENTORY);
}
else if (usrInptOptn.at(0) == 'q') {
cout << "\nGood bye." << endl;
break;
}
}
return 0;
}
所以现在。在我的班级库存中,public:void SetSumInv,这个程序做的是它获取用户输入,并统计所有内容,所以当它打印时,它应该打印库存,然后它应该打印库存的总和价值(以美元计)。我试过运行它,它不会打印库存值的总和。这有什么不对?我无法弄清楚这一点。
谢谢!
答案 0 :(得分:1)
解剖你的代码后;我花了相当多的时间来清理它。我没有时间去搜索你的bug,但我能做的是稍微清理你的课程,以便阅读更友好。我对你的一些类的函数调用做了一些修改,这些修改在注释中有解释。我还删除了你在课堂上声明的所有空函数。我还改变了类命名约定的外观(这只是用户首选项)
<强> Inventory.h 强>
#ifndef INVENTORY_H
#define INVENTORY_H
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
class Inventory {
protected:
std::string m_strName;
std::string m_strExpiration;
std::string m_strAuthor;
int m_quantity;
int m_priceInDollars;
int m_totalCost;
int m_itemsPrice;
private:
int m_totalInvPriceInDollars;
public:
Inventory(); // Default Constructor
void setName( const std::string& nm );
void setAuthor( const std::string& athr );
void setExpiration( const std::string& expir );
void setSumInv( int prcInDllrs, int individualquantity );
void setItemPrice( int whatever );
void setQuantity( int qnty );
virtual void print();
void printInventory( std::vector<Inventory*>& inventory );
// I changed these methods to pass by reference opposed to returning from function call
void addProduceToInventory( std::vector<Inventory*>& inventory );
void addBookToInventory( std::vector<Inventory*>& inventory );
void updateItemQtyInInventory( std::vector<Inventory*>& inventory );
void removeItemFromInventory( std::vector<Inventory*>& inventory );
}; // Invenotory
#endif // INVENTORY_H
<强> Inventory.cpp 强>
#include "Inventory.h"
// ----------------------------------------------------------------------------
// Inventory()
Inventory::Inventory() :
m_quantity( 0 ),
m_itemsPrice( 0 ),
m_priceInDollars( 0 ),
m_totalCost( 0 ),
m_totalInvPriceInDollars( 0 ) {
} // Inventory
// ----------------------------------------------------------------------------
// setSumInv()
void Inventory::setSumInv( int prcInDllrs, int individualquantity ) {
m_priceInDollars = m_priceInDollars + (prcInDllrs * individualquantity);
m_totalInvPriceInDollars = m_totalInvPriceInDollars + m_priceInDollars;
} // setSumInv
// ----------------------------------------------------------------------------
// setItemPrice()
void Inventory::setItemPrice( int whatever ) {
m_itemsPrice = whatever;
} // setItemPrice
// ----------------------------------------------------------------------------
// setQuantity()
void Inventory::setQuantity( int qnty ) {
m_quantity = qnty;
} // setQuantity
// ----------------------------------------------------------------------------
// setName()
void Inventory::setName( const std::string& strName ) {
m_strName = strName;
} // setName
// ----------------------------------------------------------------------------
// setAuthor()
void Inventory::setAuthor( const std::string& strAuthor ) {
m_strAuthor = strAuthor;
} // setAuthor
// ----------------------------------------------------------------------------
// setExpiration()
void Inventory::setExpiration( const std::string& strExpir ) {
m_strExpiration = strExpir;
} // setExpiration
// ----------------------------------------------------------------------------
// print()
void Inventory::print() {
std::cout << m_strName << " x" << m_quantity << " for: $" << m_itemsPrice; //" (Expires: " << expiration << ")";
if ( m_strExpiration.size() != 0 ) {
std::cout << " (Expires: " << m_strExpiration << ")" << std::endl;
} else {
std::cout << " (Author: " << m_strAuthor << ")" << std::endl;
}
} // print
// ----------------------------------------------------------------------------
// printInventory()
void Inventory::printInventory( std::vector<Inventory*>& vInventory ) {
unsigned int i = 0;
if ( vInventory.size() == 0) {
std::cout << "No items to print." << std::endl;
} else {
for ( i = 0; i < vInventory.size(); ++i ) {
std::cout << i << " - ";
vInventory.at(i)->print();
}
std::cout << "Total inventory value: " << m_priceInDollars;
}
} // printInventory
// ----------------------------------------------------------------------------
// addProduceToInventory()
void Inventory::addProduceToInventory( std::vector<Inventory*>& vInventory ) {
std::string usrInptName = "";
std::string usrInptQntyStr = "";
std::istringstream inSS;
std::istringstream inDD;
int usrInptQnty = 0;
std::string usrInptExpr = "";
std::string usrInptPrcStr = "";
int usrInptPrc = 0;
int itemCost = 0;
std::cout << "Enter name of new produce: ";
getline( std::cin, usrInptName );
setName( usrInptName );
std::cout << "Enter quantity: ";
std::getline( std::cin, usrInptQntyStr );
inSS.str( usrInptQntyStr );
inSS >> usrInptQnty;
inSS.clear();
setQuantity( usrInptQnty );
std::cout << "Enter expiration date: ";
getline( std::cin, usrInptExpr );
setExpiration( usrInptExpr );
std::cout << "Enter the price per item: $";
getline( std::cin, usrInptPrcStr );
inDD.str( usrInptPrcStr );
inDD >> usrInptPrc;
inDD.clear();
setItemPrice( usrInptPrc );
itemCost = usrInptPrc * usrInptQnty;
Inventory* pInv = nullptr; // Initialize Pointers to nullptr
pInv = new Inventory; // Using New Memory (Dyanamic) - Where Is This Being Deleted?
pInv->setName( usrInptName );
pInv->setQuantity( usrInptQnty );
pInv->setExpiration( usrInptExpr );
pInv->setSumInv( usrInptPrc, usrInptQnty );
pInv->setItemPrice(usrInptPrc);
vInventory.push_back( pInv );
} // addProduceToInventory
// ----------------------------------------------------------------------------
// addBookToInventory()
void Inventory::addBookToInventory( std::vector<Inventory*>& inventory) {
std::string usrInptName = "";
std::string usrInptQntyStr = "";
std::istringstream inSS;
int usrInptQnty = 0;
std::string usrInptAthr = "";
std::string usrInptPrcStr = "";
int usrInptPrc = 0;
std::istringstream inDD;
int sum = 0;
int itemCost = 0;
std::cout << "Enter name of new book: ";
getline( std::cin, usrInptName );
std::cout << "Enter quantity: ";
getline( std::cin, usrInptQntyStr );
inSS.str( usrInptQntyStr );
inSS >> usrInptQnty;
inSS.clear();
std::cout << "Enter author: ";
getline( std::cin, usrInptAthr );
std::cout << "Enter the price per item: $";
getline( std::cin, usrInptPrcStr );
inDD.str( usrInptPrcStr );
inDD >> usrInptPrc;
inDD.clear();
itemCost = usrInptPrc * usrInptQnty;
Inventory* pInv = nullptr; // Initialize pointers to nullptr;
pInv = new Inventory; // Using New Memory (Dyanamic) - Where Is This Being Deleted?
pInv->setName( usrInptName );
pInv->setQuantity( usrInptQnty );
pInv->setSumInv( usrInptPrc, usrInptQnty );
pInv->setAuthor( usrInptAthr );
pInv->setItemPrice( usrInptPrc );
inventory.push_back( pInv );
} // addBookToInventory
// ----------------------------------------------------------------------------
// updateItemQtyInInventory()
// This is the update function in which we can change how many items a certain purchase has
void Inventory::updateItemQtyInInventory( std::vector<Inventory*>& vInventory ) {
std::string usrIndexChoiceStr = "";
unsigned int usrIndexChoice = 0;
std::istringstream inSS;
std::string usrInptQntyStr = "";
int usrInptQnty = 0;
if ( vInventory.size() == 0 ) {
std::cout << "No items to update." << std::endl;
} else {
printInventory( vInventory );
do {
std::cout << "Update which item #: ";
getline( std::cin, usrIndexChoiceStr );
inSS.str( usrIndexChoiceStr );
inSS >> usrIndexChoice;
inSS.clear();
} while ( !(usrIndexChoice < vInventory.size()) );
std::cout << "Enter new quantity: ";
getline( std::cin, usrInptQntyStr );
inSS.str( usrInptQntyStr );
inSS >> usrInptQnty;
inSS.clear();
vInventory.at(usrIndexChoice)->setQuantity(usrInptQnty);
}
} // updateItemQtyInInventory
// ----------------------------------------------------------------------------
// removeItemFromInventory()
// Here we will be removing an entire item from the inventory
void Inventory::removeItemFromInventory( std::vector<Inventory*>& vInventory) {
std::istringstream inSS;
std::string usrIndexChoiceStr = "";
unsigned int usrIndexChoice = 0;
std::string usrInptQntyStr = "";
if ( vInventory.size() == 0 ) {
std::cout << "No items to remove." << std::endl;
} else {
printInventory( vInventory );
do {
std::cout << "Remove which item #: ";
getline( std::cin, usrIndexChoiceStr );
inSS.str( usrIndexChoiceStr );
inSS >> usrIndexChoice;
inSS.clear();
} while ( !(usrIndexChoice < vInventory.size()) );
vInventory.erase( vInventory.begin() + usrIndexChoice );
}
} // removeItemFromInventory
<强>的main.cpp 强>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
//using namespace std; // Bad Practice To Use This In A Global Scope!
#include "Inventory.h"
// ----------------------------------------------------------------------------
// main()
int main() {
std::vector<Inventory*> vInventory;
std::string strUsrInptOptn = "default";
std::string strUsrInptOptn2 = "default";
Inventory update;
while (true) {
// Get user choice
std::cout << "\nEnter (p)rint, (a)dd, (u)pdate, (r)emove, or (q)uit: ";
getline( std::cin, strUsrInptOptn);
// Process user choice
if ( strUsrInptOptn.size() == 0 ) {
continue;
} else if ( strUsrInptOptn.at(0) == 'p' ) {
update.printInventory( vInventory ); //Different!
} else if ( strUsrInptOptn.at(0) == 'a' ) { ///I don't know what the difference is between the three slashes and the two, but they are different!
std::cout << "\nEnter (b)ook or (p)roduce: ";
getline( std::cin, strUsrInptOptn2 );
if ( strUsrInptOptn2.at(0) == 'b' ) {
update.addBookToInventory( vInventory ); //Supposed to look like: INV = AddItem...(INV);
} else if ( strUsrInptOptn2.at(0) == 'p' ) {
update.addProduceToInventory( vInventory );
} else {
continue;
}
} else if ( strUsrInptOptn.at(0) == 'u' ) {
update.updateItemQtyInInventory( vInventory );
} else if ( strUsrInptOptn.at(0) == 'r' ) {
update.removeItemFromInventory( vInventory );
} else if ( strUsrInptOptn.at(0) == 'q') {
std::cout << "\nGood bye." << std::endl;
break;
}
} // while
return 0;
} // main
有了这个代码,看起来更干净,优雅的感觉。 main.cpp
没有大量的代码杂乱;保持main.cpp
简短和简单始终是一种好习惯,因为这是主要可执行文件的开始。我创建了一个库存类,并将其放在*.h
和*.cpp
文件中的自己的模块中。我还将其声明与其定义或实施分开。这样,如果其他人正在查看您的源代码以使用它;他们不必确切地知道该功能是如何工作的,只是它应该做的事情。接口或*.h
文件应告诉您有关类对象及其使用方式的所有信息。此外,这样可以实现更清晰,更快速的构建时间,因为只有在更改内容而不是重建整个解决方案时才需要编译此类的*.cpp
文件。
现在有些情况下你可能希望在头文件中有实现,有些情况下你没有选择而且必须这样。如果您的方法声明为inline
或者您正在创建仅标头库,那么您可能希望实现位于头文件中的情况。如果您要创建class templates
或function templates
,那么您可能无法选择但是在头文件中有这种情况的情况,但是当您拥有inline member functions
,{{1}这两种类型时或者class template member functions
这些通常位于内联文件或function templates
中,您可以在类声明之外和{{1}之前使用*.inl
指令包含此文件头卫;如果您使用的是#include
而不是#endif
,那么您只需在课程声明后使用此内容。
一旦开始拥有您喜欢的代码的常量结构;它将变得更容易调试和发现错误。
你的问题的一面我想我已经注意到你引入的另一个你可能会或可能不会注意到的错误;我想你的程序可能有内存泄漏!
我希望这可以作为帮助您的指南。如果我在接下来的几天内有机会;我可以继续尝试调试您的代码或尝试以更优雅的方式重写您的程序。如果我确实绕过它,我会将其作为另一个答案发布。
答案 1 :(得分:0)
几个问题: 1.)priceInDollars未正确初始化 2.)totalInvPriceInDollars需要是静态的
下面的代码修复了问题:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int totalInvPriceInDollars = 0 ;
class Inventory {
public:
Inventory(): priceInDollars(0) {};
void SetSumInv(int prcInDllrs, int individualquantity) {
priceInDollars = priceInDollars + (prcInDllrs * individualquantity);
totalInvPriceInDollars = totalInvPriceInDollars + priceInDollars;
}
void SetItemPrice(int whatever) {
itemsPrice = whatever;
}
void SetName(string nm)
{
name = nm;
};
void SetQuantity(int qnty)
{
quantity = qnty;
};
void SetAuthor(string athr) {
author = athr;
}
void SetExpiration(string expir)
{
expiration = expir;
};
virtual void Print(){
cout << name << " x" << quantity << " for: $" << itemsPrice; //" (Expires: " << expiration << ")";
if (expiration.size() != 0) {
cout << " (Expires: " << expiration << ")" << endl;
}
else {
cout << " (Author: " << author << ")" << endl;
}
}
void PrintInventory(vector<Inventory*> inventory) {
unsigned int i = 0;
if (inventory.size() == 0) {
cout << "No items to print." << endl;
}
else {
for (i = 0; i<inventory.size(); ++i) {
cout << i << " - ";
inventory.at(i)->Print();
}
cout << "Total inventory value: " << totalInvPriceInDollars;
}
return;
}
void AddItemToInventory()
{
}
vector<Inventory*> AddProduceToInventory(vector<Inventory*> inventory)
{
Inventory* prdc;
string usrInptName = "";
string usrInptQntyStr = "";
istringstream inSS;
istringstream inDD;
int usrInptQnty = 0;
string usrInptExpr = "";
string usrInptPrcStr = "";
int usrInptPrc = 0;
int ItemCost = 0;
cout << "Enter name of new produce: ";
getline(cin, usrInptName);
SetName(usrInptName);
cout << "Enter quantity: ";
getline(cin, usrInptQntyStr);
inSS.str(usrInptQntyStr);
inSS >> usrInptQnty;
inSS.clear();
SetQuantity(usrInptQnty);
cout << "Enter expiration date: ";
getline(cin, usrInptExpr);
SetExpiration(usrInptExpr);
cout << "Enter the price per item: $";
getline(cin, usrInptPrcStr);
inDD.str(usrInptPrcStr);
inDD >> usrInptPrc;
inDD.clear();
SetItemPrice(usrInptPrc);
ItemCost = (usrInptPrc * usrInptQnty);
prdc = new Inventory;
prdc->SetName(usrInptName);
prdc->SetQuantity(usrInptQnty);
prdc->SetExpiration(usrInptExpr);
prdc->SetSumInv(usrInptPrc, usrInptQnty);
prdc->SetItemPrice(usrInptPrc);
inventory.push_back(prdc);
return inventory;
}
void AddBookToInventory()
{
}
vector<Inventory*> AddBookToInventory(vector<Inventory*> inventory) {
Inventory* prdct;
string usrInptName = "";
string usrInptQntyStr = "";
istringstream inSS;
int usrInptQnty = 0;
string usrInptAthr = "";
string usrInptPrcStr = "";
int usrInptPrc = 0;
istringstream inDD;
int sum = 0;
int ItemCost = 0;
cout << "Enter name of new book: ";
getline(cin, usrInptName);
cout << "Enter quantity: ";
getline(cin, usrInptQntyStr);
inSS.str(usrInptQntyStr);
inSS >> usrInptQnty;
inSS.clear();
cout << "Enter author: ";
getline(cin, usrInptAthr);
cout << "Enter the price per item: $";
getline(cin, usrInptPrcStr);
inDD.str(usrInptPrcStr);
inDD >> usrInptPrc;
inDD.clear();
ItemCost = (usrInptPrc * usrInptQnty);
prdct = new Inventory;
prdct->SetName(usrInptName);
prdct->SetQuantity(usrInptQnty);
prdct->SetSumInv(usrInptPrc, usrInptQnty);
prdct->SetAuthor(usrInptAthr);
prdct->SetItemPrice(usrInptPrc);
inventory.push_back(prdct);
return inventory;
}
void UpdateItemQtyInventory()
{}
//This is the update function in which we can change how many items a certain purchase has
vector<Inventory*> UpdateItemQtyInInventory(vector<Inventory*> inventory) {
string usrIndexChoiceStr = "";
unsigned int usrIndexChoice = 0;
istringstream inSS;
string usrInptQntyStr = "";
int usrInptQnty = 0;
if (inventory.size() == 0) {
cout << "No items to update." << endl;
}
else {
PrintInventory(inventory);
do {
cout << "Update which item #: ";
getline(cin, usrIndexChoiceStr);
inSS.str(usrIndexChoiceStr);
inSS >> usrIndexChoice;
inSS.clear();
} while (!(usrIndexChoice < inventory.size()));
cout << "Enter new quantity: ";
getline(cin, usrInptQntyStr);
inSS.str(usrInptQntyStr);
inSS >> usrInptQnty;
inSS.clear();
inventory.at(usrIndexChoice)->SetQuantity(usrInptQnty);
}
return inventory;
}
void RemoveItemFromInventory()
{}
//Here we will be removing an entire item from the inventory
vector<Inventory*> RemoveItemFromInventory(vector<Inventory*> inventory) {
istringstream inSS;
string usrIndexChoiceStr = "";
unsigned int usrIndexChoice = 0;
string usrInptQntyStr = "";
if (inventory.size() == 0) {
cout << "No items to remove." << endl;
}
else {
PrintInventory(inventory);
do {
cout << "Remove which item #: ";
getline(cin, usrIndexChoiceStr);
inSS.str(usrIndexChoiceStr);
inSS >> usrIndexChoice;
inSS.clear();
} while (!(usrIndexChoice < inventory.size()));
inventory.erase(inventory.begin() + usrIndexChoice);
}
return inventory;
}
void GetTotalValueAsPrice()
{
}
protected:
string name;
int quantity;
int priceInDollars ;
int totalCost;
int itemsPrice;
string expiration;
string author;
};
int main() {
vector<Inventory*> INVENTORY;
string usrInptOptn = "default";
string usrInptOptn2 = "default";
Inventory update;
while (true) {
// Get user choice
cout << "\nEnter (p)rint, (a)dd, (u)pdate, (r)emove, or (q)uit: ";
getline(cin, usrInptOptn);
// Process user choice
if (usrInptOptn.size() == 0) {
continue;
}
else if (usrInptOptn.at(0) == 'p') {
update.PrintInventory(INVENTORY); //Different!
}
else if (usrInptOptn.at(0) == 'a') {///I don't know what the difference is between the three slashes and the two, but they are different!
cout << "\nEnter (b)ook or (p)roduce: ";
getline(cin, usrInptOptn2);
if (usrInptOptn2.at(0) == 'b') {
INVENTORY = update.AddBookToInventory(INVENTORY); //Supposed to look like: INV = AddItem...(INV);
}
else if (usrInptOptn2.at(0) == 'p') {
INVENTORY = update.AddProduceToInventory(INVENTORY);
}
else
{
continue;
}
}
else if (usrInptOptn.at(0) == 'u') {
INVENTORY = update.UpdateItemQtyInInventory(INVENTORY);
}
else if (usrInptOptn.at(0) == 'r') {
INVENTORY = update.RemoveItemFromInventory(INVENTORY);
}
else if (usrInptOptn.at(0) == 'q') {
cout << "\nGood bye." << endl;
break;
}
}
return 0;
}