1

在 c++ 中,如果我们输入一个包含前导零的整数,它们将是任何错误。

例如:

 int a;
 cin>>a;

我们输入 00 或 01。

或在字符串的帮助下输入是一个更好的主意。

4

3 回答 3

4

整数(或浮点数)没有前导零。如果要保留前导零,则必须将输入作为字符串读取,并在需要时将其转换为数字。或者,您可以在打印结果时使用格式添加前导零。

于 2012-06-27T05:52:20.083 回答
3

在 c++ 中,如果我们输入一个包含前导零的整数,它们将是任何错误。

根据输入流格式标志的设置,您可能无法获得预期结果。默认值是期望用户输入始终为十进制。前导零无效。如果我们通过调用来关闭它std::cin.unsetf()呢?

int main () {
  int i;
  std::cin.unsetf (std::ios::dec);
  while (std::cin >> ii) {
    std::cout << i << "\n";
  }
}

The output will be 25 if you enter 25, but if you enter 025 the output is 21. That's because C++ now interprets a leading zero on input to mean the number that follows is in octal (or in hexadecimal in the case of a leading 0x or leading 0X).

于 2012-06-27T07:05:02.533 回答
0

前导零将被修剪掉。它不会存储在内存中。

于 2012-06-27T06:36:14.613 回答