Airplane seating reservation

Hello,
This is my code for reserving an airplane seat. Im having trouble writing the cancel a reservation function

-(Cancel Reservation): The program calls a function that prompts users for the seat to be canceled as a string (e.g., 3A, 7D, etc.) and then determines the row number and column number of the array element that needs to be accessed. If the seat is available, the program displays an error message stating that the seat to be canceled has not been initially reserved. Otherwise it makes the seat available and displays a message stating that the seat reservation has been canceled.
o You should specify aisle, center and window seats during this option. After this operation
o Your program should also display the current status of the seat availability;




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

using namespace std;
const int ROWS = 30;
const int COLS = 6 + 3;


void SeatChart(ifstream& eds, string seatChart[][COLS])
{

	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			if (eds.eof()) //if eof, break out of loop 
				break;
			eds >> seatChart[i][j]; //fills array with data from file
			cout << seatChart[i][j] << " ";
			if (seatChart[i][j] == "F")  //endline everytime the letter d is in the array
				cout << endl;
		}
	}
}

void Reserve(string seatChart[][COLS], char row, char column)
{
	cout << "Enter a the row (1-30) and seat (A-F) [Example: 5B]:";
	cin >> row >> column;

	for (int x = 0; x < ROWS; x++)			// int x = 0
		for (int y = 0; y < COLS; y++)
			if (x == row)
			{
				if (seatChart[x][y][0] == toupper(column))
				{
					seatChart[x][y] = 'X';
					cout << "Seat was successfully reserved!" << endl;
				}

			}
}

void Cancel(string seatChart[][COLS], char col, char row)
{
		
}

void SeatChartFile(ofstream& pds, string seatChart[][COLS])
{
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)  //simple function to put data in array to the file 
		{
			pds << seatChart[i][j];
			pds << " ";
			if (j == 6) //everytime theres a new row, its a new line
				pds << endl;
		}
	}
}

void Stats()
{

}

void Help()
{
	cout << "Hello, seems like youre having trouble with you program." << endl;
	cout << "Pressing the letter (A) or a will give you a look at all the seats on the plane thats arer reserved and unreserved." << endl;
	cout << "Want to reserve a seat! press (B) and first input the coulumn number 1-30 and a letter A-F for the row seat." << endl;
	cout << "Option (C) will let you cancel a reservation by just inputting the seat you initially reserved." << endl;
	cout << "Option (D) will save the seating chart to a file you input." << endl;
	cout << "Option (E) will list all the statistics to the user." << endl;
	cout << "This option (F) will help you understand the program and how to work the program." << endl;
	cout << "Lastly Option (G) will the quit the program" << endl;

}

void Quit()
{
	cout << "Thank You!" << endl;
	cout << "Enjoy your Flight" << endl;
	system("pause");
	exit(0); //Exit the program
}


int main()
{
	string seatChart[ROWS][COLS];
	int row = 0;
	char col = NULL;
	char choice;

	ifstream eds;
	ofstream pds;
	string outputfile;
	eds.open("planeSeats.txt");

	bool repeat = true;

	while (repeat = true)
	{

		cout << "------------------------ Menu ---------------------------" << endl;
		cout << "A.Display Seat Chart" << endl;
		cout << "B.Reserve Seat" << endl;
		cout << "C.Cancel Reservation" << endl;
		cout << "D.Save Seat Chart to File" << endl;
		cout << "E.Statistics" << endl;
		cout << "F.Help" << endl;
		cout << "G.Quit" << endl;
		cout << "------------------------------------------------------------" << endl;
		cout << "Please Enter Your Choice (A-G):";
		cin >> choice;

		switch (toupper(choice))
		{
		case 'A':
			SeatChart(eds, seatChart);
			system("pause");
			break;

		case 'B':
			Reserve(seatChart, col, row);
			system("pause");
			break;

		case 'C':
			Cancel(seatChart, col, row);
			system("pause");
			break;

		case 'D':
			cout << "What is the name of the file you want to save the data to:";
			cin >> outputfile;
			pds.open(outputfile);
			SeatChartFile(pds, seatChart);
			pds.close();
			system("pause");
			break;

		case 'E':
			Stats();
			system("pause");
			break;

		case 'F':
			Help();
			system("pause");
			break;

		case 'G':
			Quit();

			break;

		}
		eds.close();
	}

}
Line 7: Why is COLS 9? At line 29 you says enter A-F (which is 6).

Line 27, 45: You're inconsistent in your use of row. Here it is a char while elsewhere it is an int.

Line 35: Do not loop on (! stream.eof()) or (stream.good()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> (or getline) operation as the condition in the while statement.
1
2
3
  while (stream >> var) // or while (getline(stream,var))
  {  //  Good operation
  }


Lines 32-33: There is no need for a double for loop here. You can compute y from col-'A'.

Line 21,36: Your if statement is faulty. You're comparing the contents of the seat with the column. That makes no sense.

Line 45: Why are row and col arguments? You're not passing meaningful values to the functions.

Line 93: a 2 dimension string array for for a seat chart is wasteful. a 2 dimension char array is sufficient.

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
void Cancel(string seat[][COLS])
{   int     row;
    char    col;   

    whille (true)
    {   cout << "Enter row of seat to cancel: ";
        cin >> row;
        if (row < 1 || row > 30)
        {   cout << "That is not a valid row" << endl;
            continue;
        }
        row--;  // Adjust for 0 based array
        cout << "Enter col (A-F) of seat to cancel: ";
        cin >> col;
        col = toupper(col);
        if (col <  'A' || col > 'F')
        {   cout << "That is not a valid col" << endl;
            continue;
        }
        col -= 'A';
        if (seat[row][col] != "X")
        {   cout << "That seat has not been reserved" << endl;
            continue;
        }
        seat[row][col] = " ";   //  Mark seat as empty
        return;
    }        
}



Last edited on
Thank You!! I went ahead and fix everything.
However when I enter a seat into reserve seat. It does not update the text file is there any way I could do that and update the text File with an X for the reserved seat? Also, how would I set up the statistics function?

(statistics): The program displays the following statistics:
o Number of available seats
o Number reserved seats
o Percentage of seats that are reserved
o Percentage of seats that are available
o List of window seats that are available
o List of aisle seats that are available
o List of center seats that are available
o Number of seats available in the aisle
o Number of seats available in the window
o Number of seats available in the center
• By clicking on this option, your program should automatically save all statistics into a file. Remember to specify what information is being displayed. The file name format should follow the given structure YearMonthDay_Statistics.txt;
o Use as many functions do you want for this option;

Thank You again!
All the statistics can be determine by examining the seatChart array.

o Number of available seats - count the seats where " ".
o Number reserved seats - count the seats where "X".
o Percentage of seats that are reserved - calculate reserved seats / total seats. Be sure and scale the result and don't use integer arthmetic.
o Percentage of seats that are available - available seats / total seats.
o List of window seats that are available - Examine the seat chart. Display where A or F not reserved.
o List of aisle seats that are available - Display where C or D not reserved.
o List of center seats that are available - Display where B or E not reserved.
o Number of seats available in the aisle - Count where C or D not reserved.
o Number of seats available in the window - Count where A or F not reserved.
o Number of seats available in the center - Count where B or D not reserved.
Topic archived. No new replies allowed.