I have created a 2d array for seating. This array currently only needs to hold ints. I have a main method that creates 2 arrays dynamically, the normal and vip sections of seating. I am pretty sure the arrays themselves are being created properly. The errors occur when I try to view the content in my arrays. I have 2 basic methods for checking the contents. an occupied method which is used to tell me if the ints given to it are occupied in the array and a method returns the total number of seats available.
Calling the available seats gives me random number but does not break the program.
Calling occupied crashes the program.
To have the menu keep popping up I put it in a while loop. Entering a 0 (zero) should stop the loops, program crashes instead.
#include "SeatSelection.h"
/*
default and main constructor combined.
all it does is creates the 2d array and creates the avaiable seats.
availabe seats just keeps track of the total number for each instance of the class
*/
SeatSelection::SeatSelection(int r, int s)
{
int availabeSeats=0;
int ROW=0;
int SEATS=0;
availabeSeats = (r*s);
ROW=r;
SEATS=s;
int **seats;
seats = newint * [ROW];
for(int i=0 ; i<ROW ; i++)
{
seats[i] = newint [SEATS];
for(int j=0 ; j<SEATS ; j++)
seats[i][j] = 0;
}
}
/*
Error checks are done before setSeat is called.
Brute force approach of setting seats, it goes through the entire array until it finds an empty
seat then fills that seat and keeps setting sets until it either reaches the end of the row or the
number of seats purchased.
If it reaches the end of the row it just breaks out of the while loop and keeps going through the rest of the array till it finds another empty spot.
*/
void SeatSelection::setSeat(int numSeats)
{
int counter=0;
for(int i=0 ; i<ROW ; i++)
{
for(int j=0 ; j<SEATS ; j++)
{
if(seats[i][j] == 0)
{
while(counter <= numSeats)
{
seats[i][j] = 1;
counter++;
if(j == SEATS)
break;
}
}
}
}
availabeSeats = availabeSeats-numSeats;
}
/*
looping done in main. Occupied just checks the value given with the array.
*/
int SeatSelection::occupied(int r, int s)
{
int temp = 0;
if(seats[r][s] == 1)
{
temp = 1;
}
return temp;
}
/*
returns the toal number of open seats
*/
int SeatSelection::getAvailabe()
{
return availabeSeats;
}
/*
I got no idea who to do deconstructor properly I kept getting errors for certain
parts so I am ignoring it for now
*/
SeatSelection::~SeatSelection()
{
for(int i=0 ; i<ROW ; i++)
{
delete seats[i];
}
}