Skip to main content

Department store billing system with files

Short answer

Store item data (code, name, price) in binary file items.dat. For billing, read item records to match entered code, compute amount = quantity × price, and display a formatted bill using setw, left/right, setprecision and setfill manipulators.

Write a program to make billing system of a department store. Your program should store and retrieve data to/from files. Use manipulators to display the record in proper formats.

Assumptions: the store's items (code, name, price) are saved in items.dat. For a bill, the user enters each item's code and quantity; the program reads the item from the file and prints a formatted bill using manipulators.

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

class Item {
    int code;
    char name[20];
    float price;
public:
    void getData() { cout << "Code, Name, Price: "; cin >> code >> name >> price; }
    int getCode() { return code; }
    float getPrice() { return price; }
    const char* getName() { return name; }
};

int main() {
    Item it;
    int n;
    cout << "How many items to store? ";
    cin >> n;
    ofstream fout("items.dat", ios::binary);
    for (int i = 0; i < n; i++) {            // store data
        it.getData();
        fout.write((char*)&it, sizeof(it));
    }
    fout.close();

    int m, code, qty;
    float total = 0;
    cout << "Number of items purchased: ";
    cin >> m;

    cout << "\n" << left << setw(8) << "Code" << setw(15) << "Item"
         << right << setw(6) << "Qty" << setw(10) << "Rate"
         << setw(12) << "Amount" << endl;
    cout << setfill('-') << setw(51) << "" << setfill(' ') << endl;

    for (int i = 0; i < m; i++) {
        cin >> code >> qty;
        ifstream fin("items.dat", ios::binary);   // retrieve
        while (fin.read((char*)&it, sizeof(it))) {
            if (it.getCode() == code) {
                float amt = qty * it.getPrice();
                total += amt;
                cout << left << setw(8) << code << setw(15) << it.getName()
                     << right << setw(6) << qty
                     << setw(10) << fixed << setprecision(2) << it.getPrice()
                     << setw(12) << amt << endl;
                break;
            }
        }
        fin.close();
    }
    cout << setfill('-') << setw(51) << "" << setfill(' ') << endl;
    cout << left << setw(39) << "Total" << right << setw(12) << total << endl;
    return 0;
}

Sample bill (the typed code and quantity lines are not shown):

Code    Item              Qty      Rate      Amount
---------------------------------------------------
101     Soap                2     35.00       70.00
102     Rice                5     90.00      450.00
---------------------------------------------------
Total                                        520.00

✓ Answer checked by Claude Opus 5.5 (high effort)

All Object Oriented Programming chapter 8 questions →