这是我使用的代码。我通常会收到一个名为“Run-Time Check Failure #2 - Stack around the variable 'string' was corrupted.”的错误。我不知道如何恢复数据。有什么我可以做的吗?
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
int result;
char string[5];
cout << "Enter a number in 5 digits (type 0's if less than 5 digits): ";
cin >> string;
result = atol(string);
cout << result << "\n";
system("pause");
return 0;
}
最佳答案
string
有 5 个字符的空间。
\0 终止符需要额外的空间。
输入 4 位数字或将字符串更改为总共 6 个字符的长度。
(或者因为您使用的是 C++,请找到另一种不必依赖固定长度字符缓冲区的方法)
关于atol 操作的 C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26332533/