为什么当我把k改成i就出乱码了是什么原因,是因为i在for循环中导致无法在外被使用吗,不应该啊,跳出for还是会有i的数据吧


#include <iostream>
#include <string>
using namespace std;
struct student {
    int num;
    string name;
    int english;
    int math;
    int physics;
    int ave;
};
void shuru(student s1[5])
{
    for (int i = 0; i < 5; i++)
    {
        cin >> s1[i].num >> s1[i].name >> s1[i].english >> s1[i].math >> s1[i].physics;
        s1[i].ave = ((s1[i].english + s1[i].math + s1[i].physics) / 3);
    }
}
void shuchu(student s2[5])
{
    int  max=0, i,k=0;
    for (i = 0; i < 5; i++)
    {
        cout << s2[i].ave << endl;
    }
    for (i = 0; i < 5; i++)
    {
        if (max < s2[i].ave) max = s2[i].ave;
        k = i;
    } //就是下面的k改成i并把k=i这段删除掉,就是用i来输出最大值的数据,为什么会乱码
    cout << "平均分最高的同学的数据:" << s2[k].num <<"  " << s2[k].name<< "  " << s2[k].english <<"  " << s2[k].math<<"  " << s2[k].physics<<"  " << s2[k].ave;         
}
int main()
{
    student [5];
    shuru(s);
    shuchu(s);
    return 0;
}

_______

i是有数据,那你想想跳出循环的时候i=多少?
那么你的数组长度又是多少?数组下标从多少开始的?然后想想是不是越界了?
还有,既然你要求最高分,你想想31行这里的k=i;没有在if里面是正确的吗?