c++ 错误14错误C2041:基数“8”的数字“8”非法

vx6bjr1n  于 2023-07-01  发布在  其他
关注(0)|答案(1)|浏览(121)

在编译此语句时,switch语句在case 8和case 9中出错

srand(time(0));
        int random = (rand() %10) + 1;
        LogDebug.Log (atmID, 2, iThreadNo, "sendTransacMsgToRDV", "random number for BVS [%d]",random);
        switch (random)
        {
            case 1:
                LogDebug.Log (atmID, 2, iThreadNo, "sendTransacMsgToRDV", "Right Hand Thumb");
                random = 01;
                break;
            
            case 2:
                LogDebug.Log (atmID, 2, iThreadNo, "sendTransacMsgToRDV", "Right Hand Index Finger");
                random = 02;
                break;

            case 3:
                LogDebug.Log (atmID, 2, iThreadNo, "sendTransacMsgToRDV", "Right Hand Middle Finger");
                random = 03;
                break;
            
            case 4:
                LogDebug.Log (atmID, 2, iThreadNo, "sendTransacMsgToRDV", "Right Ring Finger");
                random = 04;
                break;
            
            case 5:
                LogDebug.Log (atmID, 2, iThreadNo, "sendTransacMsgToRDV", "Right Hand Baby Finger");
                random = 05;
                break;
            
            case 6:
                LogDebug.Log (atmID, 2, iThreadNo, "sendTransacMsgToRDV", "Left Hand Thumb");
                random = 06;
                break;
            
            case 7:
                LogDebug.Log (atmID, 2, iThreadNo, "sendTransacMsgToRDV", "Left Hand Index Finger");
                random = 07;
                break;

            case 8:
                LogDebug.Log (atmID, 2, iThreadNo, "sendTransacMsgToRDV", "Left Hand Middle Finger");
                random = 08;
                break;
            
            case 9:
                LogDebug.Log (atmID, 2, iThreadNo, "sendTransacMsgToRDV", "Left Ring Finger");
                random = 09;
                break;
            
            case 10:
                LogDebug.Log (atmID, 2, iThreadNo, "sendTransacMsgToRDV", "Left Hand Baby Finger");
                random = 10;
                break;

            default:
                LogDebug.Log (atmID, 2, iThreadNo, "sendTransacMsgToRDV", "Random Number not generated successfully");
                break;

        }

Screenshot
编译代码时遇到此错误

tvz2xvvm

tvz2xvvm1#

从数字0开始的整数文字是以8为基数的八进制整数文字。
从C++语法

octal-literal:
    0
    octal-literal ’opt octal-digit

octal-digit: one of
    0 1 2 3 4 5 6 7

所以这些记录

random = 08;

random = 09;

无效。如您所见,对于基数8,有效数字包括0 -7
写就行了

random = 8;

random = 9;

使用十进制整数文字89
为了避免代码的读者感到困惑,请删除整数文字中的所有前导零。
另外,为常量引入一个有作用域或无作用域的枚举,并在case标签和赋值语句中使用它们也是一个好主意。

相关问题