Shouldn't that switch supposed to take the age input from a user and compare it before the output? Pls, how can this be done? | Sololearn: Learn to code for FREE!
+ 1

Shouldn't that switch supposed to take the age input from a user and compare it before the output? Pls, how can this be done?

from the example given on switch control structure.

3rd Jun 2017, 12:00 PM
DANIEL DANEE
2 odpowiedzi
+ 17
Talking about this..?? int age = 42;. /*Age is declared along with value*/ switch (age) { case 16: cout << "Too young"; break; case 42: cout << "Adult"; break; case 70: cout << "Senior"; break; } For user to give input and switch it.. you can modify it as.. int age;. //only variable declared.. cin>>age;. //value taken from user switch (age) { case 16: cout << "Too young"; break; case 42: cout << "Adult"; break; case 70: cout << "Senior"; break; }
3rd Jun 2017, 12:20 PM
Frost
Frost - avatar
0
#include<iostream> using namespace std; /*For accepting user input, You just have to define the variable later through user input. It can be done as follows*/ int main() { int age; /*Here our variable is "age".This is where you can opt for user input or preset it, i.e the variable "age" has no preset value(such as int age=42; or whatever etc), hence we have to give it a value thus we ask for user input*/ cout<<"Enter the age"<<endl; //This is command ask for the user to input the value, note: <<endl; cin>>age; /*This command accepts the input , which is then forwarded to switch statement for the next part of the program to execute*/ switch(age){ case 16: cout<<"Teen"; break; case 42: cout<<"Adult"; break; case 70: cout<<"Senior"; break; } }
11th Jun 2017, 11:01 AM
Beda Kayum Kuli
Beda Kayum Kuli - avatar