函数调用另一个函数会给出错误的输出 C++
function calling another functions gives wrong output c++?
仅当参数中给出的 sku 字符串与 "inventory" 数组的成员匹配时,增量库存函数才会调用 "addProduct" 函数(数组类型为 *Product 且大小为 50)。我在构造函数中将数组初始化为 nullptr。"num"是增量数字。当我测试它并输入有效的 sku 来增加库存时,我从 addproduct 函数中得到"没有空间"。
void Supplier::addProduct(Product *p)
{
bool space = false;
int counter=0;
while(!space && counter < inventory.size() )
{
if(inventory[counter] == nullptr )
{
inventory[counter] = p;
space = true;
}
counter++;
}
if (!space)
{
cout << "no space" << endl;
}
}
void Supplier::incrementStock(const string &sku, int num)
{
bool found = false;
for( int i = 0; i < inventory.size(); i++ )
{
if( inventory[i] && sku == inventory[i]->getSKU())
{
found=true;
addProduct(inventory[i]);
inventory[i]->setQuantity(inventory[i]->getQuantity() +num);
}
}
if (found ==false)
{
cout << "not found" << endl;
}
}
考虑这个循环:
for( int i = 0; i < inventory.size(); i++ )
如果您在此循环中获得 SKU 匹配项,则会将该商品的额外副本添加到库存中。 这有点奇怪,但如果你想要在库存中拥有同一指针的多个副本,那很好。
问题是,在循环的那次迭代之后,循环会继续,它也会找到我们刚刚做的副本,看到它匹配,然后再次制作另一个副本。 重复此操作,直到数组已满。
相关文章:
- 显示错误输出的简单数组排序程序
- Dijkstra 中的错误输出
- 自定义runtime_error,如果我在 #what 中使用#c_str(),则错误输出始终为空
- 流字符串错误输出
- C++ 异常处理错误输出
- C ++中的错误输出,i = 65530,当我们打印i的值时,它给出-6状态65530
- SSE和iostream:浮点类型的错误输出
- C++将 popen 错误输出读取为字符串
- 部分透视/高斯消除 - 交换列而不是产生错误输出的行
- C++,向量:获得包含 0 的错误输出
- 数组函数错误输出
- CC1plus 错误:输出文件名指定两次
- 字符串错误输出
- 子斯特的错误输出
- 对大量c++错误输出完全一无所知.我认为这与预定义的函数和迭代器有关
- 简单程序的错误输出
- XCode调试器lldb中断,没有错误输出,但程序运行良好
- std::string::append(std::string)错误输出
- 查看大错误输出的顶行
- 使用结构的错误输出