+ 2

Why this program showing the result "too young" instead of " adult" ???

#include <iostream> using namespace std; int main() { int age = 42; if (age=16) { cout<<"too young";} else if ( age=42) { cout << "Adult";} return 0; }

10th Jul 2017, 10:06 PM
mahmood
mahmood - avatar
3 Respostas
+ 11
To check equality, you *must* use ==. Here, you're using single =, which assigns 16 to age. Any value that isn't 0 is evaluated as true, so the first if runs.
10th Jul 2017, 10:10 PM
Tamra
Tamra - avatar
+ 9
yw :3
10th Jul 2017, 10:12 PM
Tamra
Tamra - avatar
+ 2
got it , thank you
10th Jul 2017, 10:12 PM
mahmood
mahmood - avatar