Change character in a Initiliazed array?

Hey everyone, my problem with this code is that i have a char array[15][30].
The program sells tickets and u may purchase a seat based on if its avaliable or not. '#' = Avaliable and '*' = means its not.
My problem out putting is not the problem, I dont know how to let the user enter the Rows and Cols to purchase a specific seat AND how to change THAT specific element in the 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
#include <iostream>
#include <iomanip>
using namespace std;
const int Row = 15;
const int Col = 30;
void showChart(char [Row][Col]);
void updateChart(char [Row][Col], const int [][COW], int);
int main()
{
	//compare the two arrays subscripts with loop counters
	//then change that subscript 2d array to "#";
	int i, j, k, totalamt = 0, choice, totaltickets = 0;
	int R, C;
	double Price[15];
	int selection[R][C];
	char Show[Row][Col] = {{ '#', '#', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '#', '*', '*', '*', '*', '*'},
		{'*', '#', '#', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '#', '*'},
		{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '#', '*'},
		{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '#', '*'},
		{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
	};
	/*for (int i = 0; i < row; i++)
	{
		cout << "Enter the price for row " << ( i + 1 ) << ".\n";
		cin >> Price[i];
		totalamt += Price[i];
	}*/
	showChart(Show);
	cout << "Enter the number of tickets you wish to purchase\n";
	cin >> choice;
	for (int k = 1 ; k <= choice;k++)
	{
		cout << "\nEnter the row:\t";
		cin >> R;
		cout << "\nEnter the column:\t";
		cin >> C;
		selection[R][C] = k;
		updateChart(Show, selection, 15);
		totaltickets++;
	}
	showChart(Show);
	return 0;
}
void showChart(char Show[Row][Col])
{
	int i, j;
	int rows = 0;
	for (int i = 0; i < Row;i++)
	{
		for (int j = 0; j < Col;j++)
		{
			rows++;//will match max row value which is 15 and then print a NEWLINE
			cout << setw(3) << Show[i][j] << " ";
			if ( rows % Row == 0)
				cout << endl;
		}
	}
}
void updateChart(char Show[Row][Col], const int testArray[][COW], int ROW)
{
	int i, j;
	for (int i = 0; i < Row;i++)
	{
		for (int j = 0; j < Col;j++)
		{
			if ( Show[i][j] == testArray[ROW][COL])
			{
				Show[i][j] = '#';
			}
		}
	}
}

Last edited on
Why do you have two arrays (selection and show)?

 
int selection[R][C];

How much storage do you expect that statement to allocate?
Hint: R and C have undefined values. i.e. You can not allocate an array of size R,C when R and C are not known.

I dont know how to let the user enter the Rows and Cols to purchase a specific seat AND how to change THAT specific element in the array.

Simply ask the user for R and C, check if show[R][C] is available and change the symbol to indicate it is taken.

1
2
3
4
5
6
7
8
9
10
    cout << "\nEnter the row:\t";
    cin >> R;
    cout << "\nEnter the column:\t";
    cin >> C;
    if (show[R][C] == '*')
        cout << "Sorry, that seat is taken" << endl;
    else
    {  show[R][C] = '*';
        cout << "That seat has been reserved" << endl;
    }


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Hint: You can edit your previous post, highlight your code and press the <> formatting button.
Hey Anon, thanks for responding, i was over thinking that process I thought I needed to have two arrays compare each other for some reason.
Here is my updated code which is pretty much complete.
As a beginner it is very easy to complicate the simple things due to a lack of experience.



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
//7.17
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
const int Row = 15;
const int Col = 30;
int numSold(char[Row][Col]);
void showChart(char [Row][Col]);
void viewticketAmount(int);
void updateChart(const char [][30], int, int);
void avaliableRow(char [Row][Col]);
void avaliableAuditorium(char[Row][Col]);
int main()
{
	char Again;
	int Seatssold, Seatotal, Showchart1;
	int i, j, k, totalamt = 0, choice, totaltickets = 0, viewticket, viewseats, Numsold, Avaliableseats;
	int Rowava, Ticketsales, Aud;
	double Price[15];
	int R, C;
	char Show[Row][Col] = {{ '#', '#', '#', '#', '#', '*', '*', '*', '#', '#', '#', '#', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '#', '*', '*', '*', '*', '*'},
		{'*', '#', '#', '#', '*', '*', '*', '#', '#', '*', '*', '*', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '#', '*'},
		{ '*', '#', '#', '*', '*', '*', '*', '#', '#', '*', '*', '*', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '#', '*'},
		{ '*', '*', '#', '*', '*', '*', '*', '#', '#', '*', '*', '*', '#', '#', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '#', '*'},
		{ '*', '*', '#', '*', '*', '*', '*', '#', '#', '*', '*', '*', '#', '*', '#', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '#', '*', '*', '*', '*', '#', '#', '*', '*', '*', '#', '*', '*', '#', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '#', '#', '*', '*', '*', '#', '#', '*', '*', '*', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '*', '#', '*', '*', '*', '#', '#', '*', '*', '*', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '*', '#', '*', '*', '*', '*', '*', '*', '*', '*', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '*', '*', '*', '#', '#', '#', '*', '*', '*', '*', '*', '#', '#', '*', '*', '*', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '#', '*', '*', '*', '*', '#', '*', '*', '*', '*', '*', '#', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '#', '*', '*', '*', '*', '#', '*', '*', '*', '*', '*', '#', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '#', '*', '*', '*', '*', '#', '*', '*', '*', '*', '*', '#', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '#', '#', '#', '#', '#', '#', '*', '*', '*', '*', '*', '#', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'},
		{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '*', '*', '#', '*', '#', '#', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '#'},
	};
	showChart(Show);
	do
	{
		cout << "Hit any key to continue\n";
		getch();
		system("cls");
		for (int i = 0; i < 15; i++)
		{
			cout << "\nEnter the price for row\t" << (i + 1) << ".\n";
			cin >> Price[i];
			totalamt += Price[i];
		}
		cout << "Enter the number of tickets you wish to purchase\n";
		cin >> choice;
		for (int k = 1 ; k <= choice;k++)
		{
			totaltickets++;
			cout << "\nEnter the row:\t";
			cin >> R;
			if (R < 0 || R > 15)
			{
				cout << "\nEnter the row,it cant be greater than 15 or less than 0:\t";
				cin >> R;
			}
			cout << "\nEnter the column:\t";
			cin >> C;
			if ( C < 0 || C > 30)
			{
				cout << "\nEnter the column:\t";
				cin >> C;
			}
			updateChart(Show, R, C);
			cout << "\nThe price for all the tickets  purchased are " << totalamt << ".";
		}
		cout << "\nEnter 1 to view how many Tickets were sold.\n";
		cin >> Ticketsales;
		if (Ticketsales == 1)
		{
			viewticketAmount(totaltickets);
		}
		cout << "\nHit any key to proceed with the program\n";
		getch();
		cout << "\nEnter  1 to see the total amount of seats sold.\n";
		cin >> Seatssold;
		if (Seatssold == 1)
		{
			Seatotal = numSold(Show);
			cout << "\nThe number of seats sold are " << Seatotal << ".\n";
		}
		cout << "\nEnter 1 to view seats avaliable in each row.\n";
		cin >> Rowava;
		if (Rowava == 1)
		{
			avaliableRow(Show);
		}
		cout << "\nEnter 1 to view how many seats are avaliable throughout the entire auditorium.\n";
		cin >> Aud;
		if ( Aud == 1)
		{
			avaliableAuditorium(Show);
		}
		cout << "\nEnter 1 to view chart again\n";
		cin >> Showchart1;
		if (Showchart1 == 1)
		{
			showChart(Show);
		}
		cout << "\nEnter Y to purchase another set\n";
		cin >> Again;
	}
	while (Again == 'Y');
	return 0;
}
void showChart(char Show[Row][Col])
{
	int i, j;
	int cols = 0;
	for (int i = 0; i < Row;i++)
	{
		for (int j = 0; j < Col;j++)
		{
			cols++;//will match max row value which is 15 and then print a NEWLINE
			cout << setw(1) << Show[i][j] << " ";
			if ( cols % Col == 0)
				cout << endl;
		}
	}
}
void updateChart(const char Show[][30], int R, int C)
{
	if (Show[R][C] == '*')
	{
		cout << "Sorry, that seat is taken.\n";
	}
	else
		Show[R][C] = '*';
	cout << "That seat has been reserved.\n";
}
void viewticketAmount(int totalTickets)
{
	cout << "\nThe total amount of tickets sold in this transaction is\t" << totalTickets << "\n";
}
int numSold(char Show[Row][Col])
{
	int i, j;
	int Totalsold = 0;
	for (int i = 0; i < Row;i++)
	{
		for (int j = 0; j < Col;j++)
		{
			if (Show[i][j] == '*')
			{
				Totalsold++;
			}
		}
	}
	return Totalsold;
}
void avaliableRow(char Show[Row][Col])
{
	int i, j;
	int Totalrow;
	for (int i = 0; i < Row;i++)
	{
		Totalrow = 0;
		for (int j = 0; j < Col;j++)
		{
			if ( Show[i][j] == '#')
			{
				Totalrow++;
				cout << "\nRow " << (i + 1) << " has " << Totalrow << "avaliable seats.\n";
			}
		}
	}
}
void avaliableAuditorium(char Show[Row][Col])
{
	int i, j;
	int Avaliable = 0;
	for (int i = 0; i < Row;i++)
	{
		for (int j = 0; j < Col;j++)
		{
			if ( Show[i][j] == '#')
			{
				Avaliable++;
			}
		}
	}
	cout << "\nThe amount of avaliable seats in the  auditorium is " << Avaliable << "\n";
}

When i update my array with the logic u have shown me, it doesnt update the origional char array of '#'.

2) also for the part of the logic that wants avaliable row seats and avaliable total auditorium seats i am having a hard time formatting the output to have it just as
a) Row 1 has "5" avaliable seats
it keeps going like Row 1 has 2 seats avaliable
Row 1 has 3 seats avaliable
its in a accumlating format where as I just want
Row 1 has 5 seats avaliable
Row 2 has 15 seats avaliable
ectt
(all of this is in the function)
Last edited on
When i update my array with the logic u have shown me, it doesnt update the origional char array of '#'.


Line 126: You've declared Show as const char. That's telling the compiler you're not going to modify Show. Your compiler should have given you an error at line 133 when you attempted to assign a value to a const array.

i am having a hard time formatting the output

Line 168 is in the wrong place. You're currently doing the cout every time you find an available seat. Move line 168 to after line 170 so it displays only after you've counted all the available seats in the row.




Last edited on
Oh ok I see now. I thought const char wouldn't matter.
I fixed all the output.
The very LAST thing of this assignment is that it "wants a list of how many seats have been sold". This is confusing because when they mention a list I am not sure how the correct answer would be a reasonable "list".

Last edited on
"wants a list of how many seats have been sold".

I agree that's confusing. That can be interpreted as "A list of the seats that have been sold", or "How many seats have been sold".

Assuming this is a class problem, I'd ask your instructor for clarification on that statement as it is ambiguous English. If you can't get clarification, then I'd suggest doing both. Iterate through Seats listing the row and col of each sold seat, and at the same time incrementing a counter of the number of sold seats and then at the end reporting the number of sold seats.
This is a practice project. I can just do whatever since its so vague. Thanks for your help Anon.
Topic archived. No new replies allowed.