+ 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<<"ď˝ď˝ď˝ăď˝ď˝ď˝ ăď˝ď˝ ď˝ď˝ăď˝ď˝ăď˝ď˝ď˝" ; break; default: cout<<"reply properly";} }
3 Answers
+ 2
Switch statement in C++ works for data types with integral values only. Instead of strings, try using enum.
+ 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;
}
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..