c++ - 尝试打印数组元素返回 “Access violation reading location 0xCCCCCCCC. occurred”

标签 c++ arrays exception memory-management

这段代码是针对一个项目的,当给定这样的文本文件时:

14.99 24 Hat
29.99 31 Shirt
17.99 12 Shorts
5.50 18 Socks
-1 -1 endofdata

应该打印出某种“收据”,但是当我尝试打印array [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;
}

最佳答案

您的readInventory()函数固有存在缺陷。您将返回一系列产品的初始产品。如果要返回整个数组,则需要使readInventory返回prod *,然后从return *array更改为return array。意思是,通过将&temp传递给writeReport(),您传递的是1个产品的数组,这当然会导致读取访问冲突。

关于c++ - 尝试打印数组元素返回 “Access violation reading location 0xCCCCCCCC. occurred”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61002351/

相关文章:

ASP.Net MissingMethodException - "ctor"方法未找到

c++ - PCL估计某些部分的法线方向错误

c++ - 如何将本地端点绑定(bind)到套接字?

c++ - ifstream.open()在Eclipse CDT中设置failbit?

javascript,循环数组,仅拉第一项

java - ClassCastException : java. lang.Object[] 无法转换为 java.lang.Double

c++ - 为什么必须在哪里放置 “template”和 “typename”关键字?

C:二维数组的嵌套循环给出了意想不到的结果

arrays - 如何在 swift 4 中计算数组的所有模式?

c - Obj-C 中的异常处理