---
title: "Department store billing system with files"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:billing-system-file"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/billing-system-file
site: BCT Engineering
---

# Department store billing system with files

Object Oriented Programming · Chapter 8: Stream Computation for Console and File Input/Output

- Asked in: 2072 Chaitra, 7 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/billing-system-file
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

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.

## 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.

## Answer

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.

```cpp
#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):

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

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does the billing program get an item's name and price for a purchased item code?
   A. It keeps all items in an array and never uses the file
   B. It opens items.dat, reads records until getCode() equals the entered code
   C. It asks the user to re-enter the price
   D. It uses seekg with the quantity as offset
   Answer: B. Items are saved in items.dat; for each purchase the file is read sequentially until the matching code is found. (Key point: Storing and retrieving items)

2. How is the line amount and bill total computed?
   A. amt = price + qty, total = last amt
   B. amt = qty * price, and total += amt for each item
   C. amt = price / qty, total = amt * m
   D. amt = code * qty, total = max of amt
   Answer: B. Each amount is quantity times unit price, and the amounts are added to a running total. (Key point: Amount calculation)

3. What does setw(12) do for the output that follows it?
   A. Prints the value 12 times
   B. Makes the next output occupy a field of width 12
   C. Rounds the value to 12 decimals
   D. Sets the file buffer size to 12
   Answer: B. setw sets the minimum field width of the next output item, which lines up the bill columns. (Key point: setw and alignment)

4. Why are fixed and setprecision(2) used when printing prices?
   A. To limit the number to two digits in total
   B. To show exactly two digits after the decimal point such as 35.00
   C. To print numbers in hexadecimal
   D. To left-align the numbers
   Answer: B. fixed with setprecision(2) prints a fixed 2 digits after the decimal point, giving a money format. (Key point: fixed and setprecision)

## Related in Object Oriented Programming

- [Patient information store and retrieve](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apatient-file-program) — Object Oriented Programming, chapter 8
- [Department class constructor and destructor](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adept-class-destructor-order) — Object Oriented Programming, chapter 4
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6
- [Display N characters using default arguments](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3An-chars-default-args-program) — Object Oriented Programming, chapter 3
- [Write and display 10 employees using file](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aemployee-file-program) — Object Oriented Programming, chapter 8
- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8

## Related in other subjects

- [Department store 36 × 15 m LED panel lighting](https://www.bctengineer.com/question.md?key=semester_6%2Findustrial_power_distribution_and_illumination%3Adepartment-store-36x15-led) — Industrial Power Distribution and Illumination, chapter 7, asked 3×
- [Department store 30 × 15 m twin 55 W LED](https://www.bctengineer.com/question.md?key=semester_6%2Findustrial_power_distribution_and_illumination%3Adepartment-store-30x15-twin-55w) — Industrial Power Distribution and Illumination, chapter 7, asked 3×
- [Department store 30 × 15 m twin 40 W fluorescent](https://www.bctengineer.com/question.md?key=semester_6%2Findustrial_power_distribution_and_illumination%3Adepartment-store-twin-40w-fluorescent) — Industrial Power Distribution and Illumination, chapter 7, asked 3×
- [I/O functions used with data files](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-io-functions) — Computer Programming, chapter 10, asked 2×
