Determining Rows and Cols in an Array

My program is supposed to be a seat reservation system for a passenger airplane.
I am having trouble with option 2 "Reserve Seat." The function should ask the user for the desired seat number(1-10)(A-D):

1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
8 A B C D
9 A B C D
10 A B C D

and then it should point to the spot in the array where that seat is located ("This corresponds to row 1 and column 1 in the array").

But, when I run the program--it always executes the "else" in my function no matter what I input for the desired seat. It says:
"Invalid seat!"
"This corresponds to row -1 and column -1 in the array"

Can anyone help me with this? Are there errors in my code?


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
#include <iostream> 
#include <fstream> 
#include <iomanip> 
#include <stdlib.h>
#include <string>

using namespace std; 

const int ROWS = 10;
const int COLS = 4;

char chart[ROWS][COLS];																	//array 
void readChart(char chart[ROWS][COLS]);													//function that reads .txt file 
void displaySeatChart(char chart[ROWS][COLS]);											//function that displays seating chart from .txt file
void reserveSeat(int&, int&, string);													//function that reserves seat in seating chart
void cancelReservation();																//function that cancels reservation in seating chart
void saveSeatChartToFile();																//function that saves seat chart to a file
void statisticsOption();																//function that enables user to see statistics
void helpOption();																		//function that displays a help page
void quitOption();																		//function that quits the program
void errorOption();																		//function that operates when there is a user error
void desiredSeat();																		//function that enables user to enter desired seat 

ifstream ins;

int main()
{
	ins.open("chartIn.txt");															//opens "chartIn.txt" for the program to read

	int choice, row, col;
	
	string seat;

	readChart(chart);																	//program reads "chartIn.txt" 

	ins.close();																		//program closes "chartIn.txt"

	do
	{
		cout << "-----------------------Menu----------------------" << endl;			//displays menu of options for the user to choose from
		cout << setw(30) << "1. Display Seat Chart                " << endl;
		cout << setw(30) << "2. Reserve Seat                      " << endl;
		cout << setw(30) << "3. Cancel Reservation                " << endl;
		cout << setw(30) << "4. Save Seat Chart To File           " << endl;
		cout << setw(30) << "5. Statistics                        " << endl;
		cout << setw(30) << "6. Help                              " << endl;
		cout << setw(30) << "7. Quit                              " << endl << endl;

		cout << "Please enter your choice (1-7): ";

		cin >> choice;

		cout << "--------------------------------" << endl;

		switch (choice)																	//switch enables user to choose from the different menu options 
		{
		case 1:
			displaySeatChart(chart);
			break;
		case 2:
			desiredSeat();
			reserveSeat(row, col, seat);
			cout << "This corresponds to Row " << row << " and column " << col << " in the array" << endl;
			break;
		case 3:
			cancelReservation();
			break;
		case 4: 
			saveSeatChartToFile();
			break;
		case 5:
			statisticsOption();
			break;
		case 6:
			helpOption();
			break;
		case 7:
			quitOption();
			break;
		default:
			errorOption();
		}
	} while (choice != 7);

	return 0;
}


void readChart(char chart[ROWS][COLS])
{
	int rowNumber;
	char seat1, seat2, seat3, seat4;

	while (!ins.eof())
	{
		ins >> rowNumber;
		ins >> seat1 >> seat2 >> seat3 >> seat4;
		chart[rowNumber - 1][0] = seat1;
		chart[rowNumber - 1][1] = seat2;
		chart[rowNumber - 1][2] = seat3;
		chart[rowNumber - 1][3] = seat4;
	}
}


void displaySeatChart(char chart[ROWS][COLS])
{
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
			cout << chart[i][j] << "  ";

		cout << endl;
	}
}



void reserveSeat(int& row, int& col, string seat)
{
	string fragment1, fragment2;

	if (seat.length() == 2)
	{
		fragment1 = seat.substr(0, 1);
		fragment2 = seat.substr(1, 1);
	}
	else if (seat.length() == 3)
	{
		fragment1 = seat.substr(0, 2);
		fragment2 = seat.substr(2, 1);
	}
	else
	{
		cout << "Invalid seat!" << endl;
		row = -1;
		col = -1;
	}

	row = atoi(fragment1.c_str()) - 1;

	switch (fragment2[0])
	{
	case 'A':
	case 'a':
		col = 0;
		break;
	case 'B':
	case 'b':
		col = 1;
		break;
	case 'C':
	case 'c':
		col = 2;
		break;
	case 'D':
	case 'd':
		col = 3;
		break;

	}

}



void cancelReservation()
{
	cout << "You selected Option 3." << endl << endl;
}



void saveSeatChartToFile()
{
	cout << "You selected Option 4." << endl << endl;
}



void statisticsOption()
{
	cout << "You selected Help." << endl << endl;
}



void helpOption()
{
	cout << "Thank you for using our seat reservation program." << endl;
	cout << "You can do any of the follwing: " << endl;
	cout << "Display the current seat chart (press 1, then hit enter). " << endl;
	cout << "Reserve a seat (press 2, then hit enter and follow the instructions). " << endl;
	cout << "Cancel a reservation (press 3, then hit enter and follow the instructions). " << endl;
	cout << "Save the seat chart to a file (press 4, then hit enter). " << endl;
	cout << "Display the statistics of the current seat chart (press 5 and then hit enter). " << endl;
	cout << "To quit the program, press 7, then hit enter." << endl << endl;
}



void quitOption()
{
	cout << "You have quit the program." << endl << endl;
}


void errorOption()
{
	cout << "Invalid choice. Please enter a valid number (1-7) and press enter." << endl << endl;
}


void desiredSeat()
{
	string seat;
	cout << "Please enter your seat number : ";
	cin >> seat;
}
Last edited on
This thread is continued from http://www.cplusplus.com/forum/general/162117/

Could you give an example of the input you are typing to your program?
"2B"
"4D"
"10C"

anything 1-10 and A-D (its a 10 row airplane with 4 seats in each row)
The variable on line 216 is not related to the variable on line 32. You use the variable from line 32 when you call the reserveSeat function on line 62. Since the variable on line 32 is never assigned a value, it is an empty string.

I recommend changing your function on line 214 to be a value returning function - it should return the string.
could you give me the syntax for that? I think I understand what you're saying but it would be extremely helpful if you could tell me the exact syntax to put on the exact lines (and which to delete if there is any).

Thanks!
Underline means I added or changed the code from your original code.
1
2
3
4
5
6
7
string desiredSeat()
{
	string seat;
	cout << "Please enter your seat number : ";
	cin >> seat;
	return seat;
}
Topic archived. No new replies allowed.