2-d array not outputting right letter

Hello. I'm working on a project where I basically have to create a seating chart reservation system. I don't understand when I use the reserve seat function why the seating chart doesn't display 'X' where the available seat used to be. For example, I choose option 2 from the menu, and reserve seat 1A. I then type 'y' to go back to the main menu and select option 2 again, but there is no 'X' in place of 'A' in the spot 1A. Why is this? What confuses me even more is that when you try to reserve the same spot again, it actually works and says that the seat is unavailable. Why then, does it not display 'X' in the spot that is unavailable?

Thanks for help!

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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

void readSettings(ifstream& chart, char theArray[10][4]);
void displayChart(char theArray[10][4]);
void reserveSeat(char theArray[10][4]);
void cancelReservation(char theArray[10][4]);
void createTxt(char theArray[10][4]);
void stats(char theArray[10][4]);
void help();
void exit();

int main() {

	ifstream chart("chart.txt");
	char seatingChart[10][4];

	readSettings(chart, seatingChart);

Start: // point to goto after operation is performed if user continues

	int option; // option 1-7 for user to pick
	char rpt; // for when user is prompted to run another operation or not
	cout << "----------Main Menu----------" << endl << endl;

	cout << "1.)  Display Seat Chart" << endl; 
	cout << "2.)  Reserve Seat" << endl;
	cout << "3.)  Cancel Reservation" << endl;
	cout << "4.)  Save Seat Chart to File" << endl;
	cout << "5.)  Statistics" << endl;
	cout << "6.)  Help" << endl;
	cout << "7.)  Quit" << endl << endl;
	cout << "-----------------------------" << endl << endl;
	cout << "Please enter your choice (1-7): ";

	cin >> option;

	cout << endl;

	if (option == 1) {

		displayChart(seatingChart);
	}
	else if (option == 2) {

		reserveSeat(seatingChart);
	}
	else if (option == 3) {

		cancelReservation(seatingChart);
	}
	else if (option == 4) {

		createTxt(seatingChart);
	}
	else if (option == 5) {

		stats(seatingChart);
	}
	else if (option == 6) {

		help();
	}
	else if (option == 7) {

		system("cls"); // closes the program
		goto End;
	}
	else {
		cout << "Error - Please select a vaild operation." << endl; //in case user enters something other than 1-7

	}

	cout << "Would you like to run another operation? Type Y for yes or N to close the program. ";
	cin >> rpt; //if the program will repeat

	if (rpt == 'Y' || rpt == 'y') {

		system("cls"); //closes current operation
		goto Start; // goes to the point "start" that was placed at beginning
	}


	else {
		exit(); // exit message
		exit(0); //closes program
	}
End: //point at which goto can be applied
	exit(); //exit message
	exit(0); //closes program


	return 0;
}


void readSettings(ifstream& chart, char theArray[10][4]) {


	for (int i = 0; i<10; i++)
	{
		for (int j = 0; j<4; j++)
		{
			chart >> theArray[i][j];
		}
	}

}
void displayChart(char theArray[10][4]) {

	int rowNum = 1;

	for (int i = 0; i<10; i++)
	{
		cout << rowNum << ". ";
		for (int j = 0; j<4; j++)
		{
			cout << theArray[i][j] << " ";
		}
		cout << endl;
		rowNum = rowNum + 1;
	}
}
void reserveSeat(char theArray[10][4]) {
	string choice;
	char seat = ' ';
	int row = 0;
	int rowNum = 1;

	cout << "Note: seats B and C are isle seats and A and D are window seats." << endl << endl;
	for (int i = 0; i<10; i++)
	{
		cout << rowNum << ". ";
		for (int j = 0; j<4; j++)
		{
			cout << theArray[i][j] << " ";
		}
		cout << endl;
		rowNum = rowNum + 1;
	}
	cout << endl << endl;

	cout << "Which seat would you like to reserve? Type the row number followed by the letter. (example: '1A') ";
	cin >> choice;
	
	row = choice[0] - 48; // don't know why, but its 48 more than it should be
	seat = choice[1];


	if (theArray[row][seat] != 'X') {
		cout << "Seat " << choice << " has successfully been reserved." << endl;
		theArray[row][seat] = 'X';

	}
	else {
		cout << "This seat is unavailable." << endl;
	}

	cout << endl << endl;
}
void exit() {
	cout << "Thank you for using the seating chart program!" << endl; 
Hello bubbagin,

FYI the beginning of the program four of your prototypes do not have matching function definitions. Not a problem until later in the program in your if statements you call those missing functions and get a compile error. You can either define an empty function for now or put a comment on the function calls that do not have a function definition yet.

The use of "goto"s is bad form and should be avoided. a "do while " loop will do the same thing. An example:

1
2
3
4
5
6
bool cont{true};  // <--- Initialized to true. Changed when exit is chosen.

do
{
    // code here
} while (cont);


Do a search on "goto" or maybe "why is goto bad". There are many posts here about "goto"s.

Your "if" statements work, but a "switch" would be a better choice.

http://www.cplusplus.com/doc/tutorial/control/#switch

Now the big problem is in the "reserveSeat" function.

You start with char seat when it needs to be int seat{ 0 };. Later on line 156 when you access the array the subscripts are "int" and "char" and that is why it is not working. After changing "seat" to an int you could do this:

1
2
3
4
5
6
7
	
    cin >> choice;

    choice[1] = toupper(choice[1]);  // <---Makes sure that the second element of string is an upper case letter

    row = choice[0] - 48; // don't know why, but its 48 more than it should be
    seat = choice[1] - 'A';


Since the input is a string each element of the string is a "char", so to get to a usable decimal "int" you subtract 48 or '0' and 65 or 'A'. Then on line 156 when you access the array you have the correct subscripts for the array, both "int"s. Next problem is the row number. You need to subtract 1 to access the correct row i.e., the first dimension. Other wise if the row is 10 this will be outside of your array bounds and the output will undetermined.

I think all that might be needed is to write the new array to the "chart.txt" file if you want to save the changes.

Hope that helps,

Andy
Last edited on
Hello bubbagin,

I had a revelation this morning. The program works fine for rows 1 - 9, but falls apart for line 10 because the string becomes > 2. Two thoughts on fixing this:

1. cin >> row >> letter;. Where letter is defined as a "char" then use the line seat = letter - 'A'; to get the number needed for the subscript.

2. Using an "if" statement with the condition to determine the length of the string "choice". You can keep what you have for the first part and the "else" part would us "stoi" to extract the number ten followed by seat = toupper(choice[2]) - 'A';.

3. You could make the "else" an else if (choice.length() == 3) then choice 2 between the {} then an "else" to handle any invalid input.

Thought about this so far, but I have not coded anything yet. Although I tend to lean more towards choices 2 and 3.

Hope that helps,

Andy
I was able to finish the project with your help.

Thank you so much!

bubba
Hello bubba,

Your are welcome. Any time.

Andy
Topic archived. No new replies allowed.