Help with a c++ code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Help with a c++ code

I made a code for naming the age: https://code.sololearn.com/cRjx536WluKW/?ref=app I made a mistake i guess, because it outputs "Adult" everytime nomatter what the input is. Could someone help me? Obviosly I don't see the mistake.

25th Jan 2018, 5:37 AM
Marius Frost
Marius Frost - avatar
7 Answers
+ 9
// Here is a workable version. #include <iostream> using namespace std; int main() { //int x; unneccessary variable int age = 0; cin >> age; //{ if (age >= 14) { if(age >= 18) { cout << "Adult"; } else { cout << "Teenager"; } } else { if (age > 0) { cout << "Child"; } else { cout << "Something's wrong"; } } //} }
25th Jan 2018, 5:46 AM
Babak
Babak - avatar
+ 9
That's right, you also should be careful about braces and nesting too many conditional statements. But, your code is well-written and quite readable, my friend.
25th Jan 2018, 6:25 AM
Babak
Babak - avatar
+ 4
Remove int x and just cin to age. int age; cin >> age;
25th Jan 2018, 5:42 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Thanks😀
25th Jan 2018, 6:32 AM
Marius Frost
Marius Frost - avatar
+ 2
Well that was fast and easy thanks!
25th Jan 2018, 5:45 AM
Marius Frost
Marius Frost - avatar
+ 2
So my mistake was that i added a var x that was not like my var age so that there was no input for var age i guess
25th Jan 2018, 6:04 AM
Marius Frost
Marius Frost - avatar
+ 1
Well that was fast and easy thanks!
25th Jan 2018, 5:45 AM
Marius Frost
Marius Frost - avatar