I have to make a program that reads the number of seats and stores it in a two dimensional array. The empty seats is hashtag and if the user buys a seat it becomes . The odd rows have 15 seats and even have 20.
When I purchase a seat, it puts the
on the seat but when I buy another seat it removes it and puts on the newly purchased seat. How can I make it so it saves the that it prints on every seat.

#include <iostream>

using namespace std;
int column2, row2,total = 0;
    char ab[20][15];
    char EMPTY = '#';
    char FULL = '*';
    int seat = 300;
    int seat2 = 0;
    int Quit = 1;
    int choice;
    int cost,answer,price;

void ShowSeats()
{    
    cout << "\tSeats" << endl;
    cout << "       0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19\n";

    for (int i = 0; i < 15; i++) {
        for (int j = 0; j < 20; j++) {
            ab[i][j] = EMPTY;
            if (i % 2 && j == 14) // 0 is false, 1 is true
            {
                break;
            }
            if (i == row2 && j == column2) // assuming these numbers start from 0
            {
                ab[i][j] = '*';
            }
        }
    }
    for (int i = 0; i < 15; i++) {
        cout << endl
             << "Row " << (i + 1);
        for (int j = 0; j < 20; j++) {

            cout << "  " << ab[i][j];

        }
    }
}

int main()
{

while (true){    
    cout << "Please select the row you would like to sit in: ";
    cin >> row2;
    cout << "Please select the seat you would like to sit in: ";
    cin >> column2;
    cout<< "Enter the price";
    cin >> price;
    if (ab [row2] [column2] == '*')
        {
            cout << "Sorry that seat is sold-out, Please select a new seat.";
            cout << endl;
        }
        else
        {
            cost = price;

            cout << "That ticket costs: " << cost << endl;
            cout << "Confirm Purchase? Enter (1 = YES)";
            cin >> answer;
            seat = seat - answer;
            seat2 += answer;

            if (answer == 1)
            {
                cout << "Your ticket purchase has been confirmed." << endl;
                ab [row2][column2] = FULL;
                total = total + cost;
                cout << "Would you like to look at another seat? (1 = YES)";
                cin>>Quit;
            }
    ShowSeats();
    }

}}

It shows me this when I purchase row 2 and seat 2
https://gyazo.com/0d8bd7ed02e969110db47b428c512f24

But when I purchase row 2 seat 3 it does not save the previous purchase and I want it to save both.
https://gyazo.com/f865ba7145d1fafac246836975f2ee00

Dani AI

Generated

Quick diagnosis: the seats array is being wiped each time the display routine runs and the array dimensions are reversed, which explains why earlier purchases disappear and why behavior is unpredictable. As hinted, the place where the code writes a FULL value is not the same place the display reads from. As suggested, initialize the seat map once and keep display logic separate.

Suggested fixes (apply together):

  • Declare the array with matching dimensions and constants (avoid magic numbers).
  • Initialize all seats once at program start (not inside the display function).
  • Let the display function only print; updates to a seat should be done where the purchase is confirmed.
  • Validate input and consistently use either 0‑based or 1‑based indices (convert user input to the array index).

Example (minimal, not a drop‑in replacement of original code):

const int ROWS = 15;
const int COLS = 20;
char seats[ROWS][COLS];

void initSeats() {
  for (int r = 0; r < ROWS; ++r) {
    int rowSeats = ((r + 1) % 2 == 1) ? 15 : 20; // rows 1,3,5... have 15 seats
    for (int c = 0; c < rowSeats; ++c) seats[r][c] = '#';
    for (int c = rowSeats; c < COLS; ++c) seats[r][c] = ' '; // unused slots
  }
}

void showSeats() {
  for (int r = 0; r < ROWS; ++r) {
    int rowSeats = ((r + 1) % 2 == 1) ? 15 : 20;
    std::cout << "Row " << (r + 1);
    for (int c = 0; c < rowSeats; ++c) std::cout << " " << seats[r][c];
    std::cout << '\n';
  }
}

Additional notes: convert user input (1..N) to zero‑based before indexing (index = input - 1), check bounds before accessing seats[r][c], and prefer std::vector or std::array for safer code. After these changes, marking a seat as '*' at purchase time will persist across calls to showSeats() and avoid undefined behavior caused by the dimension mismatch.

Recommended Answers

All 2 Replies

Think real hard about lines 34 and 71.

In line 34 you print from a certain row, then line 71 puts it into a different row. Think real hard where line 71 stored the FULL value.
numbertwo.PNG

First off I would suggest creating an initialize function that sets all array elements to EMPTY and runs only once at the beginning of execution. Separating your program logic into methods or blocks will improve readability for yourself and others.

Second, as it stands now, you need to change your for() loop control statements to:
int i = 1; i <= 15;
and
int j = 1; j <= 20;
because both indexes are starting at zero, which makes the statement ( i%2 && j ==14) useless. You can use (i%2) to determine if you're referencing an even or odd row and based on that you set j to 15 or 20.

Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.