错误 14 错误 C2041:对于基数‘8’,数字‘8’是非法的

huangapple go评论84阅读模式
英文:

Error 14 error C2041: illegal digit '8' for base '8'

问题

编译时,switch 语句在 case 8 和 9 处发生错误。srand(time(0)); 生成随机数,int random = (rand() %10) + 1; 获取一个随机数。LogDebug.Log(atmID, 2, iThreadNo, "sendTransacMsgToRDV", "为 BVS 生成的随机数为 [%d]",random)。根据随机数执行不同的操作。如果随机数为 1,则记录 "Right Hand Thumb",并将随机数设置为 01。如果随机数为 2,则记录 "Right Hand Index Finger",并将随机数设置为 02。以此类推,直到随机数为 10。如果随机数不在 1 到 10 的范围内,则记录 "未成功生成随机数"。在编译代码时遇到此错误。

英文:

while compiling this the switch statement got error in case 8 and 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</sub>

facing this error while compiling the code

答案1

得分: 2

从数字0开头的整数字面量是八进制整数字面量,基数为8。

从C++语法

八进制字面量:
    0
    八进制字面量 'opt 八进制数字

八进制数字:其中之一
    0 1 2 3 4 5 6 7

所以这些记录

随机数 = 08;

随机数 = 09;

是无效的。如您所见,对于基数为8,有效的数字是0 -7

只需写

随机数 = 8;

随机数 = 9;

使用十进制整数字面量89

为了避免让代码的读者感到困惑,请在整数字面量中删除所有前导零可能会是个好主意。

此外,引入一个具有范围或无范围的枚举类型来表示常量,并在情况标签和赋值语句中使用它们也是一个不错的主意。

英文:

Integer literals starting from the digit 0 are octal integer literals in base 8.

From the C++ grammar

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

and

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

So these records

random = 08;

and

random = 09;

is invalid. As you see for the base 8 valid digits are 0 -7 inclusive.

Just write

random = 8;

and

random = 9;

using decimal integer literals 8 and 9.

And to avoid confusing readers of the code remove everywhere leading zeroes in integer literals.

Also it would be a good idea to introduce a scoped or unscoped enumeration for the constants and use them in case labels and in assignment statements.

huangapple
  • 本文由 发表于 2023年6月27日 21:24:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定