Skip to content
Tutorials Center
My learning

Topics

Lesson 124 of 135 ยท Projects

Project: Reservation System (Bus, Hospital, Voting)

Build a bus seat reservation program in C with a seat table, booking and cancelling rules, a checksummed binary file and a command loop, then reuse the pattern for voting.

Bus tickets, hospital appointments, cinema seats, exam slots, and a vote in an election look like five different problems. They are one problem. There is a fixed table of slots, each slot is either free or owned by someone, and every operation is a rule about who may change which slot: you may take a free slot, you may release your own slot, you may not take a taken one. If you write that once, carefully, you can build any of these programs by changing the labels.

This project builds a bus seat reservation system and then, in a short third example, reuses the same pattern for a voting roll. It uses structures, arrays, enums, binary files, strings and fgets-based input. It is a companion to the library project, and the two together cover the most common shape of "management system" programs. This one adds integrity checking of the saved file.

Stage 1: the seat table and its rules

The table. The bus has 16 seats, 4 rows of 2 + 2 with an aisle in the middle. Seats are numbered 1 to 16 for people, and stored at indexes 0 to 15 in an array of struct Seat. A seat holds only the passenger's name; an empty string means the seat is free. Using an empty name as the "free" marker removes the need for a separate flag, and so there is nothing that could get out of step with the name.

The rules are functions that return a result. As in the library project, the rules live in functions that return an enum Result and never print. The caller decides how to tell the user. The rules are:

  • book_seat: the seat number must be in 1 to 16, the seat must be free, and the name must be neither empty nor too long. A name that is too long is rejected, never silently cut to fit: a ticket for "Alexandra Constantinopoulou-Smith" that quietly reads "Alexandra Constantinopo" is a booking bug that someone will meet at the bus door.
  • cancel_seat: the seat must be booked, and the caller must give the name of the passenger. A cancellation that needs only a seat number lets anyone cancel anyone's trip. A name check is a tiny form of authorization, and the point is that the rule is in the function, not in the menu.
  • find_by_name: one passenger may hold several seats, so the function counts and prints all of them. Comparison ignores case, because "ana" and "Ana" are the same person to a human.

Showing the bus. A seat map is much more useful than a list. A free seat prints its number ([ 3]), a taken seat prints [XX], and the aisle is two spaces between the pairs. The loop walks the 16 seats and starts a new line every 4 seats.

C Stage 1: book, cancel and find seats

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.

Look at the map: seats 2 and 3 are [XX], seat 1 is free again after the cancellation, and the aisle falls between seats 2 and 3 of each row. Each failed line returned a different reason, and none of them changed the table.

Stage 2: a file you can trust, and a command loop

Saving the table. As in the library project, the data is one flat array with no pointers, so fwrite can write it in one call. This version adds a header with four fields: a magic text (RSV1), the number of slots, the size of one record, and a checksum of the seat data. Together they let load_seats refuse a file that does not belong to this version of the program:

  • the magic text says "this is our file", and the 1 is the format version;
  • the slot count and record size catch a program whose SEATS or struct Seat has changed since the file was written, which would otherwise be read as garbage;
  • the checksum catches damage: a flipped bit, a half-written file, or someone editing it with a text editor.

The checksum. FNV-1a is a very small hash: start with a fixed 32-bit number, and for each byte XOR the byte in and multiply by a prime. It is not a security tool (anyone can recompute it), but it detects accidental damage with a very high probability, and it is five lines. The types come from <stdint.h>: uint32_t is exactly 32 bits on every platform, which a plain unsigned is not promised to be. This matters for a file format, where the size of a field must not depend on the machine.

Saved atomically. The program writes to bus.dat.tmp and renames it over the real file, as before, so a crash while saving cannot damage the earlier good copy.

The command loop. Input is one command per line: book 3 Ana Lopez, cancel 3 Ana Lopez, find ana, show, save, quit. The first word is read with sscanf("%15s%n"), and the arguments are parsed per command, with every sscanf result checked. For book and cancel, a helper reads the seat number and returns a pointer to the rest of the line as the name. The loop ends on quit or at the end of the input, and a program that forgot to call save loses its changes, so the loop saves automatically on exit and says so.

Proving it works, including the failure case. The demo finishes in three steps: it loads the file into a fresh table and prints it (as if the program had restarted), then it damages one byte of the file on purpose, and loads again. The checksum must reject the damaged file. A saved-file feature that was never tested with a bad file is only half tested.

C Stage 2: commands, a checksummed file and a damaged file

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.

Follow the commands against the output. The second book 2 is refused, book 17 is out of range, and book 5 has no name. cancel 1 Ben fails because seat 1 belongs to Ana, and cancel 1 Ana succeeds. The reloaded map matches the one from memory. The last line shows the checksum doing its work.

The same pattern for a voting roll

Replace seats with voters, and the booking rules with voting rules, and you have an electronic ballot box. The table is indexed by voter number. A voter may vote once, and a vote must be for a valid candidate. There is one design point that has nothing to do with C: a roll that stores who voted for whom cannot guarantee a secret ballot. So the roll records only whether each voter has voted, and the tally is a separate set of counters that knows nothing about voters.

C The pattern again: a voting roll

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 table below shows how the three examples fit the same frame. The hospital row is left as an exercise.

Program Slot The "owner" field Main rule
Bus seat 1 to 16 passenger name a taken seat cannot be booked again
Hospital a 30-minute time slot patient name and doctor a doctor cannot have two patients at the same time
Voting voter number a single voted flag one vote per voter, and no link to the choice

Ideas for going further

  • Hospital appointments. Make the slot index a time: slot s starts at 9 * 60 + s * 30 minutes after midnight. Print it as HH:MM, and add a second table for a second doctor.
  • Seat classes. Make seats 1 to 4 "premium" with a different price, and print the total for a booking of several seats.
  • Book several seats at once. book 5-8 Ana should book all four or none: check them all before changing any.
  • Waiting list. When a seat is cancelled, give it to the first name on a waiting-list queue (see stacks and queues).
  • A bigger file. Store the date of the trip in the header, and one file per day.
  • Concurrent users. Two programs writing to one file would overwrite each other. Look up file locking (flock or fcntl) and think about what the rename trick does and does not protect.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does the program reject a passenger name that is too long instead of cutting it?

    A truncated name is wrong data that looks valid. Refusing it with a clear message lets the user fix the input.
  2. 2. What does the checksum in the file header protect against?

    The checksum is recomputed on load and compared. A mismatch shows that the data changed after it was saved. It is not a secret and it does not stop deliberate tampering.
  3. 3. Why are the header fields declared as uint32_t and not int?

    The size of int is up to the platform. uint32_t from stdint.h is exactly 32 bits, so the file layout does not depend on the machine.
  4. 4. Why does the voting roll store voted flags and a separate tally, and not the voter's choice?

    Which data you store is a design decision with consequences. Keeping the choice out of the voter record makes a secret ballot possible by construction.
  5. 5. In cancel_seat, why is the passenger's name required as well as the seat number?

    The rule that a booking can be changed only by its owner belongs in the function that changes it, so every caller gets it for free.

Summary

  • Many "management" programs share one design: a fixed table of slots, an owner field per slot (an empty value means free), and a few rules that decide who may change a slot.
  • Put the rules in functions that return an enum result and print nothing. Reject bad input; do not repair it silently.
  • Give a binary file a header: a magic text with a version, the slot count, the record size and a checksum, and reject any file that fails a check.
  • Use fixed-size types such as uint32_t for fields in a file format.
  • Save through a temporary file and rename, and test the loader with a damaged file as well as a good one.
  • Choose what to store carefully: a voting roll that does not record the choice makes a secret ballot possible.

Next: the C cheat sheet, a one-page reference for everything you used in these projects.