C++ C++;二维数组值增加

C++ C++;二维数组值增加,c++,arrays,C++,Arrays,我期待着完成这个课程。谈到数组,我就不知所措了,我已经阅读了所有的课程作业、书籍等。问题是如何在这个位置增加一个二维数组元素 int main() { int quantity, warehouse, product; int inventory[4][5] = { {900,400,250,95,153}, {52, 95, 625, 44, 250}, {100,720,301,50,878}, {325,650,57,445,584}, }; cout << "Ent

我期待着完成这个课程。谈到数组,我就不知所措了,我已经阅读了所有的课程作业、书籍等。问题是如何在这个位置增加一个二维数组元素

    int main()
{
 int quantity, warehouse, product;
int inventory[4][5] = {
{900,400,250,95,153},
{52, 95, 625, 44, 250},
{100,720,301,50,878},
{325,650,57,445,584},
};
cout << "Enter the warehouse number between 1 and 4: " << endl;
cin >> warehouse;
cout << "Enter the product location between 1 and 5: " << endl;
cin >> product;
cout << "Enter the quantity delivered: " << endl;
cin >> quantity;        

 /* First the addition */
for(warehouse = 0; warehouse < 4; warehouse++)
for(product = 0; product < 5; product++)
inventory[warehouse][product] + quantity;

cout << "The amount of units in warehouse " << warehouse << " is \n\n";


/* Then print the results */
for(warehouse = 0; warehouse < 4; warehouse++ ) {
                for( product = 0; product < 5; product++ )
                    cout << "\t" <<  inventory[warehouse][product];
                cout << endl;   /* at end of each warehouse */
}
 return 0;
}
intmain()
{
整数数量、仓库、产品;
国际库存[4][5]={
{900,400,250,95,153},
{52, 95, 625, 44, 250},
{100,720,301,50,878},
{325,650,57,445,584},
};
库特仓库;
cout产品;
产量;
/*首先是添加*/
用于(仓库=0;仓库<4;仓库++)
用于(产品=0;产品<5;产品++)
存货[仓库][产品]+数量;
库特
应该是

inventory[warehouse][product] += quantity;
//                            ^^
+
只返回加法运算,不修改任何操作数。
a+=b
a=a+b
同义

这里也不需要for循环。已经给出了值

for(warehouse = 0; warehouse < 4; warehouse++)
for(product = 0; product < 5; product++)
inventory[warehouse][product] + quantity;
请注意
+=
的使用。这实际上修改了数组中的值,而不仅仅是获取值并向其中添加
数量

接下来,您似乎只想打印与
仓库
对应的仓库的库存。为此,您不应该迭代所有仓库,而应该只迭代产品:

for( product = 0; product < 5; product++ ) {
  cout << "\t" <<  inventory[warehouse][product];
}
for(product=0;product<5;product++){

cout

/* First the Addition */
是不必要的,似乎您试图通过循环数组来获取要更改的索引。这是不必要的

inventory[warehouse][product] += quantity;

就是让程序正常工作所需的一切。它会将用户指定的数量添加到用户指定的索引中。

谢谢各位。这很有道理,而且我的程序工作得很好。我想知道为什么阅读有关这方面的书是浪费时间。
/* First the Addition */
inventory[warehouse][product] += quantity;