### 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:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125`` ``````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: "<>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<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<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<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
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))`

// 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..)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249`` ``````#include #include #include #include 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>set; cout< 100)&&(set<200)) //for a coach ticket. { random = (rand() + time(0)) % 2; rowPos = (rand() + time(0)) % 2; cout<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<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<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
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

 ``12345`` ``````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:
 ``12345678910111213141516171819`` ``````#include 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.