分配帮助更新特定城市中 "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
我能够编写读取每一行文本和数字并区分 "shipments" 和 "orders"
的代码
#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;
}
我的作业要求我在名为 "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
我能够编写读取每一行文本和数字并区分 "shipments" 和 "orders"
的代码 #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;
}