程序不会打印字符串数组C++
Program won't Print a string array C++
我已经在这个程序上工作了一段时间,但是它拒绝在这最后一点上合作。该程序的重点是将数据文件筛选为三个数组,对数组进行排序,然后将它们打印到一个表中。我的问题似乎出在桌子上。该程序分为四个函数,当我尝试调试时,它不会在函数中显示productName数组。
出现故障的代码段如下所示:
void printReport (string productName[], int numberinStock[], float price[], int number_of_products)
{
float totalPrice;
cout << setw(18) << " " << "Friendly Grocer Store Inventory" << setw(17) << " " << endl;
cout << setw(18) << "Inventory Item" << setw(16) << "Number in Stock" << setw(16) << "Unit Price" << setw(16) << "Total Sales" << endl;
for (int count=0; count <number_of_products-1; count++)
{
cout << setw(18) << productName[count] << setw(16) << numberinStock[count] << setw(16) << std::setprecision(2) << price[count] << setw(16) << std::fixed << std::setprecision(2) << price[count]*numberinStock[count] << endl;
}
cout << "Total Price: " << totalPrice;
}
它将打印其他所有内容,但不打印productName
。在for循环之外的一些调试语句,如cout << productName[1]
,将打印出适当的productName
,但在实际报告中它完全是空白的。经过一些调试之后,似乎在for循环中打印productName
之后的每个项都覆盖了产品名称。
例如只留下cout << setw(18) << productName[count] << setw(16) << numberinStock[count] << endl;
将产生
" 3 s "
" 10 h"
" 2a"
产品名称有芒果、三明治和披萨。
我很茫然。我哪里搞砸了?
您可能把数据传递给函数时搞砸了。如果在函数中设置了测试数组,则应该可以正确打印。
在c++中使用arrayname传递数组。如
int main ()
{
string productName[] = {"mango"};
...
printReport(productName, numofProduct);
return 0;
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '