2

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;
}
  • [0xCC means you're accessing uninitialized memory](https://stackoverflow.com/q/370195/995714) – phuclv Apr 03 '20 at 02:25

1 Answers1

1

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.

Nadpher
  • 186
  • 1
  • 10
  • Also you can't print a string without including – Nadpher Apr 02 '20 at 23:05
  • And don't use plain arrays, if you need const size array, use `std::array` and if you need array with changeable size, use `std::vector`. – Kerek Apr 02 '20 at 23:12
  • It is pointless to return an input argument in the first place. The function can simply be `void`. – rustyx Apr 02 '20 at 23:19
  • That is true. really i just wanted to find a solution and as soon as i found it i just put a fix together in a couple of seconds. of course one would need to settle down to design everything correctly but this is not codereview lol. – Nadpher Apr 02 '20 at 23:28
  • Thank you. It is my first question on here and your solution worked. We were not allowed to use vectors, so I had to stick with arrays. Is it in all cases that changing the function to a pointer allows you to return the value instead of the reference? – oneWhoLearns Apr 03 '20 at 00:16
  • Changing the function to return a pointer lets you return a pointer. if you want to return a reference, just put & next to the return type. When you want to return an array, you have to return a pointer of the array type, in this case prod*. that's because arrays are just pointers, after all. You might wanna read up on memory and c++ data types/structures. – Nadpher Apr 03 '20 at 06:58