首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

[新手]这段代码为什么不能算总分?该如何解决

2012-04-02 
[新手]这段代码为什么不能算总分?#includeiostream.h #includestdlib.h structstud{charname[8]intxu

[新手]这段代码为什么不能算总分?
#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 就能算总和了

热点排行