How to use switch statement to read multi character string and output accordingly | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to use switch statement to read multi character string and output accordingly

how to use switch to read multi character strings.? how should i initialize tht variable string that is to be inputted Eg: #include <iostream> using namespace std; int main() { char reply[20]; cout<<"do u give up and agree tht you failed? Reply yes or no"; cin>>reply; switch (reply) { case 'yes':cout<<"γ๑บ αяε รαƒε"; break; case 'no':cout<<"you are dead my boy" ; break; default: cout<<"reply properly";} }

20th Nov 2018, 4:18 AM
Abhishek +×
Abhishek +× - avatar
3 Answers
+ 2
Switch statement in C++ works for data types with integral values only. Instead of strings, try using enum.
20th Nov 2018, 4:47 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Alternatively you can use switch (reply[0]) { case 'y': case 'Y': // safe processing break; case 'n': case 'N': // dead processing break; default: // response did not start with y or n // process response message break; }
8th Mar 2019, 2:25 PM
Roger Greenlaw
0
Thankz for replying. Im new to this and luckily i dont know how to use enum. I will learn that on the way. But can you plz share a sample program to use enum if have done..
20th Nov 2018, 6:24 AM
Abhishek +×
Abhishek +× - avatar