+ 3

What's wrong with this code?!!

#include <iostream> using namespace std; int main() { int age=18; if (age > 18){ if (age==18) { if (age == 20) { cout<<"you are adult"; } } } if (age > 0) { if (age < 18 ) { cout<<"you are a child" } } else { cout<<"something went wrong"; } return 0; }

17th Sep 2017, 6:49 PM
Lukman Nasir
Lukman Nasir - avatar
17 Réponses
+ 6
@Lukman wat result do u want
17th Sep 2017, 7:21 PM
David Akhihiero
David Akhihiero - avatar
+ 6
If u want "you are an adult" to print, that wont work because first: age is not > 18 and age is not = 20
17th Sep 2017, 7:23 PM
David Akhihiero
David Akhihiero - avatar
+ 6
I repeat : If u want "you are an adult" to print, that wont work because first: age is not > 18 and age is not = 20
17th Sep 2017, 7:37 PM
David Akhihiero
David Akhihiero - avatar
+ 6
Lukman u can combine as many as u want in most languages
18th Sep 2017, 5:22 AM
David Akhihiero
David Akhihiero - avatar
+ 4
Try this #include <iostream> using namespace std; int main() { int age=18; if (age > 18){ { if (age==18) { if (age == 20) { cout<<"you are adult"; } } } if (age > 0) { if (age < 18 ) { cout<<"you are a child" } } } else { cout<<"something went wrong"; } return 0; } EDIT: #include <iostream> using namespace std; int main() { int age=18; if (age >= 18){ if (age==18) { //NOT NECESSARY if (age == 20) { cout<<"you are adult"; } } // } if (age > 0) { if (age < 18 ) { cout<<"you are a child"; } } else { cout<<"something went wrong"; } return 0; }
17th Sep 2017, 7:03 PM
David Akhihiero
David Akhihiero - avatar
+ 3
@Yerucham just the same!!!
17th Sep 2017, 7:05 PM
Lukman Nasir
Lukman Nasir - avatar
+ 3
Yep that's what I'm talking about
17th Sep 2017, 7:26 PM
Lukman Nasir
Lukman Nasir - avatar
+ 3
I was just trying the nested if
17th Sep 2017, 7:27 PM
Lukman Nasir
Lukman Nasir - avatar
+ 3
So, in C# can you inculde as many && as you want in the if statement
18th Sep 2017, 3:57 AM
Lukman Nasir
Lukman Nasir - avatar
+ 2
@Tobias Hallmans but can't you see it. i've wrote: if (age==18) { cout <<"you are adult": }
17th Sep 2017, 6:56 PM
Lukman Nasir
Lukman Nasir - avatar
+ 2
@Ace so can I write else (Statement) { cout <<"......"; }
17th Sep 2017, 6:58 PM
Lukman Nasir
Lukman Nasir - avatar
+ 2
So helpful! thank you so much 😊☺☺
17th Sep 2017, 7:01 PM
Lukman Nasir
Lukman Nasir - avatar
+ 1
You ask whether age is strictly greater then 18. If so, you ask if it's equal to 18. That can never happen here.
17th Sep 2017, 6:52 PM
1of3
1of3 - avatar
+ 1
You're missing a semicolon here: cout<<"you are a child"; <-- here The output as the code is currently written will be nothing not "something went wrong".
17th Sep 2017, 6:56 PM
ChaoticDawg
ChaoticDawg - avatar
0
that part of the code is never reached as it will be skipped by the surrounding if statement. 18 > 18 is false
17th Sep 2017, 6:57 PM
ChaoticDawg
ChaoticDawg - avatar
0
why do you not use logical "and(&&) " and "or(||)". for example : if (age>0 && age <18) . or in c++ it does not work( i learn c#)
17th Sep 2017, 8:28 PM
Artyr
Artyr - avatar
- 1
Don’t know
4th Oct 2017, 5:40 PM
CAGEMAN
CAGEMAN - avatar