分配帮助更新特定城市中"shipments"和"orders"的每项项目的数量

Assignment help updating amounts of each item for "shipments" and "orders" in a specific city

本文关键字:orders 项目 shipments 更新 帮助 城市 分配      更新时间:2023-10-16

我的任务要求我在一个名为"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;
}