Range checks using a switch statement

后端 未结 7 1293
陌清茗
陌清茗 2020-12-23 02:15

My teacher has assigned a program to use both if-else statements and switch statements, so we understand how to implement both. The program asked u

7条回答
  •  太阳男子
    2020-12-23 03:10

    You can not use a double inside a switch. The documentation says:

    switch ( expression )
       case constant-expression : statement
       [default   : statement]
    

    The expression must be of an integral type or of a class type for which there is an unambiguous conversion to integral type. Integral promotion is performed as described in Integral Promotions.

    On a side note:

    There are some compilers (like Clang 3.5.1) which are allowing the case x ... y as an extension to the C++ language. But that too is for an integral datatype. Something like

    switch(x){
           case 0:
                cout << "Test1";
                break;
           case 0 ... 9:
                cout << "Test2";
                break;
    

提交回复
热议问题