Locating error in a 2d array, certain methods cause program to crash

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.

Here is the main :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include "SeatSelection.h"

using namespace std;

int main(int argc, char* argv[])
{
        int *pRows = new int;
        int *pSeats = new int;
        int *rRows = new int;
        int *rSeats = new int;
        int *choice = new int;
        int *purchase = new int;
        int runner = 0;


        //opening, used to get the theater size
        cout << "Welcome to the theater!" << endl;
        cout << "How many Premium rows of seats does this theater have?" << endl;
        cin >> *pRows;
        cout << "How many Premium seats are in each row?" << endl;
        cin >> *pSeats;
        cout << "How many Regular rows of seats does this theater have?" << endl;
        cin >> *rRows;
        cout << "How many Regular seats are in each row?" << endl;
        cin >> *rSeats;

        //creation of the arrays using values gotten from input
        SeatSelection * premium = new SeatSelection(*pRows, *pSeats);
        SeatSelection * regular = new SeatSelection(*rRows, *rSeats);

        //main menu with loop to remain in that menu
        while(runner==0)
        {
            cout << endl << endl << endl << "Main Menu" << endl;
            cout << "1. Purchase Premium tickets." << endl;
            cout << "2. Purchase Regular tickets." << endl;
            cout << "3. Check how many availabe Premium seats" << endl;
            cout << "4. Check how many availabe Regular seats" << endl;
            cout << "5. Show occupied Premium seats" << endl;
            cout << "6. Show occupied Regular seats" << endl;
            cout << "7. Show all occupied seats" << endl;
            cout << "Enter a 0 (zero) to exit the program." << endl;
            cout << "Input the number corresponding to the option" << endl;
            cin >> *choice;

            if(*choice == 1)//adds to premium seating array after making sure that the array has room
            {
                cout << "How many Premium tickets would you like to purchase?" << endl;
                cin >> *purchase;
                if(premium->getAvailabe() < *purchase)
                    cout << "We do not have that many seats available!" << endl;
                else
                premium->setSeat(*purchase);

                delete purchase;
            }
            else if(*choice == 2)//adds to regular seating array after making sure that the array has room
                {
                    cout << "How many Regular tickets would you like to purchase?" << endl;
                    cin >> *purchase;
                    if(regular->getAvailabe()< *purchase)
                        cout << "We do not have that many seats available!" << endl;
                    else
                    regular->setSeat(*purchase);

                    delete purchase;
                }
                else if(*choice == 3)//checks premium array room
                    {
                        cout << premium->getAvailabe() << endl;
                    }
                    else if(*choice == 4)//checks regular array room
                        {
                            cout << regular->getAvailabe() << endl;
                        }
                        else if(*choice == 5)//shows premium seating
                            {
                                cout << "Premium seats taken:" << endl;
                                for(int i=0 ; i<*pRows ; i++)
                                {
                                    for(int j=0 ; j<*pSeats ; j++)
                                    {
                                    if(premium->occupied(i,j)==1)
                                        cout << i << (char)(j+65)<< endl;
                                    }
                                }

                            }
                            else if(*choice == 6)//shows regular seating
                                {
                                    cout << "Regular seats taken:" << endl;
                                    for(int i=0 ; i<*rRows ; i++)
                                    {
                                        for(int j=0 ; j<*rSeats ; j++)
                                        {
                                        if(regular->occupied(i,j)== 1)
                                            cout << i << (char)(j+65)<< endl;
                                        }
                                    }
                                }
                                else if(*choice == 7)////shows all seating
                                    {
                                        cout << "Premium seats taken:" << endl;
                                        for(int i=0 ; i<*pRows ; i++)
                                        {
                                            for(int j=0 ; j<*pSeats ; j++)
                                            {
                                            if(premium->occupied(i,j)== 1)
                                                cout << i << (char)(j+65)<< endl;
                                            }
                                        }
                                        cout << "Regular seats taken:" << endl;
                                        for(int i=0 ; i<*rRows ; i++)
                                        {
                                            for(int j=0 ; j<*rSeats ; j++)
                                            {
                                            if(regular->occupied(i,j)==1)
                                                cout << i << (char)(j+65)<< endl;
                                            }
                                        }
                                    }
                                    else if(*choice == 0)//used to exit loop, pretty sure it causes program to crash since there is nothing to do after
                                        {
                                            runner = 100;
                                        }
                                        else//used to catch invalid input for everything but the initial values
                                        {
                                            cout << "Please enter a Valid Choice (must be an integer)" << endl;
                                        }
        }
        delete premium;
        delete regular;
        delete pRows;
        delete pSeats;
        delete rRows;
        delete rSeats;
        delete choice;
        delete purchase;

   return 0;
}

I will post the class separately as I am out of room
Here is the class part of the code. If anyone was wondering the program I am using is codeblocks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#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 = new int * [ROW];
    for(int i=0 ; i<ROW ; i++)
    {
        seats[i] = new int [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];
    }
}
Last edited on
Topic archived. No new replies allowed.