C++ 尝试打印数组元素返回值";访问冲突读取位置0xCCCC。“发生”;
此代码适用于以下项目:当给定文本文件时:C++ 尝试打印数组元素返回值";访问冲突读取位置0xCCCC。“发生”;,c++,arrays,exception,memory-management,C++,Arrays,Exception,Memory Management,此代码适用于以下项目:当给定文本文件时: 14.99 24 Hat 29.99 31 Shirt 17.99 12 Shorts 5.50 18 Socks -1 -1 endofdata 应该打印出某种“收据”,但当我尝试打印数组[I].name时,第73行出现了一个异常(在那里放置一个所有大写注释)。 我尝试将其更改为&array[I].name(以及我尝试打印的其他元素),它可以很好地打印地址。 我非常感谢你的帮助。代码显示在下面 #include <iostream> #i
14.99 24 Hat
29.99 31 Shirt
17.99 12 Shorts
5.50 18 Socks
-1 -1 endofdata
应该打印出某种“收据”,但当我尝试打印数组[I].name时,第73行出现了一个异常(在那里放置一个所有大写注释)。
我尝试将其更改为&array[I].name(以及我尝试打印的其他元素),它可以很好地打印地址。
我非常感谢你的帮助。代码显示在下面
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
struct prod {
string name;
float price;
int inStock;
};
void swapName(string* name1, string* name2) {
string temp = *name1;
*name1 = *name2;
*name2 = temp;
}
prod readInventory(prod array[], int max) {
ifstream inventoryF("inventory.txt");
if (inventoryF.fail()) {
cout << "Unable to open input file.\n";
for (int i = 0; i < 3; i++) {
array[i].price = 0;
array[i].inStock = 0;
array[i].name = " ";
}
}
else {
int i = 0;
while (array[i].price > 0) {
inventoryF>> array[i].price;
inventoryF >> array[i].inStock;
inventoryF >>array[i].name;
i += 1;
}
cout << "Inventory read."<< endl;
}
return *array;
}
float totalValue(prod array[]) {
int i = 0;
float total = 0;
while (array[i].price> 0) {
total+=array[i].price* array[i].inStock;
i++;
}
return total;
}
prod sortByName(prod array[]) {
for (int i = 0; i < 5; i++) {
if (array[i].name > array[i + 1].name) {
swapName(&array[i].name, &array[i + 1].name);
}
}
cout << "Poducts sorted by name.\n";
return *array;
}
void writeReport(prod array[],int max) {
cout <<setprecision(2)<< "+---------------------------+" << endl;
cout << "| Current Inventory |" << endl;
cout << "+---------------------------+" << endl;
cout << left << setw(15) << "NAME" << setw(12) << "PRICE" << "#" << endl;
cout << "------------ ------- ---" << endl;
int j = 0;
float total = totalValue(array);
for (int i =0;i< max;i++){
//PROBLEM IS ON THE LINE BELOW
cout << left << setw(15) << array[i].name << setw(2) << "$" << array[i].price<< right << array[i].inStock<< endl;
j++;
}
cout << "+---------------------------+" << endl;
cout << left << setw(22) << "Number of products:" << j << endl;
cout << setw(22) << "Inventory total value:" << total << endl;;
}
int main() {
const int prodMax = 20;
int current = 0;
prod productArray[prodMax];
prod temp = readInventory(productArray, prodMax);
//temp = sortByName(&temp);
writeReport(&temp, prodMax);
system("pause");
return 0;
}
#包括
#包括
#包括
使用名称空间std;
结构产品{
字符串名;
浮动价格;
int inStock;
};
void swapName(字符串*name1,字符串*name2){
字符串temp=*name1;
*name1=*name2;
*名称2=温度;
}
产品读取目录(产品数组[],int max){
ifstream inventoryF(“inventory.txt”);
if(inventoryF.fail()){
cout(0){
inventoryF>>数组[i]。价格;
inventoryF>>数组[i].inStock;
inventoryF>>数组[i]。名称;
i+=1;
}
cout数组[i+1]。名称){
swapName(&array[i].name,&array[i+1].name);
}
}
cout你的readInventory()
函数本身存在缺陷。您返回的是一系列产品的初始产品。如果您想返回整个阵列,则需要执行readInventory
return prod*并将return*array
更改为justreturn array
。意思是将&temp传递到writeReport()
您正在传递一个包含1个产品的数组,当然会导致读访问冲突。此外,如果您需要常量大小数组,请使用std::array
,如果您需要大小可变的数组,请使用std::vector
。在首先。函数可以是void
。这是真的。我真的只是想找到一个解决方案,一找到它,我就在几秒钟内完成了一个修复。当然,人们需要安定下来正确设计所有东西,但这不是codereview lol。谢谢。这是我关于这里和你的解决方案的第一个问题我们不允许使用向量,所以我必须坚持使用数组。是否在所有情况下,将函数更改为指针都允许您返回值而不是引用?