You are browsing as a guest. Sign up (or log in) to start making projects!

6h 8m 47s logged

AeroNex - Devlog #3

What I did

This update I worked more on AeroNex. I did the price system. I also started to move code into many files.

In Devlog #2 I showed the terminal boxes. The program looked nice. But inside the booking logic was broken. So this time I fixed the real code.

Old price system (problem in v1)

In old version, when user booked a ticket, I saved the choice like this. I added numbers into one char:

accounts[index].accountsBooked[accounts[index].currentBooking] += 1;  // France
accounts[index].accountsBooked[accounts[index].currentBooking] += 8;  // Business
accounts[index].accountsBooked[accounts[index].currentBooking] += 32; // Today

My idea was to save each option in different bit of the byte. It looked smart. But it was broken:

  • char only holds up to 255, so it overflows very fast
  • I never moved currentBooking, so every booking overwrote same slot
  • to read the number back into real ticket was almost impossible

New price system

This update I fixed it. I use the bit idea correctly now. Every choice has its own bits:

  • Destination - bits 0 to 2
  • Class - bits 3 to 4
  • Time - bits 5 to 7

Bits do not overlap. So I can build the code with |:

unsigned char code = destination | (class << 3) | (time << 5);

And read it back with & and »:

unsigned char dest  =  code        & 0x07;
unsigned char klass = (code >> 3)  & 0x03;
unsigned char when  = (code >> 5)  & 0x07;

Then price is just three lookups added together:

int count_price(unsigned char code)
{
    return basePrice[dest] + classPrice[klass] + timePrice[when];
}

One byte for booking. One int for price. No overflow. No collisions. Easy to read.

Split into files

I also started to move code into different files:

  • one file for checks (login, name, password, phone)
  • one file for printing menus and boxes
  • one file for booking logic
  • one file for saving data
  • main.c stays small and simple

Now main.c reads like a story: register, login, book, save.

Fixing bugs

While I refactored I found many bugs from v1:

  • in checkLogin one condition could never be true (login[0] < 97 && login[0] > 123)
  • dead branches in checkName that never ran
  • the currentBooking counter that never moved
  • time loop in booking() that never exited (I wrote flag = 0 but I need flag1 = 0)
  • one segmentation fault. Program crashed when I read string after loop.

Fixing these bugs helped me understand C much better.

What I will do next

Next update I want to:

  • finish the file split with a good Makefile
  • save bookings to disk so they stay after program closes
  • make terminal boxes cleaner
  • add real ticket printing with new price

Project is still not finished. But the new bit price system fixed the core of booking logic.

1
70

Comments 0

No comments yet. Be the first!