Answer to Question #306093 in C++ for Rival

Question #306093

Write a program in C++ that uses a structure to store the following product data in a file (File 

Name: product.txt):

Product ID

Product Name

Quantity 

Wholesale Cost

Retail Cost

Date Added to Product

The program should have a menu that allows the user to perform the following tasks:

• Add new records to the file.

• Display any record in the file.

The program should calculate and display the following data:

• The total wholesale value of the product

• The total retail value of the product

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.


1
Expert's answer
2022-03-05T04:11:22-0500
#include <iostream>
#include <fstream>
#include <cctype>

using namespace std;
const int DESCRIPTIONSIZE = 50, DATASIZE = 10;
struct strinventory {
    char description[DESCRIPTIONSIZE];
    int qty;
    int wholesaleCost;
    int retailCost;
    char dateAdded[DATASIZE];
};

// function declaration
void createstrinventory(strinventory &, fstream &, int &);

int displaymenu();

void newRecord(strinventory &, fstream &, int &);

void displayRecord(strinventory &, fstream &, int);

void changeRecord(strinventory &, fstream &, int);

//main function
int main() {
    int totRecords = 0;
    int chocice = 0;
    // creating the file if the file already exists
    fstream inventoryFile("Inventory.dat", ios::in | ios::out | ios::binary | ios::trunc);
    strinventory product;
    if (!inventoryFile) { cout << "Error opening the file" << endl; }
    createstrinventory(product, inventoryFile, totRecords);
    while (chocice != 4) {
        chocice = displaymenu();
        switch (chocice) {
            case 1:
                newRecord(product, inventoryFile, totRecords);
                break;
            case 2:
                displayRecord(product, inventoryFile, totRecords);
                break;
            case 3:
                changeRecord(product, inventoryFile, totRecords);
                break;
            case 4:
                break;
            default:
                cout << "Invalid choice\n";
        }
    }
    cout << "Program end\n";
    // close the file
    inventoryFile.close();
    return 0;
}

// *=== END MAIN FUNCTION ===*
// function definitions
// ==================================================
// ====== FUNCTION DEFINITION ======
// let user fill in info for a product
// user can create as many records as necessary
void createstrinventory(strinventory &product, fstream &fileName, int &records) {
    char choiceaddrec;
    int totRecords = 0;
    do {
        cout << "Enter the details of the product:\n";
        cout << "Item Description: ";
        cin.getline(product.description, DESCRIPTIONSIZE);
        do {
            cout << "Quantity on hand: ";
            cin >> product.qty;
            cin.ignore();
        } while (product.qty < 0);
        do {
            cout << "Wholesale cost: ";
            cin >> product.wholesaleCost;
            cin.ignore();
        } while (product.wholesaleCost < 0);
        do {
            cout << "Retail cost: ";
            cin >> product.retailCost;
            cin.ignore();
        } while (product.retailCost < 0);
        do {
            cout << "Date added to inventory (MM/DD/YYYY): ";
            cin.getline(product.dateAdded, DATASIZE);
        } while (!(ispunct(product.dateAdded[2])) || !(ispunct(product.dateAdded[5])));
        fileName.write(reinterpret_cast<char *>(&product), sizeof(product));
        cout << "Do you want to write another record? (y for yes)\n";
        cin >> choiceaddrec;
        cin.ignore();
        ++totRecords;
    } while (choiceaddrec == 'Y' || choiceaddrec == 'y');
    records += totRecords;
}

int displaymenu() {
    int choice;
    cout << "\n\n =================================\n";
    cout << "\tMenu\n";
    cout << "Choose a chocice:\n";
    cout << "1) add new records to the file\n";
    cout << "2) display any record in the file\n";
    cout << "3) change any record in the file\n";
    cout << "4) Quit\n";
    cout << "\n =================================\n\n";
    cin >> choice;
    cin.ignore();
    return choice;
}

void newRecord(strinventory &product, fstream &fileName,
               int &records) {
    fileName.clear();
    // clear flags       
    int numRecordAdded = 0;
    // number of new records added  
    char choiceaddrec;
    // To hold Y or N       
    // go to the end of the file to append the new record 
    fileName.seekg(0L, ios::end);
    cout << "\n\tAdd new records\n";
    do {
        cout << "Enter the following data about a product:\n";
        cout << "Item Description: ";
        cin.getline(product.description, DESCRIPTIONSIZE);
        do {
            cout << "Quantity on hand: ";
            cin >> product.qty;
            cin.ignore();
        } while (product.qty < 0);
        do {
            cout << "Wholesale cost: $";
            cin >> product.wholesaleCost;
            cin.ignore();
        } while (product.wholesaleCost < 0);
        do {
            cout << "Retail cost: $";
            cin >> product.retailCost;
            cin.ignore();
        } while (product.retailCost < 0);
        do {
            cout << "Date added to strinventory (MM/DD/YYYY): ";
            cin.getline(product.dateAdded, DATASIZE);
        } while (!(ispunct(product.dateAdded[2])) || !(ispunct(product.dateAdded[5])));
        fileName.write(reinterpret_cast<char *>(&product), sizeof(product));
        cout << "Do you want to write another record? (y for yes)\n";
        cin >> choiceaddrec;
        cin.ignore();
        ++numRecordAdded;
    } while (choiceaddrec == 'Y' || choiceaddrec == 'y');
    records += numRecordAdded;
}

void displayRecord(strinventory &product, fstream &fileName,
                   int records) {
    fileName.clear();
    // clear flags     
    cout << "\n\tDisplay a record\n";
    int recNum;
    // seek the file to the beginning 
    fileName.seekg(0L, ios::beg);
    cout << "You have " << records << " records" << endl;
    cout << "Which record would you like to display? ";
    cin >> recNum;
    cin.ignore();
    --recNum;
    // minus 1 to seek properly   
    // seek to the record, store(read it), and display it 
    fileName.seekg(sizeof(product) * recNum, ios::beg);
    fileName.read(reinterpret_cast<char *>(&product), sizeof(product));
    cout << "Displaying record #" << (recNum + 1) << endl << endl;
    cout << "Item description: " << product.description << endl;
    cout << "Quantity on hand: " << product.qty << endl;
    cout << "Wholesale cost: " << product.wholesaleCost << endl;
    cout << "Retail cost: " << product.retailCost << endl;
    cout << "Date added to strinventory (MM/DD/YYYY): " << product.dateAdded
         << endl;
    // seek the file back to the beginning 
    fileName.seekg(0L, ios::beg);
}

void changeRecord(strinventory &product, fstream &fileName,
                  int records) {
    fileName.clear(); // clear flags  
    int recCh;
    // record to change     
    cout << "\n\tChange a record\n";
    cout << "You have " << records << " records" << endl;
    cout << "Which record would you like to change? ";
    cin >> recCh;
    cin.ignore();
    --recCh; // minus 1 to seek properly    
    // seek to the record  
    fileName.seekg(sizeof(product) * recCh,
                   ios::beg);      // let the user change the record
    cout << "Changing record #" << (recCh + 1) << endl << endl;
    cout << "Enter the following data about a product:\n";
    cout << "Item Description: ";
    cin.getline(product.description, DESCRIPTIONSIZE);
    do {
        cout << "Quantity on hand: ";
        cin >> product.qty;
        cin.ignore();
    } while (product.qty < 0);
    do {
        cout << "Wholesale cost: $";
        cin >> product.wholesaleCost;
        cin.ignore();
    } while (product.wholesaleCost < 0);
    do {
        cout << "Retail cost: $";
        cin >> product.retailCost;
        cin.ignore();
    } while (product.retailCost < 0);
    do {
        cout << "Date added to strinventory (MM/DD/YYYY): ";
        cin.getline(product.dateAdded, DATASIZE);
    } while (!(ispunct(product.dateAdded[2])) || !(ispunct(
            product.dateAdded[5])));
    // rewrite over record in the file    
    fileName.write(reinterpret_cast<char *>(&product),
                   sizeof(product));  // seek the file back to the beginning  
    fileName.seekg(0L, ios::end);
    cout << "The file has been rewritten\n";
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog