Skip to content
Tutorials Center
My learning

Topics

Lesson 126 of 135 ยท Projects

Project: Telecom Billing System

Build a telecom billing program in C: customer records you can add, view, modify and delete by phone number, tiered call charges in cents, and a payment ledger saved to a file.

A telephone company does three things with its data: it keeps a customer record for every phone number, it turns usage into a bill using a tariff, and it records payments against that bill. This project builds all three in a small console program. It practises the same skills as the bank project (structures, arrays, file handling) but puts the emphasis on two ideas that come up in every billing program: money as integers and tiered pricing.

Money is not a double

Never store money in float or double: 0.1 has no exact binary representation, so sums drift (floating point explains why). Store the amount in the smallest unit, cents, in an int or long, and divide by 100 only when printing. A bill of $12.34 is the integer 1234; printing it is printf("$%ld.%02ld", cents / 100, cents % 100).

The components

Part Job
struct Customer Phone number (a string, not an int: it can start with 0 or +), name, plan, minutes used, amount paid in cents
add_customer Refuses duplicates and a full table
find_customer Looks up by phone number and returns an index or -1
modify_customer Changes the name or plan of an existing record
delete_customer Removes a record by moving the last one into its slot
compute_bill Applies the tariff to the minutes used
record_payment Adds a payment and reports the balance

Every function that takes a phone number goes through find_customer, so the "not found" case is handled in one place.

Stage 1: records and the tariff

The tariff has three tiers. The first 100 minutes cost 10 cents each, the next 400 cost 7 cents each, and everything above 500 costs 5 cents each. A flat monthly fee is added on top, and it depends on the plan. Tiers are marginal: a customer with 120 minutes pays the first rate for 100 of them and the second rate only for the other 20, like income tax brackets. The most common mistake is to apply the rate of the highest tier to every minute.

delete_customer shows a handy idiom for arrays whose order does not matter: copy the last element into the freed slot and shrink the count. It is O(1) instead of shifting everything down.

C Stage 1: customers, tiers and bills

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check the arithmetic by hand: Omar uses 120 minutes on the plus plan, so he pays 1500 + 100 x 10 + 20 x 7 = 2640 cents, $26.40. Ines uses 650 minutes: 500 + 1000 + 400 x 7 + 150 x 5 = 5050 cents. After the delete, the last record (0155-1003) has moved into slot 0, which is why the order changed. If the order of records matters to you, shift the following elements down with memmove instead.

Stage 2: payments and a file

A billing system must not lose data when the program ends, so payments go to a file. The program below writes each customer to a text file as one line (phone;name;plan;minutes;paid), reads it back, and then applies two payments and prints each customer's balance: the bill minus what has been paid. A negative balance means the customer has paid in advance, and a positive one is the amount still due.

Text with semicolons between fields is easy to inspect in an editor. Parse it with sscanf and a scan set such as %29[^;], which reads up to the next semicolon, so a name containing spaces survives. Always check that sscanf returned the number of fields you expected, because a damaged line must be skipped, not half-loaded.

C Stage 2: saving, loading and payments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The output shows the damaged line being skipped with its line number, the two bad payments being refused, and the balances: Maya owes $0.00 after paying $13.00 for her $13.00 bill (500 + 80 x 10 = 1300 cents), Omar is already settled, and Ines has overpaid by $9.50 (a bill of $50.50 against $60.00 paid, so her balance is -$9.50). The final save_all rewrites the file so the payments survive. A more careful version would write to a temporary file and rename it over the original, so a crash in the middle of saving cannot destroy the data.

Ideas for extending it

  • A modify_customer function that changes the name or plan, with the same "not found" handling.
  • A call detail record (CDR) file with one line per call (number, start time, seconds) and a program that adds up the minutes per customer with time functions. Billing systems round each call up to a whole minute.
  • Tax as a percentage, computed in integer arithmetic with an explicit rounding rule.
  • A customer menu that shows only that customer's bill and payments, which is the usual split between an "admin" and a "customer" view.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is money stored as an integer number of cents?

    Integer cents are exact, and dividing by 100 happens only when displaying the value.
  2. 2. A customer used 120 minutes. The first 100 cost 10 cents, the next ones 7 cents. How many cents is the usage charge?

    100 x 10 + 20 x 7 = 1040. Marginal tiers charge each minute at its own tier's rate.
  3. 3. Why is a phone number stored as a string and not an int?

    Identifiers that you never do arithmetic on are best kept as text, which also keeps leading zeros.
  4. 4. What does the delete idiom customers[i] = customers[--customer_count] do?

    It is fast but changes the order of records, which is acceptable when the order is not meaningful.

Summary

  • Keep money in integer cents and format it only for display.
  • Tiered tariffs are marginal: split the usage across the tiers and add the parts.
  • Identifiers such as phone numbers are strings; every lookup goes through one find_customer function.
  • Delete from an unordered array by moving the last record into the gap.
  • Save records as one line each, parse with sscanf and check its return value, and skip damaged lines with a report.

Next: Project: Console Snake Game