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:
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..)
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!
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.
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>
usingnamespace 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.
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.