需要 7 个并行向量的 C++ 程序(用于学校)存在问题
Posted
技术标签:
【中文标题】需要 7 个并行向量的 C++ 程序(用于学校)存在问题【英文标题】:Having problems with a C++ program (for school) that requires 7 parallel Vectors 【发布时间】:2021-04-29 03:08:49 【问题描述】:实验室问题:
编写一个程序来跟踪五金店的库存。商店有卖 各种物品。对于商店中的每件商品,都会保留以下信息: 商品ID、商品名称、订购件数、当前件数 在商店中,售出的件数,商品的制造商价格,以及 商店的售价。每个周末,店长都会 希望看到以下形式的报告:
友好的五金店 itemID itemName pOrdered pinStore pSold manufPrice sellPrice 4444 圆锯 150 150 40 45.00 125.00 3333 灶具 50 50 20 450.00 850.00 . . . 总库存:$#########.## 店内商品总数:_____________
...
输入格式:
项目ID 项目名称 pOrdered manufPrice sellPrice
使用七个并行向量来存储信息。该程序必须包含在 至少有以下功能:一个将数据输入到向量中,一个用于显示 菜单,一个用于销售商品,一个用于为经理打印报告。
我的代码:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
// Function Prototypes
void getInventory (ifstream& infile, vector<string>& itemID, vector<string>& itemName,
vector<int>& pOrdered, vector<int>& pInStore, vector<int>& pSold,
vector<double>& manufPrice, vector<double>& sellingPrice);
void displayMenu (vector<string>& itemID, vector<string>& itemName, vector<int>& pOrdered,
vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice,
vector<double>& sellingPrice);
void sellItem (vector<string>& itemID, vector<string>& itemName, vector<int>& pOrdered,
vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice,
vector<double>& sellingPrice);
void searchItem (vector<string>& itemID, vector<string>& itemName, vector<int>& pOrdered,
vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice,
vector<double>& sellingPrice);
void printReport (vector<string>& itemID, vector<string>& itemName, vector<int>& pOrdered,
vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice,
vector<double>& sellingPrice);
//Driver Function: Open File, Get Inventory, Display Menu, Close File.
int main()
//Vectors
vector<int> itemQuantity;
vector<string> itemID;
vector<string> itemName;
vector<int> pOrdered;
vector<int> pInStore;
vector<int> pSold;
vector<double> manufPrice;
vector<double> sellingPrice;
// External Inventory File
ifstream infile;
infile.open("inventory.txt");
if (!infile)
cout << "Error opening inventory file! " << endl;
return 1;
getInventory (infile, itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
displayMenu (itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
infile.close();
return 0;
// Get Inventory Definition
void getInventory (ifstream& infile, vector<string>& itemID, vector<string>& itemName, vector<int>& pOrdered,
vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice, vector<double>& sellingPrice)
// Declaring some Temp variables.
string item;
string name;
int ordered;
double mPrice;
double sPrice;
while (infile)
infile >> item;
infile.ignore(100,'\n'); // process the new-line character
getline(infile,name);
infile >> name >> ordered >> mPrice >> sPrice;
itemID.push_back(item);
itemName.push_back(name);
pOrdered.push_back(ordered);
manufPrice.push_back(mPrice);
sellingPrice.push_back(sPrice);
pInStore = pOrdered;
// Display Menu Definition
void displayMenu (vector<string>& itemID, vector<string>& itemName, vector<int>& pOrdered,
vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice,
vector<double>& sellingPrice)
char menuAnswer;
cout << "Type 'C' to check if an item is in stock." << endl;
cout << "Type 'S' to sell an item to a customer." << endl;
cout << "Type 'R' to print an inventory report." << endl;
cin >> menuAnswer;
if (menuAnswer == 'C' || menuAnswer == 'c')
searchItem(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
if (menuAnswer == 'S' || menuAnswer == 's')
sellItem(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
if (menuAnswer == 'R' || menuAnswer == 'r')
printReport(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice);
// Sell Item
void sellItem (vector<string>& itemID, vector<string>& itemName, vector<int>& pOrdered,
vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice,
vector<double>& sellingPrice)
int itemNumber;
int amtBought;
double cash;
char verifyItem;
int i;
cout << "Enter, or scan the Item ID. ";
cin >> itemNumber;
i = itemNumber;
if (i != -1)
cout << "You are selling " << itemName[i] << ", which costs $"
<< sellingPrice[i] << "Is this correct? (Y/N)" << endl;
cout << endl;
cin >> verifyItem;
if (verifyItem == 'N' || verifyItem == 'n')
cout << "What item number is being purchased? " << endl;
cin >> itemNumber;
cout << "How many items are being purchased? " << endl;
cin >> amtBought;
if (amtBought < 1)
cout << "None of this item is being purchased!" << endl;
else
if (pInStore[i] < amtBought)
cout << "Only " << pInStore[i] << " of these items can be sold." << endl;
else
cout << "The total price of this transaction is " << amtBought * sellingPrice[i] << endl;
cout << "Enter amount given." << endl;
cin >> cash;
pInStore[i] = pInStore[i] - amtBought;
cout << "The change is $" << cash - (amtBought * sellingPrice[i]) << endl;
cout << "Have a nice day!" << endl;
// Search Item Definition
void searchItem (vector<string>& itemID, vector<string>& itemName, vector<int>& pOrdered,
vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice,
vector<double>& sellingPrice)
int number;
cout << "Which item would you like to check?" << endl;
cout << endl;
cin >> number;
for (number = 0; number < 5; number++)
if (pInStore[number] > 0)
cout << "There are " << pInStore[number] << "of that item available." << endl;
// Print Report Definition
void printReport (vector<string>& itemID, vector<string>& itemName, vector<int>& pOrdered,
vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice,
vector<double>& sellingPrice)
unsigned int i;
int totalItems = 0;
double totalInventory = 0;
cout << " AJ's Hardware Store " << endl << endl;
cout << "itemID ItemName pOrdered pInStore pSold manufPrice sellingPrice" << endl;
cout << fixed << showpoint;
cout << setprecision(2);
for (i = 0; i < itemID.size(); i++)
cout << left;
cout << setw(7) << itemID.at(i);
cout << setw(15) << itemName.at(i);
cout << right;
cout << setw(8) << pOrdered.at(i);
cout << setw(9) << pInStore.at(i);
cout << setw(6) << pSold.at(i);
cout << setw(11) << manufPrice.at(i);
cout << setw(13) << sellingPrice.at(i) << endl;
totalInventory += pInStore.at(i) * sellingPrice.at(i);
totalItems += pInStore.at(i);
cout << endl;
cout << "Total Inventory: $" << totalInventory << endl;
cout << "Total number of items in the store: " << totalItems << endl;
我遇到了一个从未遇到过的分段错误,而本章(第 16.23 章 Malik 第 8 版)对我来说是新的。我认为我尝试加载向量的方式有问题,我似乎无法弄清楚。
在此先感谢您的任何指导。
【问题讨论】:
您在代码中的任何位置调用push_back
、insert
、emplace_back
或resize
以适当调整向量的大小?使用[]
,向量不会自行增长。请注意,拥有七个并行阵列并不好。相反,将使用包含七个向量的单个 struct
或 class
。
Doh,好吧,所以我想我把它当成一个数组来对待了。好吧,我假设,当我接受输入时,我可以使用 .push_back?哪个,它本身不会更新向量的大小吗?在 getInventory 函数中?
这就是为什么你应该从一个很小的main program开始并练习基本的向量运算,而不是写完所有这些代码并意识到向量的基本运算是错误的。
在 getInventory 中你需要类似while(inFile >> col1 >> col2 >> ...) v1.push_back(col1); v2.push_back(col2); ...
@PaulMcKenzie 你是对的。我从字面上选择了额外的信用问题,而不是先练习简单的元素。我会做一些练习并重新审视这个!
【参考方案1】:
分段错误错误是因为您正在访问不允许(或尚未考虑)的内存。考虑使用“push_back”(或我不知道的其他一些函数)将项目插入向量。这就是向向量添加新项目的方式。
你的这部分代码没有任何意义:
...
cin >> number;
for (number = 0; number < 5; number++)
if (pInStore[number] > 0)
cout << "There are " << pInStore[number] << "of that item available." << endl;
...
我还建议制作如下所示的结构类型,然后创建它的向量。因为您需要知道向量的项与另一个项中的哪个项相关:
struct itemName
int itemName;
int itemID; // or something that is unique for each item( product )
通过这种方式,您可以在项目(产品)的不同属性之间建立关系。
【讨论】:
谢谢!我已经更新了我的代码,删除了一些东西,获得了一个(有点)工作功能,使用了临时变量,但在进行项目搜索时仍然遇到了那个段和一些奇怪的东西。 @iamjarvi 你试过调试你的代码吗?以上是关于需要 7 个并行向量的 C++ 程序(用于学校)存在问题的主要内容,如果未能解决你的问题,请参考以下文章