c++ - Character constant too long for its type using switch case -


i wish use more 1 character in case statement whole 'cosech' in program:

#include <iostream>      #include <math.h>       using namespace std; int main() {     char select[10];     cout<<"enter value of angle in degrees";float angle;     cin>>angle;     cout<<"choose trigonometric function \ntype cosech function cosech()";     cin>>select;     switch(select[0]<<9)     {         case 'cosech':            cout<<"the cosech of angle "<<angle<<" = "<<1/sinhf(angle);            break;     }     return 0; } 

the compiler gives following error

line 10 character constant long type

you can't. c++, in common c, not support using switch statements in way.

however, not cause of error. single quotes ' used character constants, i.e., single letter. should use double quotes " string literals. however, still not work, cannot use string in switch statement.

two options are:

  1. come single character abbrievations

    char c; cin>>c; switch (c) {    case 't':   //handle tan   break;   case 'c':  //handle cos } 

    etc. perhaps use lower case , upper case distinguish between tan , tanh

  2. use chain of if / else if statements, , use strcmp compare string supported options:

    if(!strcmp(select, "cosech") {     //handle cosech } else if(!strcmp(select, "tan") {     //handle tan } 

etc.

i have no idea trying achieve this: switch(select[0]<<9) isn't doing want.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -