c++ 类与对象 编程题 主函数搞不定了

「已注销」 2020-03-28 06:14:53
题目是这样的:
某天,教务处要统计学生的C++成绩。
假设你是计算机某班的班长,现在已经知道了所有学生的学号,姓名,C++成绩,统计你班学生的人数与你班C++的平均成绩。请定义一个名为Student的类,其中,用成员数据myID来记录学生id,myName来记录学生姓名,myScore来记录学生成绩,并用成员数据classSize记录班级人数和averageScore记录平均成绩,编写成员函数getNum()来统计班级人数,getAverage()来计算平均成绩。

Input
输入包含多行数据。对于每一行,包含三个内容:学生学号(8位数)、姓名(不超过20个字符)、成绩(成绩为int型)

Output
输出一行,第一个数为学生人数,第二个数位学生的平均成绩,中间用空格连接(平均成绩四舍五入保留整数)。

Sample Input
20170000 王二麻 99
20170001 王二麻 98
20170002 王二麻 93
20170003 王二麻 94
Sample Output
4 96

系统给的例子是输入了四组数据,但题目没说明输入几组数据,我用以下代码系统不给过,如果主函数中用while(cin>>)这样的,得出的结果是四组平均成绩了。不知道是我的代码不对?还是主函数要写成while(cin>>id>>name>>score)循环那样?要是要写成循环形式我就不会了,求助


以下是我的代码

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;
class Student
{
private:
int myID;
string myName;
int myScore;
static int classSize;
static int totalScore;
static int averageScore;
public:
Student(int id, string name, int score)
{
myID = id;
myName = name;
myScore = score;


classSize++;
totalScore += score;
averageScore = totalScore / classSize;
}
static int getNum()
{
return classSize;
}
static int getAverage()
{
return averageScore;
}
};
int Student::classSize = 0;
int Student::totalScore = 0;
int Student::averageScore = 0;

int main()
{
int id, score;
string name;
cin >> id >> name >> score;
Student stu1(id, name, score);

cin >> id >> name >> score;
Student stu2(id, name, score);

cin >> id >> name >> score;
Student stu3(id, name, score);

cin >> id >> name >> score;
Student stu4(id, name, score);

cout << Student::getNum() << " " << Student::getAverage() << endl;

return 0;
}
...全文
331 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
谦谦自牧 2020-04-07
  • 打赏
  • 举报
回复
在二楼的基础上改成averageScore = totalScore*1.0/ classSize+0.5即可
looprack 2020-04-01
  • 打赏
  • 举报
回复
就是题目没说要怎么结束输入
「已注销」 2020-03-28
  • 打赏
  • 举报
回复
还是不行啊,好绝望
Italink 2020-03-28
  • 打赏
  • 举报
回复
题目没给你说清楚终止条件 一般这些测试平台的测试数据,会在输入结束后给一个EOF信号,自己测试的话,按一下Ctrl+Z添加EOF信号,按回车就结束了

#include <iostream>
#include <cmath>
#include <iomanip>
#include <vector>
using namespace std;
class Student
{
private:
	int myID;
	string myName;
	int myScore;
	static int classSize;
	static int totalScore;
	static int averageScore;
public:
	Student(int id, string name, int score)
	{
		myID = id;
		myName = name;
		myScore = score;


		classSize++;
		totalScore += score;
		averageScore = totalScore / classSize;
	}
	static int getNum()
	{
		return classSize;
	}
	static int getAverage()
	{
		return averageScore;
	}
};
int Student::classSize = 0;
int Student::totalScore = 0;
int Student::averageScore = 0;

int main()
{
	int id, score;
	string name;
	vector<Student> students;
	while (cin >> id >> name >> score) 
		students.push_back({ id,name,score });
	cout << Student::getNum() << " " << Student::getAverage() << endl;
	return 0;
}

65,201

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧