[新手]这段代码为什么不能算总分?
#include "iostream.h "
#include "stdlib.h "
struct stud
{
char name[8];
int xuehao,cj[5],aver,total;
};
void zongfen(stud a[],int n) //算总分
{
int k;
for(k=0;k <5;k++)
a[n].total +=a[n].cj[k];
}
void print(stud a[]) //输出成绩
{
int i;
for(i=0;i <5;i++)
{
cout < < "总分 " < <a[i].total < < " ";
cout < < '\n ';
}
}
void main()
{
stud stu[5];
int i,j;
for(i=0;i <5;i++) //用随机数对cj数组赋值
for(j=0;j <5;j++)
stu[i].cj[j]=rand()%61+40;
for(i=0;i <5;i++) // 验证随机数
{
for(j=0;j <5;j++)
cout < <stu[i].cj[j] < < " ";
cout < <endl;
}
for(i=0;i <5;i++)
zongfen(stu,i);
print(stu);
}
[解决办法]
你的total没有设置初始值为0 中间加stu[i].total=0 就能算总和了