Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program for a retail store that only sells three (category) types of mer

ID: 3539252 • Letter: W

Question

Write a program for a retail store that only sells three (category) types of merchandises: Appliance, Kitchenware and Tool. This program shall use a structure to store the following inventory data in a file, test.dat:
Item Description


The program should have a menu (Ch4.10 Menu, p.190) that allows the user to perform the following tasks:


Input Validation: The program should not accept quantities, or wholesale or retail costs, less than 0. The program should not accept dates that the programmer determines are unreasonable.

The store would be very pleased to have a properly working program, no need to optimize for performance.



#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

const int SKU_SIZE = 31,
        CAT_SIZE = 31,
        DATE_SIZE = 31;

struct Inventory
{
    char SKU[SKU_SIZE];
    char category[CAT_SIZE];
    int qty;
    double cost;
    char date[CAT_SIZE];
};

void enterRecords(fstream &);
void displayRecords(fstream &);
void changeRecords(fstream &);
void wholesaleItem(fstream &);
void wholesaleCat(fstream &);
void qtyAll(fstream &);

int main()
{
    int menu;
    const int enter = 1,
            display = 2,
            change = 3,
            item = 4,
            cat = 5,
            all = 6,
            quit = 7;   
    Inventory Info = {"", "", 0, 0.0, ""};
    fstream Inventory("test.dat", ios::out | ios::binary);
    for(int i = 0; i < 31; i++)
        Inventory.write(reinterpret_cast<char *>(&Info), sizeof(Info));
    Inventory.close();   
    Inventory.open("test.dat", ios::out | ios::binary);
    do
    {
        cout << "1) Add New Records" << endl;
        cout << "2) Display Any Record" << endl;
        cout << "3) Change Any Record" << endl;
        cout << "4) Display Total Wholesale Value of Item Inventory" << endl;
        cout << "5) Display Total Wholesale Value of a Particular Category Inventory" << endl;
        cout << "6) Display Total Quantity of All Items In the Inventory" << endl;
        cout << "7) Quit" << endl;
        cout << "Please Make Your Selection" << endl;
        cin >> menu;
       
        while(menu < enter || menu > quit)
        {
            cout << "Please Make Your Selection" << endl;
            cin >> menu;
        }
       
        switch(menu)
        {
            case enter:
                enterRecords(Inventory);
                break;
            case display:
                displayRecords(Inventory);
                break;
            case change:
                changeRecords(Inventory);
                break;
            case item:
                wholesaleItem(Inventory);
                break;
            case cat:
                wholesaleCat(Inventory);
                break;
            case all:
                qtyAll(Inventory);
                break;
        }
    }while(menu != quit);
    Inventory.close();
    return 0;
}

void enterRecords(fstream &)
{
    cout << "Enter Following Inventory Data" << endl;
    Inventory Info;
    fstream Inventory("test.dat", ios::out | ios::binary | ios::app);
    cout << "SKU (Stock Keeping Unit): ";
    cin.ignore();
    cin.getline(Info.SKU, SKU_SIZE);
    cout << "Category: ";
    cin >> Info.category;
    cout << "Quantity On Hand: ";
    cin >> Info.qty;
    cout << "Wholesale Cost: ";
    cin >> Info.cost;
    cout << "Date Added To Inventory: ";
    cin >> Info.date;
    Inventory.write(reinterpret_cast<char *>(&Info), sizeof(Info));
    cout << "Record Added To File" << endl << endl;
    if(Inventory.fail())
        Inventory.clear();
    Inventory.close();
}

void displayRecords(fstream &)
{
    Inventory Info;
    fstream Inventory("test.dat", ios::in | ios::binary);
    int numRec;
    cout << "Enter the Record Number of Item to View: ";
    cin >> numRec;
    Inventory.seekg(numRec * sizeof(Info), ios::beg);
    Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));
   
    cout << "SKU (Stock Keeping Unit): " << Info.SKU << endl;
    cout << "Category: " << Info.category << endl;
    cout << "Quantity On Hand: " << Info.qty << endl;
    cout << "Wholesale Cost: " << Info.cost << endl;
    cout << "Date Added To Inventory: " << Info.date << endl << endl;   
    if(Inventory.fail())
        Inventory.clear();
    Inventory.close();
}
       
void changeRecords(fstream &)
{
    Inventory Info;
    fstream Inventory("test.dat", ios::out | ios::binary | ios::app);
    int numRec;
    cout << "Enter the Record Number of Item to Change: ";
    cin >> numRec;
    cout << "SKU (Stock Keeping Unit): ";
    cin.ignore();
    cin.getline(Info.SKU, SKU_SIZE);
    cout << "Category: ";
    cin >> Info.category;
    cout << "Quantity On Hand: ";
    cin >> Info.qty;
    cout << "Wholesale Cost: ";
    cin >> Info.cost;
    cout << "Date Added To Inventory: ";
    cin >> Info.date;
    Inventory.seekp(numRec * sizeof(Info), ios::beg);
    Inventory.write(reinterpret_cast<char *>(&Info), sizeof(Info));
    if(Inventory.fail())
        Inventory.clear();
    Inventory.close();
}
   
void wholesaleItem(fstream &)
{
    Inventory Info;
    fstream Inventory("test.dat", ios::in | ios::binary);
    if(Inventory.fail())
        Inventory.clear();
    double wholesale = 0;
    Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));
    while(!Inventory.eof())
    {
        wholesale += Info.cost;
        Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));
    }
    cout << "The Total Wholesale Value of the Inventory is $" << wholesale << endl << endl;
    if(Inventory.fail())
        Inventory.clear();
    Inventory.close();
}

void wholesaleCat(fstream &)
{
    char cate[31];
    Inventory Info;
    fstream Inventory("test.dat", ios::in | ios::binary);
    if(Inventory.fail())
        Inventory.clear();
    double wholesale = 0;
    cout << "Please Enter Category To View Total Wholesale Value of: ";
    cin.ignore();
    cin.getline(cate, 31);
    Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));
    while(!Inventory.eof())
    {
        if(strcmp(Info.category, cate) == 0)
            wholesale += Info.cost;
        Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));
    }
    cout << "The Total Wholesale Value of Category " << cate << " is $" << wholesale << endl << endl;
    if(Inventory.fail())
        Inventory.clear();
    Inventory.close();
}

void qtyAll(fstream &)
{
    Inventory Info;
    fstream Inventory("test.dat", ios::in | ios::binary);
    int amt = 0;
    Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));
    while(!Inventory.eof())
    {
        amt += Info.qty;
        Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));
    }
    cout << "The Total Quantity of All Items In Inventory is " << amt << endl << endl;
    if(Inventory.fail())
        Inventory.clear();
    Inventory.close();
}




Everything works fine until I change a record. The total wholesale will keep the previous wholesale price and add the one i just changed. For example: 1) cost = 50... change to 1) cost = 100; the total cost would be 150, rather than just 100.


I have to submit my test.data, but it leaves unreadable content.
       
   

Explanation / Answer

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

const int SKU_SIZE = 31,
        CAT_SIZE = 31,
        DATE_SIZE = 31;

struct Inventory
{
    char SKU[SKU_SIZE];
    char category[CAT_SIZE];
    int qty;
    double cost;
    char date[CAT_SIZE];
};

void enterRecords(fstream &);
void displayRecords(fstream &);
void changeRecords(fstream &);
void wholesaleItem(fstream &);
void wholesaleCat(fstream &);
void qtyAll(fstream &);

int main()
{
    int menu;
    const int enter = 1,
            display = 2,
            change = 3,
            item = 4,
            cat = 5,
            all = 6,
            quit = 7;   
    Inventory Info = {"", "", 0, 0.0, ""};
    fstream Inventory("test.dat", ios::out | ios::binary);
    for(int i = 0; i < 31; i++)
        Inventory.write(reinterpret_cast<char *>(&Info), sizeof(Info));
    Inventory.close();   
    Inventory.open("test.dat", ios::out | ios::binary);
    do
    {
        cout << "1) Add New Records" << endl;
        cout << "2) Display Any Record" << endl;
        cout << "3) Change Any Record" << endl;
        cout << "4) Display Total Wholesale Value of Item Inventory" << endl;
        cout << "5) Display Total Wholesale Value of a Particular Category Inventory" << endl;
        cout << "6) Display Total Quantity of All Items In the Inventory" << endl;
        cout << "7) Quit" << endl;
        cout << "Please Make Your Selection" << endl;
        cin >> menu;
       
        while(menu < enter || menu > quit)
        {
            cout << "Please Make Your Selection" << endl;
            cin >> menu;
        }
       
        switch(menu)
        {
            case enter:
                enterRecords(Inventory);
                break;
            case display:
                displayRecords(Inventory);
                break;
            case change:
                changeRecords(Inventory);
                break;
            case item:
                wholesaleItem(Inventory);
                break;
            case cat:
                wholesaleCat(Inventory);
                break;
            case all:
                qtyAll(Inventory);
                break;
        }
    }while(menu != quit);
    Inventory.close();
    return 0;
}

void enterRecords(fstream &)
{
    cout << "Enter Following Inventory Data" << endl;
    Inventory Info;
    fstream Inventory("test.dat", ios::out | ios::binary | ios::app);
    cout << "SKU (Stock Keeping Unit): ";
    cin.ignore();
    cin.getline(Info.SKU, SKU_SIZE);
    cout << "Category: ";
    cin >> Info.category;
    cout << "Quantity On Hand: ";
    cin >> Info.qty;
    cout << "Wholesale Cost: ";
    cin >> Info.cost;
    cout << "Date Added To Inventory: ";
    cin >> Info.date;
    Inventory.write(reinterpret_cast<char *>(&Info), sizeof(Info));
    cout << "Record Added To File" << endl << endl;
    if(Inventory.fail())
        Inventory.clear();
    Inventory.close();
}

void displayRecords(fstream &)
{
    Inventory Info;
    fstream Inventory("test.dat", ios::in | ios::binary);
    int numRec;
    cout << "Enter the Record Number of Item to View: ";
    cin >> numRec;
    Inventory.seekg(numRec * sizeof(Info), ios::beg);
    Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));
   
    cout << "SKU (Stock Keeping Unit): " << Info.SKU << endl;
    cout << "Category: " << Info.category << endl;
    cout << "Quantity On Hand: " << Info.qty << endl;
    cout << "Wholesale Cost: " << Info.cost << endl;
    cout << "Date Added To Inventory: " << Info.date << endl << endl;   
    if(Inventory.fail())
        Inventory.clear();
    Inventory.close();
}
       
void changeRecords(fstream &)
{
    Inventory Info;
    fstream Inventory("test.dat", ios::out | ios::binary | ios::app);
    int numRec;
    cout << "Enter the Record Number of Item to Change: ";
    cin >> numRec;
    cout << "SKU (Stock Keeping Unit): ";
    cin.ignore();
    cin.getline(Info.SKU, SKU_SIZE);
    cout << "Category: ";
    cin >> Info.category;
    cout << "Quantity On Hand: ";
    cin >> Info.qty;
    cout << "Wholesale Cost: ";
    cin >> Info.cost;
    cout << "Date Added To Inventory: ";
    cin >> Info.date;
    Inventory.seekp(numRec * sizeof(Info), ios::beg);
    Inventory.write(reinterpret_cast<char *>(&Info), sizeof(Info));
    if(Inventory.fail())
        Inventory.clear();
    Inventory.close();
}
   
void wholesaleItem(fstream &)
{
    Inventory Info;
    fstream Inventory("test.dat", ios::in | ios::binary);
    if(Inventory.fail())
        Inventory.clear();
    double wholesale = 0;
    Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));

    wholesale=0;
    while(!Inventory.eof())
    {
        wholesale += Info.cost;
        Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));
    }
    cout << "The Total Wholesale Value of the Inventory is $" << wholesale << endl << endl;
    if(Inventory.fail())
        Inventory.clear();
    Inventory.close();
}

void wholesaleCat(fstream &)
{
    char cate[31];
    Inventory Info;
    fstream Inventory("test.dat", ios::in | ios::binary);
    if(Inventory.fail())
        Inventory.clear();
    double wholesale = 0;
    cout << "Please Enter Category To View Total Wholesale Value of: ";
    cin.ignore();
    cin.getline(cate, 31);
    Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));

    wholesale=0;
    while(!Inventory.eof())
    {
        if(strcmp(Info.category, cate) == 0)
            wholesale += Info.cost;
        Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));
    }
    cout << "The Total Wholesale Value of Category " << cate << " is $" << wholesale << endl << endl;
    if(Inventory.fail())
        Inventory.clear();
    Inventory.close();
}

void qtyAll(fstream &)
{
    Inventory Info;
    fstream Inventory("test.dat", ios::in | ios::binary);
    int amt = 0;
    Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));
    while(!Inventory.eof())
    {
        amt += Info.qty;
        Inventory.read(reinterpret_cast<char *>(&Info), sizeof(Info));
    }
    cout << "The Total Quantity of All Items In Inventory is " << amt << endl << endl;
    if(Inventory.fail())
        Inventory.clear();
    Inventory.close();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote