我正在学习C ++。我的代码中的某些东西不起作用,但我不明白这是什么问题。我认为stat数组的增量计算T数组中重复的数量...我该如何解决?也许语法stat [elt] ++不正确?
#include <iostream>
#include <cctype>
#include <ctime>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
int anneeSecu(int T[12][13], int j){
// here I join the 2 numbers of tab (they refer to the year of birth) and I add the number 20, because I know that they were born all after 2000
int a = T[j][1];
int b = T[j][2];
ostringstream oss;
oss << "20" << a << b;
istringstream iss(oss.str());
int ab;
iss >> ab;
int annee = ab;
cout << annee << endl;
return annee;
}
int main(){
int T[12][13]={ {1,1,2,0,2,3,7,1,9,8,2,0,5},
{2,1,3,0,5,8,9,9,0,8,1,0,2},
{2,1,6,0,8,1,9,2,6,5,4,8,7},
{2,1,5,0,6,5,6,3,4,6,0,6,7},
{1,1,5,0,6,5,6,3,4,6,0,7,5},
{2,1,5,0,2,5,6,3,4,6,0,6,7},
{2,1,5,0,6,2,9,1,4,7,1,6,3},
{2,1,4,0,6,2,2,1,4,7,1,6,3},
{2,1,4,0,9,3,5,1,4,7,1,6,3},
{1,1,1,1,2,5,6,1,7,5,0,0,7},
{1,1,1,0,9,5,6,1,7,5,1,0,7},
{2,1,4,1,0,3,5,1,4,7,0,2,2} };
int stat[6]={0,0,0,0,0,0};
for (int i=0;i<12;i++){
int l=anneeSecu(T,i);
**stat[l]++;**
}
return 0;
}
输出和冻结cmd
2012
Process returned -1073741819 (0xC0000005) execution time : 1.764 s
Press any key to continue.
解 错误在stat [l] ++中(因为它只需要从0到5的数字......) 校正=&gt; STAT [1- 2011] ++
答案 0 :(得分:1)
这段代码存在问题:
int l=anneeSecu(T,i);
stat[l]++;
因为anneeSecu
接缝返回的数字比数组的长度更大。 l
必须介于0到5之间,否则您的程序将崩溃。