对于使用开关盒的类型,字符常量太长

时间:2015-05-15 14:28:36

标签: c++ char switch-statement

我希望在我的案例陈述中使用多个字符,就像整个'cosech'一样,在这个程序中:

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

编译器提供以下错误

  

第10行字符常量对于其类型

太长

2 个答案:

答案 0 :(得分:4)

你做不到。与C一样,C ++不支持以这种方式使用switch语句。

但是,这不是错误的原因。单引号'用于字符常量,即单个字母。您应该对字符串文字使用双引号"。但是,这仍然无效,因为您无法在switch语句中使用字符串。

有两个选项:

  1. 想出单一字符的消除

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

    等。您也许可以使用小写和大写来区分说 t T anh

  2. 使用if / else if语句链,并使用strcmp将字符串与支持的选项进行比较:

    if(!strcmp(select, "cosech")
    {
        //handle cosech
    }
    else if(!strcmp(select, "tan")
    {
        //handle tan
    }
    
  3. 我不知道你想用这个来实现什么:switch(select[0]<<9) 但它没有做你想做的事。

答案 1 :(得分:1)

您应该使用typedef union _LARGE_INTEGER { struct { DWORD LowPart; LONG HighPart; } DUMMYSTRUCTNAME; struct { DWORD LowPart; LONG HighPart; } u; #endif //MIDL_PASS LONGLONG QuadPart; } LARGE_INTEGER; 函数比较字符串,并且应该使用dev_appserver.py代替pip

$(function() {
  $('select').change(function() {
    $('select option.default-option').remove();
  });
});