DEV Community

May cccc
May cccc

Posted on

My C++ training project

As part of my journey to practice and master core programming concepts in C++, I built a simple yet functional Console Ticket Reservation System.
Building small projects like this has been one of the best ways to solidify my understanding of data structures, conditional logic, and control loops. In this article, I’ll walk you through how the code works and what I learned while building it.
🎯 What Does the Application Do?
The application simulates a theater seat reservation system using a 4x4 grid (16 total seats):

  • represents an available seat. X represents a reserved seat. Users can repeatedly choose seat coordinates (row and column), see live updates of the theater map, get notified if a seat is already taken, and receive a final price calculation upon checkout. 💡 Key Features & Concepts Used
  • 2D Arrays for Grid Layouts The seating layout is managed using a 2D array: char seats[4][4]. This allowed me to map human-readable row and column inputs (1 to 4) directly to array indices [row - 1][col - 1].
  • Custom Visualization Function To present the map clearly after each reservation, I created a dedicated helper function the_map() that wraps the output with visual border styling:

void the_map(char seat[4][4]) {

cout << "~⁠~⁠~~⁠~⁠~~⁠~⁠~~⁠~⁠~~⁠~⁠~~⁠~⁠~" << endl;
for (int i = 0; i < 4; i++) {
cout << endl;
for (int j = 0; j < 4; j++) {
cout << seat[i][j] << " ";
}
}
cout << "\n~⁠~⁠~~⁠~⁠~~⁠~⁠~~⁠~⁠~~⁠~⁠~~⁠~⁠~" << endl;
}

  1. Conflict Prevention & Checkout Logic Before marking a seat as taken, the system checks if seats[e][r] == 'X'. If occupied, it displays an error message preventing double-booking; otherwise, it reserves the seat and updates the ticket counter. 🛠️ Complete Source Code Here is the complete implementation from my training exercise:

include

using namespace std;

void the_map(char seat[4][4]) {

cout << "~⁠~⁠~~⁠~⁠~~⁠~⁠~~⁠~⁠~~⁠~⁠~~⁠~⁠~" << endl;
for (int i = 0; i < 4; i++) {
cout << endl;
for (int j = 0; j < 4; j++) {
cout << seat[i][j] << " ";
}
}
cout << "\n~⁠~⁠~~⁠~⁠~~⁠~⁠~~⁠~⁠~~⁠~⁠~~⁠~⁠~" << endl;
}

int main() {

bool next_ = 1;
int totalTickets = 0;
double TICKET_PRICE = 12.5;
double totalPrice = 0;
int off;
char seats[4][4];

cout << "Do you want to reserve a seat?" << endl;
cout << "Enter 4 to exit, or any other number to proceed: ";

do {
    cin >> off;
} while (off == 4);

// Initialize map with available seats '*'
for (int e = 0; e < 4; e++) {
    for (int r = 0; r < 4; r++) {
        seats[e][r] = '*';
    }
}

// Main booking loop
do {
    for (int e = 0; e < 4; e++) {
        cout << endl;
        for (int r = 0; r < 4; r++) {
            cout << seats[e][r] << " ";
        }
    }

    int g, h;
    cout << "\n\nChoose row (1-4): ";
    cin >> g;
    cout << "Choose column (1-4): ";
    cin >> h;

    int e = g - 1;
    int r = h - 1;

    if (seats[e][r] == 'X') {
        cout << "Conflict: Seat already taken!" << endl;
    } else {
        seats[e][r] = 'X';
        totalTickets++;
    }

    the_map(seats);

    cout << "Enter 1 to continue booking, or 0 to exit: ";
    cin >> next_;

} while (next_);

// Summary calculation
totalPrice = TICKET_PRICE * totalTickets;
cout << "\n----------------------------------------" << endl;
cout << "Total tickets booked: " << totalTickets << endl;
cout << "Total price: $" << totalPrice << endl;
cout << "----------------------------------------" << endl;

return 0;
Enter fullscreen mode Exit fullscreen mode

}

Top comments (2)

Collapse
 
huy_tn_d100a05a9c4a782e5 profile image
Darwin •

nice but for what I know you should avoid using the "using namespace std" to prevent name conflicts/ambiguity and namespace pollution. You can ask AI why it will explain for ya.

Collapse
 
maya__354c16ced7bfb8 profile image
May cccc •

Thank you for your kind comment! I appreciate your help in identifying my mistakes and developing my skills. You have sparked my curiosity to learn more about this. Next time, I will use:

using std::cout;
using std::cin;
using std::endl;