【发布时间】:2018-05-13 05:43:34
【问题描述】:
我是编程初学者。最近,我写了一个程序,从字符串 date 中提取日、月、年并转换为 int 并分配给数组的元素。但是VS在编译时返回一个我不太明白的错误。请帮我解释一下! 提前致谢! 我的程序
using namespace std;
int a[2];
int j = 0;
string str_date;
string str_date_sub;
void get_date()
{
if (str_date.find('/') == str_date.npos)
{
stringstream ss(str_date);
ss >> a[j];
}
for (int i = 0; i <= str_date.length(); i++)
{
if (str_date[i] == '/')
{
str_date_sub = str_date.substr(0, i - 1);
str_date.erase(0, i + 1);
stringstream ss(str_date_sub);
ss >> a[j];
j++;
break;
}
}
get_date();
}
int main()
{
cout << "Please input the date DD/MM/YYYY:\n";
str_date = "12/05/1234";
get_date();
cout << a[1];
system("pause");
return 0;
}
编辑: 错误在这里
ss >> a[j];
Exception thrown at 0x5B03297A (msvcp140d.dll) in Project6.exe: 0xC0000005: Access violation writing location 0x01116890.
【问题讨论】:
-
错误是什么?
-
对不起,错误在这里 ss >> a[j];
标签: c++