Problem With Two Dimensional Arrays

I'm writing a program for school that has the user input the cost of a plane ticket, and the program outputs where the user will sit on the plane, based on the hundreds and tens place. Whenever I go to enter the cost, however, the cost gets ignored and the user gets put in seat[5][3], where the aisle is. What did I do wrong?
More info: I'm using a two dimensional array, and a series of if loops. This part of the program is in a function, if that helps.
Here's the code:

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
void getSeat(int set, char seat[ROWS][COLUMNS])
{
	int rows = 0; //rows
	int columns = 0; //columns
	int rowPos = 0;
	int random = 0;  //to determine wether the person sits on the left or right side of the plane.
         int cost = 0;
	//cout<<"rowPos: "<<rowPos<<endl;
        cout<<"Please enter the ticket cost: $";
        cin>>cost;
        set = cost;
	//coach class
	rows = 0;
	rowPos = 0;
	if ((set > 100)||(set<200))
	{
		random = (rand() + time(0)) % 2; 
		//rowPos = (rand() + time(0)) % 2;
		rows = (rowPos + 5);
		cout<<rows<<endl;
		if ((set>140)||(set<170))
		{
			//if (random = 0)
			//	columns = 5;
			//else if (random = 1)
			//	columns = 3;
			seat[rows][columns] = 'X';
		}
		else if ((set>=170)||(set<199)) //window seat
		{
			if (random = 0)
				columns = 6;
			else if (random = 1)
				columns = 0;
			seat[rows][columns] = 'X';
		}
		else if ((set<140)||(set>100))
		{
			//if (random = 0)
			//	columns = 2;
			//else if (random = 1)
			//	columns = 4;
			seat[rows][columns] = 'X';
		}
		rows = 0;
		rowPos = 0;
	}
	//business class, ticket cost >200, 300
	else if ((set >= 200)||(set<300))
	{
		random = (rand() + time(0)) % 2; 
		//rowPos = (rand() + time(0)) % 2;
		rows = (rowPos+2);
		cout<<rows<<endl;
		if ((set>240)||(set<270))
		{
			//if (random = 0)
			//	columns = 5;
			//else if (random = 1)
			//	columns = 1;
			seat[rows][columns] = 'X';
		}
		else if ((set>=270)||(set<299))
		{
			//if (random = 0)
			//	columns = 6;
			//else if (random = 1)
			//	columns = 0;
			seat[rows][columns] = 'X';
		}
		else if ((set<240)||(set>200))
		{
			//if (random = 0)
			//	columns = 2;
			//else if (random = 1)
			//	columns = 4;
			seat[rows][columns] = 'X';
		}
		rows = 0;
		rowPos = 0;
	}
	//first class
	else if ((set >= 300)||(set<400))
	{
		random = (rand() + time(0)) % 2; 
	//	rowPos = (rand() + time(0)) % 2;
		rows = rowPos;
		cout<<rows<<endl;
		if ((set>340)||(set<370)) //aisle seat
		{
			//if (random = 0)
			//	columns = 5;
			//else if (random = 1)
			//	columns = 3;
			seat[rows][columns] = 'X';
		}
		else if ((set>=370)||(set<399)) //windows seat
		{
			//if (random = 0)
			//	columns = 6;
			//else if (random = 1)
			//	columns = 0;
			seat[rows][columns] = 'X';
		}
		else if ((set<340)||(set>300))
		{
			//if (random = 0)
			//	columns = 2;
			//else if (random = 1)
			//	columns = 4;
			seat[rows][columns] = 'X';
		}
	}
	for(int i = 0;i<ROWS;i++)
	{
		cout<<"Row "<<i<<": ";
		for(int c= 0;c<COLUMNS;c++)
		{
			cout<<seat[i][c];
		}
		cout<<endl;
		rows = 0;
		rowPos = 0;
	}
}
Last edited on
I would need the whole code to compile it.

to guess, I would say the problem is
if ((set > 100)||(set<200))
should be
if ((set > 100)&&(set<200))

you have no comments like
// this is for a 100-200 ticket
so i'm guessing at what your trying to do.
I saw this error yesterday, so I went to fix it. Now the program outputs nothing to the array. All available spots are still empty. And there isn't much code besides this, but I'll provide it anyways. (I've removed the intro text. It had nothing in it in the first place, besides telling the user what it'll do..)

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <iostream>
#include <windows.h>
#include <ctime>
#include <cstdlib>

using namespace std;

const int ROWS = 7;
const int COLUMNS = 7;

void intro();
char initialize(char[ROWS][COLUMNS]);
void output(char[ROWS][COLUMNS]);
int getCost(int);
void getSeat(int, char[ROWS][COLUMNS]);

int main()
{
	char seats[ROWS][COLUMNS]; //rows, columns
	int cost = 0;
	
	//introduction
	intro();
	//initialize variables, create outFile
	initialize(seats);
	output(seats);
	//get user input (in form of ticket cost, hundreds determines class, tens determines seat position
	getCost(cost);
	//calculate the user input-use some nested if loops
	getSeat(cost, seats);
	//save it to outfile
	//display table of where person sits
	

	return 0;
}

void intro()
{
	//intro text
}

char initialize(char set[ROWS][COLUMNS])
{
	for(int i = 0;i<ROWS;i++)
	{
		for(int c= 0;c<COLUMNS;c++)
		{
			set[i][c] = 'O';
		}
		cout<<endl;
	}
	return set[2][3];
}

void output(char set[ROWS][COLUMNS])
{
	for(int i = 0;i<ROWS;i++)
	{
		for(int c= 0;c<COLUMNS;c++)
		{
			cout<<set[i][c];
		}
		cout<<endl;
	}
	cout<<endl;
}
int getCost(int set)
{
	set = 0;
	cout<<"Please enter the cost of your ticket: $";
	//set = 0;
	cin>>set;
	cout<<set<<endl;
	return set;
}
void getSeat(int set, char seat[ROWS][COLUMNS])
{
	int rows = 0; //rows
	int columns = 0; //columns
	int rowPos = 0;
	int random = 0;
	//to determine wether the person sits on the left or right side of the plane.
	//cout<<"rowPos: "<<rowPos<<endl;
	//coach class
	rows = 0;
	rowPos = 0;
	//coach class seat, cost more than 100 but less than 200.
        if ((set > 100)&&(set<200)) //for a coach ticket.
	{
		random = (rand() + time(0)) % 2; 
		rowPos = (rand() + time(0)) % 2;
		cout<<rowPos<<endl;
		rows = (rowPos + 5);
		cout<<rows<<endl;
		if ((set>140)&&(set<170)) //aisle seat
		{
			if (random = 0){
				columns = 5;}
			else if (random = 1){
				columns = 3;}
			seat[rows][columns] = 'X';
		}
		else if ((set>=170)&&(set<199)) //window seat
		{
			if (random = 0){
				columns = 6;}
			else if (random = 1){
				columns = 0;}
			seat[rows][columns] = 'X';
		}
		else if ((set<140)&&(set>100)) //middle seat
		{
			if (random = 0){
				columns = 2;}
			else if (random = 1){
				columns = 4;}
			seat[rows][columns] = 'X';
		}
		rows = 0;
		rowPos = 0;
	}
	//business class, ticket cost >200, 300
	else if ((set >= 200)&&(set<300))
	{
		random = (rand() + time(0)) % 2; 
		rowPos = (rand() + time(0)) % 2;
		rows = (rowPos+2);
		cout<<rows<<endl;
		cout<<rowPos<<endl;
		if ((set>240)&&(set<270)) //aisle seat
		{
			if (random = 0){
				columns = 5;}
			else if (random = 1){
				columns = 1;}
			seat[rows][columns] = 'X';
		}
		else if ((set>=270)&&(set<299)) //window seat
		{
			if (random = 0){
				columns = 6;}
			else if (random = 1){
				columns = 0;}
			seat[rows][columns] = 'X';
		}
		else if ((set<240)&&(set>200)) //middle seat
		{
			if (random = 0){
				columns = 2;}
			else if (random = 1){
				columns = 4;}
			seat[rows][columns] = 'X';
		}
		rows = 0;
		rowPos = 0;
	}
	//first class
	else if ((set >= 300)&&(set<400))
	{
		random = (rand() + time(0)) % 2; 
		rowPos = (rand() + time(0)) % 2;
		cout<<rowPos<<endl;
		rows = rowPos;
		cout<<rows<<endl;
		if ((set>340)&&(set<370)) //aisle seat
		{
			if (random = 0){
				columns = 5;}
			else if (random = 1){
				columns = 3;}
			seat[rows][columns] = 'X';
		}
		else if ((set>=370)&&(set<399)) //windows seat
		{
			if (random = 0){
				columns = 6;}
			else if (random = 1){
				columns = 0;}
			seat[rows][columns] = 'X';
		}
		else if ((set<340)&&(set>300)) //middle seat
		{
			if (random = 0){
				columns = 2;}
			else if (random = 1){
				columns = 4;}
			seat[rows][columns] = 'X';
		}
	}
	/*if((seat<400)||()) //first class, commented out to begin re-writing it.
	{
		if() //window seat
		{
			
		}
		if() //aisle seat
		{
			
		}
		if() //middle seat
		{
			
		}
	}
	
	else if (()||()) //business class
	{
		if() //window seat
		{
			
		}
		if() //aisle seat
		{
			
		}
		if() //middle seat
		{
			
		}
	}

	else if(()&&()) //coach
	{
		if() //window seat
		{
			
		}
		if() //aisle seat
		{
			
		}
		if() //middle seat
		{
			
		}
	}*/
	for(int i = 0;i<ROWS;i++) //outputs the array.
	{
		cout<<"Row "<<i<<": ";
		for(int c= 0;c<COLUMNS;c++)
		{
			cout<<seat[i][c];
		}
		cout<<endl;
		rows = 0;
		rowPos = 0;
	}
}
Last edited on
Something I noticed in your conditional statements is that you aren't using the operator ==. You are using the operator =.

= is the assignment operator
== is a conditional operator to determine equality

1
2
3
4
5
int someNumber = 5;

someNumber = 6;     //Assigns 6 to someNumber
//is not the same as
someNumber == 6;     //Tests someNumber to see if it is equal to 6 
The operator == doesn't apply with the conditional statements.
To be sure, though, I changed the = in the conditionals to ==, and it broke the program. It wouldn't compile. Thanks for the help, though!
closed account (D80DSL3A)
cost still = 0 when you pass it to getSeat() on line 30 in main().
Either pass cost by reference to the getCost() (not by value) or use the return value from the function to assign cost.

EDIT: This is why out (type) values in C# make sense to me.
Last edited on
The operator == doesn't apply with the conditional statements.
To be sure, though, I changed the = in the conditionals to ==, and it broke the program. It wouldn't compile. Thanks for the help, though!


Assignment operators in conditional operators do indeed work as new cppUser pointed out. If you use if(x = y) your statement will always be true as you are setting x to equal y. In order to compare the two you would need to use if(x==y).

If changing this stops your program from compiling then you've done something else wrong.

If you won't take our word for it, run the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
    int x=1;

    if(x==1)
    {
        cout << "x == 1\n";
    }
    if(x=2)
    {
        cout << "x = 2\n";
    }
    cout << "x now equals: " << x << endl;
    return 0;
}
The operator == doesn't apply with the conditional statements.
To be sure, though, I changed the = in the conditionals to ==, and it broke the program. It wouldn't compile. Thanks for the help, though!


What an if() tests for is whether the value inside the parenthesis is true or false. To C++, 0 is false and any other integer is true (1, 3, -2, etc.)

Essentially, when you are saying:
if (x=1)
you are saying
if (true)
because
if (x=1)
becomes
if (x)
which in this case is
if (1)
which evaluates to if(true) because 1 is a non-zero integer.

However, when you have
if (x==1)
you are asking "Is x equal to the integer 1?".
It will then test the condition. It will return true if they are equal or false if they are not equal.

Edit:
Just because your program compiles does not mean your code is not logically wrong. It just means there are no compile-time errors.
Last edited on
I have solved it, and it works using the operator =. It had something to do with my conditionals being logically wrong, and I ended up re-writing the code.

EDIT: I've never ran into the problem that new cppUser pointed out, even while using x=1 instead of x==1.
Last edited on
Topic archived. No new replies allowed.