How will it output teenager!!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

How will it output teenager!!!

https://code.sololearn.com/ca25j2hiXQ7o/?ref=app. (from ex by sololearn)

18th May 2018, 5:38 PM
Abhay
Abhay - avatar
10 Answers
+ 4
Here is something I wrote awhile back for someone asking the same thing. Hope it helps. In your case, you simply have a value that's hardcoded, rather than being received as input from the user, so it's always 18, which is adult. Either change the variable value prior to running it or accept input from user. https://code.sololearn.com/cmKy9026j10L/#cpp #include <iostream> using namespace std; int main() { int age; cout << "enter age\n"; cin >> age; if(age >= 0 && age <= 1) { // 0-1 Baby cout << "Baby"; } else if(age > 1 && age <= 3) { // 2-3 Toddler cout << "Toddler"; } else if(age > 3 && age <= 12) { // 4-12 Child cout << "Child"; } else if(age > 12 && age <= 17) { // 13-17 Teen cout << "Teen"; } else if(age > 17 && age <= 64) { // 18-64 Adult cout << "Adult"; } else if(age > 64) { // 65+ Elder cout << "Elder"; } return 0; }
18th May 2018, 5:52 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 4
that's not the point fatal 1 error ,anyway thks
18th May 2018, 6:06 PM
Abhay
Abhay - avatar
+ 4
shadow thks for answering but if I remove the line [if age >14], shouldn't it output teenager instead of child !!
18th May 2018, 6:08 PM
Abhay
Abhay - avatar
+ 4
oh wait I think it's ignores the first if else statement when I input 14 after removing Frist if statement,and outputs child and if I input 15,16,17 in the original code it checks if it greater than 14,then proceeds to check if it's greater than or equal to 18 and if it's true it outputs adult otherwise teenager!!am I right?
18th May 2018, 6:26 PM
Abhay
Abhay - avatar
+ 4
lol ,Fatal1 error I try to write code in every lesson ,anyway I was just trying to understand if I remove first if statement why was it giving me errors :-|
19th May 2018, 12:55 PM
Abhay
Abhay - avatar
+ 3
It's hard to know your point since you were vague with your question. However, as mentioned, you simply have a value that's hardcoded, rather than being received as input from the user, so it's always 18, which is adult. Either change the variable value prior to running it or accept input from user. As Shadow said, change the value of your variable to something other than 18 and you'll get the result based upon your conditions. Shadow never said to remove your IF condition. ::::BASED ON YOUR CODE::: #include <iostream> using namespace std; int main() { int age = 15; if (age > 14) { if(age >= 18) { cout << "Adult"; } else { cout << "Teenager"; } } else { if (age > 0) { cout << "Child"; } else { cout << "Something's wrong"; } } return 0; } :::: OUTPUT ::::: Teenager ^As you can see, your variable 'age' is what's holding onto the value that's being checked in your IF statements. If you change that, it's a new value being checked against the conditions. Per your conditions, 15-17 is considered teenager to you. If you have it accept user input instead, each time you run it you can give it a new age and be able to test your conditions more easily.
18th May 2018, 6:22 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
Yup. If you put 14, then when it goes to "age > 14" it would be false because it is equal to 14 instead of greater than. It's starting to dawn on me that you didn't write the code, which explains some things. :D
18th May 2018, 6:30 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
fatal 1 ErrOr sorry imao I think I am not able to make my point properly !!
18th May 2018, 7:07 PM
Abhay
Abhay - avatar
+ 3
I was kinda confused how will it procceed step by step if you want all the outputs when you have different inputs
18th May 2018, 7:10 PM
Abhay
Abhay - avatar
+ 1
Change the value of "age" to 15, 16 ot 17 and it will output "Teenager" ...
18th May 2018, 5:42 PM
Blank
Blank - avatar