+ 1

Need Help!!

Hi there, i was trying to create this if statement using the example in the course but changed it so that you could input you age and then it will write it back to you the age you put in an wither or not your an Adult, Teenager or Child. I thought that I had coded it correctly but when I go to run it, I get a return the console of no input if I input a number and don't press return and click submit. or if i put in a number and press return and then press submit i get loads of error messages which don't make sense because i though i had done the stuff they had asked me to do. hears all of my code and if there is something i am doing wrong then i would love it if someone could help. #include <iostream> using namespace std; int main(){ int age; cout << "Enter your age to see if you are an Adult, Teenager or child: " << endl; cin >> age; if(age >= 13){ If (age >= 18){ cout << "Your age is " << age << " that makes you an adult!!"; }else(age <= 17);{ cout << "Your age is " << age << " that makes you an Teenager!!"; } }else;{ if(age<13){ cout << "Your age is " << age << " that makes you an Child!!"; }else;{ cout << "somethiing went wrong"; } } return 0; }

9th Jan 2019, 3:35 PM
Alasdair .Anderson
Alasdair .Anderson - avatar
4 ответов
+ 6
Have you figured it out yet? well, in case you haven't, I had checked your code, commented the problems, and add a little note underneath, see if it works. #include <iostream> using namespace std; int main() { int age; cout << "Enter your age to see if you are an Adult, Teenager or child: " << endl; cin >> age; if(age >= 13) { if (age >= 18) { /* If (age >= 18){ */ cout << "Your age is " << age << " that makes you an adult!!"; } else if(age <= 17) { /* }else(age <= 17);{ */ cout << "Your age is " << age << " that makes you an Teenager!!"; } } else { /* }else;{ */ if(age >= 1) { /* if(age < 13) { */ cout << "Your age is " << age << " that makes you an Child!!"; } else { /* }else;{ */ cout << "somethiing went wrong"; } } return 0; } // @ 11: "if" was written "If" // @ 13, 16: semicolon before opening bracket { // @ 17: changed condition, since it already // is within block for age < 13 Hth, cmiiw
9th Jan 2019, 7:01 PM
Ipang
+ 2
You're welcome Alasdair : ) Now, allow me to explain a little about the "if(age >= 1)"; if(age >= 13) { // "if" block is executed if // <age> larger than or equal // to 13 } else { // "else" block is executed if // <age> is less than 13 } The "else" block is only executed if input <age> is less than 13, that's why it wasn't necessary to recheck if <age> is less than 13 inside the "else" block. And the reason why I put "if(age >= 1)" was to check, if input <age> makes sense, that way when input <age> was less than 1 it will process the block which prints "something went wrong", we shouldn't accept zero or negative value for <age>, it's not right : )
11th Jan 2019, 2:37 PM
Ipang
+ 1
Thank you very much for you help I am able to run it now but an numbers 12 and under hose strate to something went wrong stage
11th Jan 2019, 12:40 PM
Alasdair .Anderson
Alasdair .Anderson - avatar
+ 1
Don't worry I fixed it and it is all now working thank you very much for you help I think it for some reason didn't like the 'if(age >=1) so i just kept it as if(age < 13) and it seemed to work. This is how the finaly code truned out. #include <iostream> using namespace std; int main(){ int age; cout << "Enter your age to see if you are an Adult, Teenager or child: " << endl; cin >> age; if(age >= 13){ if (age >= 18){ cout << "Your age is " << age << " that makes you an adult!!"; } else if (age <= 17){ cout << "Your age is " << age << " that makes you an Teenager!!"; } }else{ if(age<13){ cout << "Your age is " << age << " that makes you an Child!!"; }else{ cout << "somethiing went wrong"; } } return 0; }
11th Jan 2019, 12:47 PM
Alasdair .Anderson
Alasdair .Anderson - avatar