accelerated c++ 第四章 程序疑问?程序意图:利用流输入多个学生的姓名和成绩,并调用函数计算最终成绩,最后
accelerated c++ 第四章 程序疑问?
程序意图:利用流输入多个学生的姓名和成绩,并调用函数计算最终成绩,最后输出姓名和最终成绩。
出现问题:输入第一个学生姓名和成绩时正常,但输入第二个学生成绩和姓名时程序直接跳出。
- C/C++ code
#include<iostream>#include<vector>#include<string>using namespace std;struct Student_info //学生的成绩 { string name; double midterm,final; vector<double> homework;};istream& read_hw(istream& in,vector<double>& hw){ if (in) { hw.clear(); double x; while (in>>x) { hw.push_back(x); } in.clear(); } return in;}istream& read(istream& is,Student_info& s) { //cout<<"输入姓名,期中,期末:"; is>>s.name>>s.midterm>>s.final; //cout<<"输入家庭作业成绩:"; read_hw(is,s.homework); //读取家庭作业 return is;}int main(){ vector<Student_info> students; Student_info record; string::size_type maxlen=0; while (read(cin,record)) { maxlen=max(maxlen,record.name.size()); students.push_back(record); } //后面输出省略 return 0;}[解决办法]
ctrl+z,我也在想你是怎么结束的。。。
[解决办法]
晕。。给击倒了。
istream& read_hw(istream& in,vector<double>& hw)
{
if (in)
{
hw.clear();
double x;
while (in>>x)
{
hw.push_back(x);
}
in.clear();
in.sync();
}
return in;
}
