C++,求解为啥我的求平均值求不出来,老是出现一堆数字,负数
#include<iostream.h>#include<string>classStudent{public:intsum(){returnsum_score=m_sc...
#include<iostream.h>#include<string>class Student{public:int sum() { return sum_score=m_score+e_score+c_score; } int average() { return sum_score/3; } void print() { cout<<"This student`s name is:"<<name[10]<<endl; cout<<"This student`s math score is:"<<m_score<<endl; cout<<"This student`s English score is:"<<e_score<<endl; cout<<"This student`s computer score is:"<<c_score<<endl; cout<<"This student`s sum score is:"<<sum()<<endl; cout<<"This student`s average score is:"<<average()<<endl; } int get_reg_num() { return reg_num; } void set_stu_inf() { cout<<"Plese input student`s register number:"<<endl; cin>>reg_num; cout<<"Plese input student`s name:"<<endl; cin>>name[10]; cout<<"Plese input student`s math score:"<<endl; cin>>m_score; cout<<"Plese input student`s English score:"<<endl; cin>>e_score; cout<<"Plese input student`s computer score:"<<endl; cin>>c_score; } int sum_score; int average_score;protected:int reg_num; char name[10]; int m_score; int e_score; int c_score; };void main(){ int i=0,j=0,t=0,average=0,sum=0,reg=0; Student array[3]; while(i<3) { array[i].get_reg_num(); array[i].set_stu_inf(); i++; } for(i=0,sum=0;i<3;i++) sum=sum+array[i].average(); average=sum/3; cout<<"All student`s average score is:"<<average<<endl; for(i=0;i<2;i++) { j=i+1; if (array[t].sum()<=array[j].sum()) t=j; } cout<<"The highest score of all students is:"<<array[t].sum()<<endl; cout<<"If you want to get someone`s information,please input his/her register number:"<<endl; cin>>reg; for(i=0;reg!=array[i].get_reg_num();i++); array[i].print();}别的结果输出都没问题,就是61行求所有成绩的平均值时,答案为-286331153
展开
若以下回答无法解决问题,邀请你更新回答
展开全部
平均,总和之类的变量 改用 float 型,不要用整型。
average() 要调用 sum(); 才正确。
例如:
float sum() {
sum_score=m_score+e_score+c_score;
return sum_score;}
float average() { sum(); return sum_score/3; } // 这里要调用 sum();
cin>>name; // 不用 name[10]
float sum_score;
float average_score;
主函数也用 float
float average=0,sum=0;
average() 要调用 sum(); 才正确。
例如:
float sum() {
sum_score=m_score+e_score+c_score;
return sum_score;}
float average() { sum(); return sum_score/3; } // 这里要调用 sum();
cin>>name; // 不用 name[10]
float sum_score;
float average_score;
主函数也用 float
float average=0,sum=0;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询