reading integers from file in to an array

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
# include <iostream>
# include <iomanip>
# include <fstream>
using namespace std;

void seats(double[], int);
void mapSeats();

int main()
{
	const int rowNum = 15;
	double rowValue[rowNum]; //array to hold row pices
	char selection;
	int row2, col2;
	const char TAKEN = '#';//seats taken
	const char EMPTY = '*';//seats free
	const int row = 15;//number of rows
	const int col = 20;//number of col
	char map[row][col];//array to hold seat chart 
	ifstream file;



	for (int i = 0; i<row; i++)//initiating array 
	{
		for (int j = 0; j<col; j++)
		{
			map[i][j] = EMPTY;
		}
	}
	cout << "* Seats are available" << endl;
	cout << "# Reserved seats" << endl;
	mapSeats();

	seats(rowValue, rowNum);//ask user to enter price of each row
	cout << endl;

	do
	{

		cout << "MOVIE THEATER MENU" << endl;
		cout << "------------------" << endl;
		cout << "1) Sell a ticket" << endl;
		cout << "2) Total sell and exit" << endl;
		cout << "Enter your choice: ";
		cin >> selection;

		if (selection == '1')
		{
			cout << "Enter row:";
			cin >> row2;
			cout << endl;
			cout << "Enter seat:";
			cin >> col2;
			cout << endl;

			// Check if seat is free
			if (map[row2][col2] == TAKEN)
			{
				cout << "Invalid seat choice. \n";
				continue; // start the loop again
			}
			else // and if it is - sell the ticket
				map[row2][col2] = TAKEN;

			// Add the next loop to immediately see the effects:
			for (int i = 0; i < row; i++)
			{
				for (int j = 0; j < col; j++)
				{
					cout << map[i][j];
				}
				cout << endl;
			}


		}
		else if (selection == '2')
		{
			cout << "UPDATED SEATING CHART AND SALES INFO" << endl;
			mapSeats();
			cout << "TOTAL TICKETS SOLD: " << endl;
			cout << "TOTAL REVENUE: "<< endl;
		}
		else
		{
			cout << "Invalid choice." << endl;
		}
	} while (selection != '1' || selection != 'q' || selection != 'Q');


	system("PAUSE");
	return 0;
}


void seats(double rowPrice[], int row)
{
	fstream file;
	const int size = 4;
	char data[size];
	file.open("prices.txt", ios::in | ios::binary);
	file.read(data, sizeof(data));

	for (int i = 0; i < row; i++)
	{
		cout << data[i] << " " << endl;;
	}
}

void mapSeats()
{
	const char TAKEN = '#';//seats taken
	const char EMPTY = '*';//seats free
	const int rw = 15;
	const int cl = 20;

	cout << "Seats ";
	for (int k = 0; k <20; k++) //loop to display nums 0 to 14
	{
		cout << fixed << "" << setw(2) << k << " ";
	}

	for (int i = 0; i<rw; i++)//making array display what's in it
	{
		cout << endl << "Row " << setw(2) << i;
		for (int j = 0; j<cl; j++)
		{
			cout << fixed << " " << EMPTY << " ";
		}
	}
	cout << endl;
}




this is a movie theater seating program that sells seats to user. It's almost done. I just need to fix the part where i need to read a list of integers in a txt file (prices.txt) and save them into an array to save the prices for each row.



after that, I need to be able to save all the seats sold and the total revenue made. can somebody pls show me how to code properly for these two issues?
Last edited on
Topic archived. No new replies allowed.