标题:[求助]这个c++问题到底错在哪里了?谢谢
取消只看楼主
lusamy
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-10-25
 问题点数:0 回复次数:1 
[求助]这个c++问题到底错在哪里了?谢谢


谁帮我看下这个程序,很简单,就想求3个学生成绩的总分及平均分,但输入2,结果是正确的,输入3为什么不行啊?

#include <iostream>
using namespace std;

class Student
{
public:
Student(int n,int a,float s): num(n),age(a),score(s){}
float total();
float average();

private:
int num,age,score;
static float sum;
static int count;
};

float Student::total()
{
sum=sum+score;
count++;
return sum;
}

float Student::average()
{
return (sum/count);
}

float Student::sum=0;
int Student::count=0;

int main()
{
Student stu[3]=
{
Student(1001,21,11),
Student(1002,22,12),
Student(1005,28,13),
};

int n;
cout<<"please enter the number of the student: ";
cin>>n;
for(int i=0;i<n;i++)
stu[i].total();
cout<<"the sum of the student's score is: "<<stu[i].total()<<endl;
cout<<"the average score of "<<n<<" students is "<<stu[i].average()<<endl;
return 0;
}

为什么输入2,答案才是正确的,输入3,反而错了。。。

2006-11-18 14:49
lusamy
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-10-25
得分:0 
恩,想出来了,应该是这两句:
cout<<"the sum of the student's score is: "<<stu[i].total()<<endl;
cout<<"the average score of "<<n<<" students is "<<stu[i].average()<<endl;
出现了重复调用对象stu[i],但是应该怎么修改才能正确显示呢?
2006-11-18 15:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-103836-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.111667 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved