NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Operations

//Leo Vazquez Estrada
//CIS 1202
//March 15, 2023
//Programming Assignment #10

//Personal Library
#include "PLLVE.h"

const int NAME_SIZE = 40;
//Structure
struct Product
{
    long number;
    char name[NAME_SIZE];
    double price;
    int quantity;
};
//Function Prototypes
int getMenu();
void createFile(fstream&);
void displayFile(fstream&);
void displayRecord(fstream&);
void modifyRecord(fstream&);
long byteNum(int);
void showRec(Product);
//Main Code
int main() {
    fstream file;
    int userInput = 0;
    file.open("inventory.dat", ios::beg | ios::in | ios::out | ios::binary);
    createFile(file);
    userInput = getMenu();
    do {
        switch (userInput) {
        case 1: displayFile(file);
            break;
        case 2: displayRecord(file);
            break;
        case 3:modifyRecord(file);
            break;
        case 4: endprogram();
        }
    } while (userInput != 4);
    file.close();
}

//Functions

int getMenu()
{
    int userNum = 0;
    cout << " 1. Display Entire Inventory" << endl;
    cout << " 2. Display a certain Inventory Item" << endl;
    cout << " 3. Modify a Product" << endl;
    cout << " 4. End Program" << endl;
    userNum = validateInt("Enter the number of the action you'd like to take place. ", 1, 4);
    return userNum;
}

void createFile(fstream& file)
{
    vector<Product> products = {
        { 12345, "dog", 11.99, 5 },
        { 12346, "cat", 11.98, 6 },
        { 12347, "rat", 10.99, 7 },
        { 12348, "bird", 12.99, 4 },
        { 12349, "snake", 13.99, 3 },
        { 12350, "hamster", 14.99, 9 }
    };

    file.clear();
    if (!file.fail()) {
        file.write(reinterpret_cast<char*>(products.data()), products.size() * sizeof(Product));
    }
    else {
        cout << "Your File Has Failed To Open" << endl;
    }
}


void displayFile(fstream& file) {
    Product prodInfo;
    int recNum = 0;

    file.clear();
    if (!file.fail()) {
        file.seekg(0, ios::end);
        int fileSize = file.tellg();
        file.seekg(0, ios::beg);

        while (file.tellg() < fileSize) {
            file.read(reinterpret_cast<char*>(&prodInfo), sizeof(prodInfo));
            if (file.gcount() != sizeof(prodInfo)) {
                cout << "Error: could not read product record" << endl;
                break;
            }
            cout << recNum << ". ";
            showRec(prodInfo);
            recNum++;
        }
        file.clear();
    }
    else {
        cout << "Error: could not open file" << endl;
    }
}
void modifyRecord(fstream& file) {
    int userChoice = 0;
    displayFile(file);
    userChoice = validateInt("Enter the number of the record you'd like to modify. ");

    int recNum = userChoice - 1;
    file.seekg(byteNum(recNum));

    Product prodInfo;
    if (file.read(reinterpret_cast<char*>(&prodInfo), sizeof(prodInfo))) {
        cout << "nEnter the new product number: ";
        cin >> prodInfo.number;
        cout << "nEnter the new product name:";
        getline(cin, prodInfo.name);
        cout << "nEnter the new product price: ";
        cin >> prodInfo.price;
        cout << "nEnter the new product quantity: ";
        cin >> prodInfo.quantity;
        file.seekp(byteNum(recNum));
        file.write(reinterpret_cast<char*>(&prodInfo), sizeof(prodInfo));
        showRec(prodInfo);
    }
    else {
        cout << "Record not found." << endl;
    }
}

long byteNum(int recNum) {
    return sizeof(Product) * recNum;
}

void showRec(Product prodInf) {
    cout << "Number: ";
    cout << prodInf.number << " ";
    cout << "Name: ";
    cout << prodInf.name << " ";
    cout << "Price: ";
    cout << prodInf.price << " ";
    cout << "Quantity: ";
    cout << prodInf.quantity << endl;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.