Display a menu and then use an option in that menu to display a text file

//I'm having trouble figuring out how to open the file "chartIn.txt" in my menu under option one. I am able to get the menu to display but when I press "option 1" nothing happens.
Can anyone tell me how to fix this?



#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

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

char chart[ROWS][COLS];

ifstream ins;

void readChart();

void displaySeatChart();
void reserveSeat();
void cancelReservation();
void saveSeatChartToFile();
void statisticsOption();
void helpOption();
void quitOption();


int main()
{

int choice;

ins.open("chartIn.txt");

readChart();

ins.close();

do
{
cout << "----------------------------Menu----------------------------" << endl;
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: ";
cin >> choice;
cout << "----------------------------" << endl;

switch (choice)
{
case 1:
displaySeatChart();
break;
case 2:
reserveSeat();
break;
case 3:
cancelReservation();
break;
case 4:
saveSeatChartToFile();
break;
case 5:
statisticsOption();
break;
case 6:
helpOption();
break;
default:
quitOption();
}

} while (choice != 6);

return 0;
}


void readChart()
{
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()
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
cout << chart[i][j] << " ";

cout << endl;
}
}
Last edited on
@dtinsman

Note.. Please use code tags to display codes in the future.

Here's what I used as the text, "chartIn.txt", to be read in.
First the row number, followed by 'A' for an Adult in the seat, or 'C' for a Child. Not sure what you were using, as you didn't say, or hint at it.

1
2
3
4
5
6
7
8
0
A A C C
1
A A A C
2
A A C C
3
C C C C


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
// Chart In.cpp : main project file.

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

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


char chart[ROWS][COLS];
void readChart(char chart[ROWS][COLS]);
void displaySeatChart(char chart[ROWS][COLS]);
void reserveSeat();
void cancelReservation();
void saveSeatChartToFile();
void statisticsOption();
void helpOption();
void quitOption();

ifstream ins;

int main()
{

	int choice;

	ins.open("chartIn.txt");

	readChart(chart);

	ins.close();

	do
	{
		cout << "----------------------------Menu----------------------------" << endl;
		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: ";

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

		cin >> choice;

		switch (choice)
		{
		case 1:
			displaySeatChart(chart);
			break;
		case 2:
			reserveSeat();
			break;
		case 3:
			cancelReservation();
			break;
		case 4:
			saveSeatChartToFile();
			break;
		case 5:
			statisticsOption();
			break;
		case 6:
			helpOption();
			break;
		case 7:
			quitOption();
		}

	} while (choice != 7);

	return 0;
}


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

	while (ins.good())
	{
		ins >> rowNumber; // Text file has row number. No need to reduce by one
		// Otherwise, you could be going out of array bounds
		ins >> seat1 >> seat2 >> seat3 >> seat4;
		chart[rowNumber][0] = seat1;
		chart[rowNumber][1] = seat2;
		chart[rowNumber][2] = seat3;
		chart[rowNumber][3] = seat4;
	}
}


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

void reserveSeat()
{

}

void cancelReservation()
{

}

void saveSeatChartToFile()
{

}

void statisticsOption()
{

}

void helpOption()
{

}

void quitOption()
{

}

Thanks for your response. Sorry about my code, it was my first time posting and I wasn't sure how to do the correct format.

Notes:
*I am on a mac using Xcode (apparently Xcode has many problems with the fstream library?)
*My text file is a basic airplane seating chart. 1-10 are rows and letters A-D symbolize the seats in the rows.
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

When I compiled my code, nothing at all came up when I called the function to display the seating chart.
Although, when I ran yours something did come up but not the text file that I displayed above...

All it returned was:
row 0:
row 1:
row 2:
row 3:
row 4:
row 5:
row 6:
row 7:
row 8:
row 9:

I'm wondering if you or anyone else has any idea as to why this is happening? By the way I've tried all the suggestions for changing the settings in Xcode, the file location and everything else I could find and still none of this works.
Last edited on
@dtinsman

Sorry, I won't be much help, as I am not on a Mac, so don't know how to overcome these problems. Hopefully, someone more versed in this OS will lend a hand.
Topic archived. No new replies allowed.