分配帮助更新特定城市中"shipments"和"orders"的每项项目的数量
Assignment help updating amounts of each item for "shipments" and "orders" in a specific city
我的任务要求我在一个名为"ITEMRECORD.txt"的文件中创建一个包含记录列表的文件,我在下面有:
type warehouse amt1 amt2 amt3
-----------------------------
s NewYork 23 14 1
s Miami 25 25 25
s LosAngeles 40 13 17
s Houston 100 30 10
s Chicago 42 23 19
s NewYork 0 0 15
s Miami 13 17 21
o LosAngeles 15 10 15
o NewYork 12 24 8
o Houston 75 45 10
o Chicago 20 15 15
o NewYork 15 0 0
s LosAngeles 10 20 10
s Houston 0 30 40
o NewYork 15 15 25
o Chicago 75 30 40
s NewYork 20 15 20
o Houston 10 20 10
我能够编写代码,阅读每一行的文本和数字,并区分"发货"answers"订单"
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
ifstream itemRec;
struct record
{
char type;
string warehouse[5] = {"NewYork", "LosAngeles", "Miami", "Houston", "Chicago"};
int amt[3] = {0,0,0};
};
struct price {
double price;
};
int main ()
{
itemRec.open("ITEMRECORD.txt");
record records;
price item[3];
string city1;
int amt1, amt2, amt3;
if(!itemRec)
{
cout << "Cannot open the record of items file!! Check file please!" << endl;
}
for (int i=0; i<3; i++)
{
itemRec >> item[i].price;
}
while (itemRec >> records.type)
{
if (records.type == 's') // READING IN A SINGLE SHIPMENT CARD
{
itemRec >> city1;
for(int i=0; i<5; i++)
{
if (city1 == records.warehouse[i])
{
itemRec >> records.amt[0];
itemRec >> records.amt[1];
itemRec >> records.amt[2];
}
}
cout << records.type <<"t" << city1 << "t" << records.amt[0] << "t" << records.amt[1] << "t" << records.amt[2] << endl;
}
else if (records.type == 'o') // READING IN A SINGLE ORDER CARD
{
itemRec >> city1;
for(int i=0; i<5; i++)
{
if (city1 == records.warehouse[i])
{
itemRec >> records.amt[0];
itemRec >> records.amt[1];
itemRec >> records.amt[2];
}
}
cout << records.type <<"t" << city1 << "t" << records.amt[0] << "t" << records.amt[1] << "t" << records.amt[2] << endl;
}
}
然而,我遇到的问题是,因为每张卡都被读入以更新每个仓库中的物品数量。例如,第一行是s NewYork 23 14 1
当它看到s NewYork 0 0 15
时,我希望程序将amt1、amt2、amt3中的数字分别更新为23 14 16
。
当发现特定城市的金额时,我如何开始编写代码来更新这些金额?
无论你找到什么,你都会立即分配它,并删除旧值:
if (city1 == records.warehouse[i])
{
itemRec >> records.amt[0];
itemRec >> records.amt[1];
itemRec >> records.amt[2];
}
所以改为这样做:
if (city1 == records.warehouse[i])
{
itemRec >> temp1; // declare a place holder
itemRec >> temp2;
itemRec >> temp3;
records.amt[0] += temp1; // add the holder's value to the current value
records.amt[1] += temp2;
records.amt[2] += temp3;
}
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 既然存在危险,为什么项目要使用-I include开关
- cmake在我的项目中所需的所有静态库都不成功
- QT通过C++添加映射QML项目
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在linux上调试巨大的C++项目
- 在其他文件中创建类时在 c++ 项目中不起作用
- 使外部项目可用于find_package CMake
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- 如何维护资源管理器项目视图中当前可见的项目列表
- 错误-我无法在VS2019中打开新的Qt项目
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗