Code Clean Up Help

Good Afternoon,
I am trying to clean up my code and what I would like to do is find a way to turn the following bit of code into a function. Everything works as intended but I have the same lines of code repeating 3 times because I cannot get them to work inside a function. The code that is repeated follows immediately with the entire program shortly after the break.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
case ECONOMY:
			economySeats(seats);
			cout << "Please select your seat from those available, as indicated by a '*', please select your row : ";
			cin >> r;
			cout << "Please select your aisle : ";
			cin >> aisle;
			aisleConversion(aisle, c);
			if (seats[r - 1][c] == true)
			{
				cout << "That seat is occupied." << endl;
			}
			else if (seats[r - 1][c] == false)
			{
				cout << endl << "You have selected to sit in seat " << r << " in aisle " << aisle << "." << endl;
				seats[r - 1][c] = true;
			}
			businessClassSeats(seats);
			break;


Here is the entire program:
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

#include <iostream> //allows use of cout and endl commands
#include <iomanip>  // allows use of manipulation commands (setprecision, ect)


using namespace std; //This line allows you to use cin, cout and endl without the std:: prefix
// function declaration so function can be put after main
void menu(); 
void openSeats(bool seats[][6]);
void firstClassSeat(bool seats[][6]);
void businessClassSeats(bool seats[][6]);
void economySeats(bool seats[13][6]);
int aisleConversion(char aisle, int& c);

//static variables to avoid global variables
static int choice;
static int r;
static int c;
static char aisle;
bool seats[13][6] = {	{0, 0, 1, 0, 1, 1},//Row 1
						{0, 1, 0, 1, 0, 1},//Row 2
						{0, 0, 1, 1, 0, 1},//Row 3
						{1, 0, 1, 0, 1, 1},//Row 4
						{0, 1, 0, 1, 0, 0},//Row 5
						{0, 1, 0, 0, 0, 1},//Row 6
						{1, 0, 0, 0, 1, 1},//Row 7
						{0, 1, 0, 1, 1, 0},//Row 8
						{1, 0, 1, 1, 0, 1},//Row 9
						{0, 1, 0, 1, 1, 1},//Row 10
						{0, 0, 1, 0, 1, 0},//Row 11
						{0, 0, 1, 1, 0, 1},//Row 12
						{0, 0, 0, 0, 1, 0} };//Row 13

//menu choices
static const int DISPLAYSEATS = 1, FIRSTCLASS = 2, BUSINESSCLASS = 3, ECONOMY = 4, QUIT = 9;
int main()//heading of the function main, every program must have a main function
{
	//info line
	cout << "This program allows you to pick a seat for your upcoming flight. " << endl;
	cout << endl;

	//start of loop
	do
	{
		menu(); //show menu function
		switch (choice) //switch start
		{
		//cases to execute functions
		case DISPLAYSEATS: 
			openSeats(seats);
			break;

		case FIRSTCLASS:
			firstClassSeat(seats);
			cout << "Please select your seat from those available, as indicated by a '*', please select your row : ";
			cin >> r;
			cout << "Please select your aisle : ";
			cin	>> aisle;
			aisleConversion(aisle, c);
			if (seats[r - 1][c] == true)
			{
				cout << "That seat is occupied." << endl;
			}
			else if (seats[r - 1][c] == false)
			{
				cout << endl << "You have selected to sit in seat " << r << " in aisle " << aisle << "." << endl;
				seats[r - 1][c] = true;
			}
			firstClassSeat(seats);
			cout << endl;
			break;

		case BUSINESSCLASS:
			businessClassSeats(seats);
			cout << "Please select your seat from those available, as indicated by a '*', please select your row : ";
			cin >> r;
			cout << "Please select your aisle : ";
			cin >> aisle;
			aisleConversion(aisle, c);
			if (seats[r - 1][c] == true)
			{
				cout << "That seat is occupied." << endl;
			}
			else if (seats[r - 1][c] == false)
			{
				cout << endl << "You have selected to sit in seat " << r << " in aisle " << aisle << "." << endl;
				seats[r - 1][c] = true;
			}
			businessClassSeats(seats);
			break; 

		case ECONOMY:
			economySeats(seats);
			cout << "Please select your seat from those available, as indicated by a '*', please select your row : ";
			cin >> r;
			cout << "Please select your aisle : ";
			cin >> aisle;
			aisleConversion(aisle, c);
			if (seats[r - 1][c] == true)
			{
				cout << "That seat is occupied." << endl;
			}
			else if (seats[r - 1][c] == false)
			{
				cout << endl << "You have selected to sit in seat " << r << " in aisle " << aisle << "." << endl;
				seats[r - 1][c] = true;
			}
			businessClassSeats(seats);
			break;
		}
	} while (choice != QUIT); //if 9 is entered quits the program
	system("pause"); //pauses the command window to see program running
	return 0; //ends the program
}

void menu()// sets function type as one that is not returning data to the program, only displays the menu choices, all text
{
	cout << endl;
	cout << "Menu Choices: " << endl;
	cout << endl;
	cout << "Press 1 to display all open seats: " << endl;
	cout << "Press 2 to reserve a first class seat: " << endl;
	cout << "Press 3 to reserve a business class seat: " << endl;
	cout << "Press 4 to reserve an economy class seat: " << endl;
	cout << "Press 9 to exit the program: " << endl;
	cout << "Please enter a selection: ";
	cin >> choice; //variable for selection from menu function
}
void openSeats(bool seats[][6])//shows seat menu
{
	cout << endl << "The current status of the seats on this flight are: . " << endl; 
	
	cout << setw(10) << "A" << setw(5) << "B" << setw(5) << "C" << setw(5) << "D" << setw(5) << "E" << setw(5) << "F" << endl;
	for (size_t row = 0; row < 13; ++row) 
	{
		cout << "Row " << row + 1;
		for (size_t col = 0; col < 6; ++col) 
		{
		if (row < 9)
			cout << setw(5) << (seats[row][col] == false ? '*' : 'X');
		else 
			if (col == 0)
				cout << setw(4) << (seats[row][col] == false ? '*' : 'X');
			else if(col > 0)
				cout << setw(5) << (seats[row][col] == false ? '*' : 'X');
		}
		cout << '\n';
	}
	cout << endl << "Seats available for occupancy are indicated by a *. " << endl;
}
void firstClassSeat(bool seats[][6])
{
	cout << endl << "The following seats are available for reservation: " << endl;
	cout << setw(10) << "A" << setw(5) << "B" << setw(5) << "C" << setw(5) << "D" << setw(5) << "E" << setw(5) << "F" << endl;
	for (size_t row = 0; row < 2; ++row)
	{
		cout << "Row " << row + 1;
		for (size_t col = 0; col < 6; ++col)
			cout << setw(5) << (seats[row][col] == false ? '*' : 'X');
		cout << '\n';
	}
	cout << endl << "Seats available for occupancy are indicated by a *. " << endl;
	}

void businessClassSeats(bool seats[][6])
{
	cout << endl << "The following seats are available for reservation: " << endl;
	cout << setw(10) << "A" << setw(5) << "B" << setw(5) << "C" << setw(5) << "D" << setw(5) << "E" << setw(5) << "F" << endl;
	for (size_t row = 3; row > 2 && row < 8; ++row)
	{
		cout << "Row " << row + 1;
		for (size_t col = 0; col < 6; ++col)
			cout << setw(5) << (seats[row][col] == false ? '*' : 'X');
		cout << '\n';
	}
	cout << endl << "Seats available for occupancy are indicated by a *. " << endl;
}

void economySeats(bool seats[13][6])
{
	cout << endl << "The current status of the seats on this flight are: . " << endl;
	cout << setw(10) << "A" << setw(5) << "B" << setw(5) << "C" << setw(5) << "D" << setw(5) << "E" << setw(5) << "F" << endl;
	for (size_t row = 8; row > 7 && row < 13; ++row)
	{
		cout << "Row " << row + 1;
		for (size_t col = 0; col < 6; ++col)
		{
			if (row < 9)
				cout << setw(5) << (seats[row][col] == false ? '*' : 'X');
			else
				if (col == 0)
					cout << setw(4) << (seats[row][col] == false ? '*' : 'X');
				else if (col > 0)
					cout << setw(5) << (seats[row][col] == false ? '*' : 'X');
		}
		cout << '\n';
	}
	cout << endl << "Seats available for occupancy are indicated by a *. " << endl;
}
int aisleConversion(char aisle, int& c)
{
	switch (aisle)
	{
	case 'A':
		c = 0;
		break;
	case 'B':
		c = 1;
		break;
	case 'C':
		c = 2;
		break;
	case 'D':
		c = 3;
		break;
	case 'E':
		c = 4;
		break;
	case 'F':
		c = 5;
		break;
	}
	return c;
}
its up to you to handle r and c being out of bounds, but something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void assignseats(bool seatsp[13][6], int r, int c, int aisle)
{	
   	if (!seatsp[r - 1][c])			
			{
				cout << endl << "You have selected to sit in seat " << r << " in aisle " << aisle << "." << endl;
				seatsp[r - 1][c] = true;
				return;
			}
				cout << "That seat is occupied." << endl;		
}


//called by :
assignseats(seats, 2,3, aisle); //etc



this is the trouble with 2-d (and higher) arrays. they are aggravating to use in functions.
Last edited on
Create a reserveSeat() function that takes the start and end rows to display. Then menu options 2-4 become calls to reserveSeat.

Change openSeats() to take start and end row numbers to support reserveSeat.

OpenSeats() logic can be simplified.

AisleConversion() can be done with arithmetic instead of a case statement.


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
#include <iostream>				 //allows use of cout and endl commands
#include <iomanip>				 // allows use of manipulation commands (setprecision, ect)

using namespace std;		//This line allows you to use cin, cout and endl without the std:: prefix
// function declaration so function can be put after main
void menu();
void openSeats(bool seats[][6], int from, int to);
void reserveSeat(int fromRow, int toRow);
int aisleConversion(char aisle, int &c);

//static variables to avoid global variables
static int choice;
static int r;
static int c;
static char aisle;
bool seats[13][6] = { {0, 0, 1, 0, 1, 1},	 //Row 1
{0, 1, 0, 1, 0, 1},				 //Row 2
{0, 0, 1, 1, 0, 1},				 //Row 3
{1, 0, 1, 0, 1, 1},				 //Row 4
{0, 1, 0, 1, 0, 0},				 //Row 5
{0, 1, 0, 0, 0, 1},				 //Row 6
{1, 0, 0, 0, 1, 1},				 //Row 7
{0, 1, 0, 1, 1, 0},				 //Row 8
{1, 0, 1, 1, 0, 1},				 //Row 9
{0, 1, 0, 1, 1, 1},				 //Row 10
{0, 0, 1, 0, 1, 0},				 //Row 11
{0, 0, 1, 1, 0, 1},				 //Row 12
{0, 0, 0, 0, 1, 0}
};						 //Row 13

//menu choices
static const int DISPLAYSEATS = 1, FIRSTCLASS = 2, BUSINESSCLASS = 3, ECONOMY = 4, QUIT =  9;
int
main()				//heading of the function main, every program must have a main function
{
    //info line
    cout << "This program allows you to pick a seat for your upcoming flight. " << endl;
    cout << endl;

    //start of loop
    do {
	menu();					 //show menu function
	switch (choice)				 //switch start
	{
	    //cases to execute functions
	case DISPLAYSEATS:
	    openSeats(seats, 0, 12);
	    break;

	case FIRSTCLASS:
	    reserveSeat(0, 1);
	    break;

	case BUSINESSCLASS:
	    reserveSeat(2, 8);
	    break;

	case ECONOMY:
	    reserveSeat(9, 12);
	    break;
	}
    } while (choice != QUIT);			 //if 9 is entered quits the program
    system("pause");				 //pauses the command window to see program running
    return 0;					 //ends the program
}

void
menu()				// sets function type as one that is not returning data to the program, only displays the menu choices, all text
{
    cout << endl;
    cout << "Menu Choices: " << endl;
    cout << endl;
    cout << "Press 1 to display all open seats: " << endl;
    cout << "Press 2 to reserve a first class seat: " << endl;
    cout << "Press 3 to reserve a business class seat: " << endl;
    cout << "Press 4 to reserve an economy class seat: " << endl;
    cout << "Press 9 to exit the program: " << endl;
    cout << "Please enter a selection: ";
    cin >> choice;				 //variable for selection from menu function
}

//shows seat menu for rows "from" to "to" (inclusive)
void
openSeats(bool seats[][6], int from, int to)
{
    cout << endl << "The current status of the seats on this flight are: . " << endl;

    cout << setw(11) << "A" << setw(5) << "B" << setw(5) << "C" << setw(5) << "D" <<
	setw(5) << "E" << setw(5) << "F" << endl;
    for (int row = from; row <= to; ++row) {
	cout << "Row " << setw(2) << row + 1;
	for (size_t col = 0; col < 6; ++col) {
	    cout << setw(5) << (seats[row][col] == false ? '*' : 'X');
	}
	cout << '\n';
    }
    cout << endl << "Seats available for occupancy are indicated by a *. " << endl;
}

void
reserveSeat(int fromRow, int toRow)
{
    openSeats(seats, fromRow, toRow);
    cout <<
	"Please select your seat from those available, as indicated by a '*', please select your row : ";
    cin >> r;
    cout << "Please select your aisle : ";
    cin >> aisle;
    aisleConversion(aisle, c);
    if (seats[r - 1][c] == true) {
	cout << "That seat is occupied." << endl;
    } else if (seats[r - 1][c] == false) {
	cout << endl << "You have selected to sit in seat " << r << " in aisle " << aisle
	    << "." << endl;
	seats[r - 1][c] = true;
    }
    cout << endl;
}

int
aisleConversion(char aisle, int &c)
{
    c = toupper(aisle) - 'A';
    return c;
}
Topic archived. No new replies allowed.