Seating Program Error

The program keeps producing error Run-Time Check Failure #3 - The variable 'selection' is being used without being initialized. Not sure how to address that.

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>

#include <cstdlib>

#include <ctime>

# include <iomanip>


using namespace std;


int main();
void Menu();
int SeatSelection();
void DisplaySeating();

int main()
{

    // Seed the random number generator
	srand(time(0));

    Menu();

    return 0;
}

	// Menu so can keep playing or close the program
void Menu()
{
    int selection = 0;
    bool Selecting = true;

    while (Selecting)
    {
        cout << "Theatre Seatting\n\n";
        
        cout << "Menu\n\n";
        
        cout << "1) Sell Ticket\n";
        cout << "2) Exit\n\n";

        cout << "Enter your selection: ";
        cin >> selection;
        cin.ignore();

        cout << endl;

        if (selection == 1)
            SeatSelection();
        else if (selection == 2)
            Selecting = false;
		else if (selection != 1 || 2) //lets the user know why they are going back to the menu
			cout << "Try again, choose 1 or 2" << endl;
    }
}

int SeatSelection()
{
    const int rows = 12;
    const int columns = 8;
	int TotalSeats = 96;
	int selection;
	double RowValue[rows];
	char map[rows][columns];
	int RowNum, ColNum;
	const char Taken = '#';//seats taken
	const char Empty = '*';//seats free
	

	for(int i= 0;i<rows;i++)//initiating array 
	{
		for (int j=0;j<columns;j++)
		{
			map[i][j]=Empty;
		}
	}

	DisplaySeating();

	(RowValue);//ask user to enter price of each row
	cout << endl;

	
    cout << "Seat Selection Menu\n";

    while (true)
    {
        cout << "You have " << TotalSeats << " seats remaining\n\n"; // so the user can keep track of seats left
        cout << "Please enter a row number and a seat number for the ticket: " ;
				cout << "Row # :" ;
				cin >> RowNum;
				cout << endl;
				cout << "Seat # :" ;
				cin >> ColNum;
				cout << endl;
				cin.ignore();
				// Check if seat is free

		if(map[rows][columns] == Taken)
		{
			cout << "This seat is taken! Try another one. \n";
			continue; // start the loop again
		}
		else // and if it is - sell the ticket
			map[rows][columns]=Taken;
		// Add the next loop to immediately see the effects:
			for (int i = 0; i < rows; i++){
			for (int j = 0; j < columns; j++){
			cout << map[i][j];
			}
			cout << endl;
			}

        cout << '\n';

        if (selection == Taken)
        {
            cout << "Sorry, that seat is taken: " << RowNum, ColNum; "Please select another seat";
            cin.get();
            cout << "\n\n";

            return (0);
        }
		else if (selection == Empty)
		{
			cout << "Thank you for your purchase" << endl; 
			cin.get();
            cout << "\n\n";
   		}
    	

        TotalSeats--;

        if (TotalSeats == 0)
        {
            cout << "Sorry, we are sold out " << endl;
            cout << "\n";
            return (0);
        }
    }
}
void seats(double RowPrice[], int row)
{

	cout << "Please enter a ticket price for each row." << endl;
	
	for(int i = 0 ; i < row; i++)
	{
		cout << "Row # " << i++ << ": " ;
		cin >> RowPrice[i];	
	}
}

void DisplaySeating()
{
	const char TAKEN = '#';//seats taken
	const char EMPTY = '*';//seats free
	const int rows = 12;
	const int columns = 8;

	cout << "Seats " ;
	for(int k = 0 ; k < 12; k++) //loop to display nums 0 to 12
	{
		cout << fixed<< setw(2) << " " << k ;
	}

	for (int i = 0; i < rows; i++)//making array display what's in it
	{
		cout << endl<< "Row " << i;
		for (int j = 0;j < columns; j++)
		{
			cout << setw(3) << "" << EMPTY;
		}
	}
	cout << endl;
}
Line 64: you declare the variable
Line 118: you use the variable even though you have never assigned a value to it.

Learn to use your IDE's integrated debugger. (Although in this case all I did was Ctrl+F "selection" and I spotted the problem immediately)
Last edited on
Topic archived. No new replies allowed.