如何修复'for'循环没有停止

How to fix 'for' loops didn't stop

本文关键字:for 何修复 循环      更新时间:2023-10-16

我正在制作一个名为 scoreint数组,但for循环无法正常工作(我认为循环没有停止(。

我试图删除cin >> score[i],然后变为正常。

array<int, 9> score;
cout << "Score graphics from 1 to tennn";
for(int i = 0; i <= score.size(); i++){
    cout << "The number of people who get " << i + 1 << " : ";
    cin >> score[i];
}

我希望输出The number of people who get 1 : (input)The number of people who get 10 : (input)

this:

for(int i = 0; i <= score.size(); i++){

应该是:

for(int i = 0; i < score.size(); i++){

由于score.size()将返回9,但数组的最后索引是8

&nbsp;

使用您的原始代码,循环的最后运行将在访问带有太大索引的数组时调用一些不确定的行为:

cin >> score[9];  // score array only goes from 0 to 8!!