希望这将是我一段时间以来的最后一个问题。
我对为什么我的功能无法正常工作感到有些困惑。这是我的代码(稍后再解释):
#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;
}
所以现在。在我的班级清单,公共:void SetSumInv中,该程序执行的操作是获取用户输入,并汇总所有内容,因此在打印时,应打印清单,然后再打印清单的总和。价值(以美元为单位)。我已经尝试运行此程序,但它不会显示库存值的总和。怎么了我不知道这个。
谢谢!
最佳答案
几个问题:
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;
}
关于c++ - C++对函数和 vector 感到困惑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33839036/