Attempt to print array element returns "Access violation reading location 0xCCCCCCCC. occurred"

Viewed 49

This code is for a project that when given a text file such as this:

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

should print out a "receipt" of sorts, but I am getting an exception at line 73 (put an all caps comment there) when I try to print array[i].name. I tried to change it to &array[i].name (along with other elements I try to print) and it prints the addresses just fine. I would really appreciate your help. Code is displayed below.

#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;
}
1 Answers

Your readInventory() function is inherently flawed. you're returning the initial product of an array of products. if you wanted to return the whole array, you'd need to make readInventory return prod* and change from return *array to just return array. Meaning, by passing &temp to writeReport() you're passing an array of 1 product, of course causing a read access violation.

Related