+ 2

Please tell me why this code is not run!!!!šŸ˜¢šŸ˜¢

#include<iostream> include namespace std; int main() {int age; cout<<"enter age"; cin>>age; switch (age) {case 16: cout<<"young"; break; case 30: cout<<"adult"; break; } return 0; }

21st Feb 2018, 4:40 PM
Sujay Kundu
Sujay Kundu - avatar
2 Answers
+ 8
Many issues... First one being that you put 0 effort into formatting your code. Sure, that's not why it isn't running, but it would have been nice for the people you're asking help from. Second issue, you'll want to use 'using namespace std;' instead of include. Third issue, SWITCH doesn't deal with ranges very well, so unless you input 16 or 30, you won't receive output. It is ONLY checking those two numbers and not the numbers in between. This situation is best suited for an IF/ELSE statement. EXAMPLE: 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) { cout << "Baby"; } else if(age > 1 && age <= 3) { cout << "Toddler"; } else if(age > 3 && age <= 12) { cout << "Child"; } else if(age > 12 && age <= 17) { cout << "Teen"; } else if(age > 17 && age <= 64) { cout << "Adult"; } else if(age > 64) { cout << "Elder"; } return 0; }
21st Feb 2018, 4:55 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 10
#include<iostream> using namespace std; int main() {int age; cout<<"enter age"; cin>>age; switch (age) {case 16: cout<<"young"; break; case 30: cout<<"adult"; break; } return 0; } you should do like this using namespace std;
21st Feb 2018, 4:47 PM
GAWEN STEASY
GAWEN STEASY - avatar