标题:为什么读出来的输出结果多一个?求教
只看楼主
starlz
Rank: 2
等 级:论坛游民
帖 子:15
专家分:27
注 册:2016-10-22
结帖率:100%
已结贴  问题点数:20 回复次数:3 
为什么读出来的输出结果多一个?求教
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;
class Students
{
private:
int stuno,classno;
string name;
float cscore, pscore, tscore;
public:
Students():stuno(0),classno(0),cscore(0),pscore(0),tscore(0){}
friend istream &operator>>(istream&, Students&);
friend ostream &operator<<(ostream&, Students&);
float avgscore(){ return (cscore + pscore + tscore) / 3; }


};
istream &operator>>(istream &is, Students &s)
{
is >> s.stuno >> s.name >> s.classno >> s.cscore >> s.pscore >> s.tscore;
return is;
}
ostream &operator<<(ostream &os, Students &s)
{
os << s.stuno << " " << s.name << " " << s.classno << " " << s.cscore << " " << s.pscore << " " << s.tscore << endl;
return os;
}
int main()
{
vector<Students>s;
Students a;
ifstream in;
in.open("Text.txt");

while (in)
{
in >> a;
s.push_back(a);
}
in.close();



ofstream out("Text1.txt");
for (vector<Students>::iterator it = s.begin(); it != s.end(); ++it)
{
out << *it << endl;
}
out.close();
return 0;
}
输入:1 lz 4 99 99 99
2 lx 4 98 98 98
3 wl 4 97 97 97
输出:
1 lz 4 99 99 99
2 lx 4 98 98 98
3 wl 4 97 97 97
3 wl 4 97 97 97
为什么呢?
搜索更多相关主题的帖子: private include public friend return 
2016-11-01 08:15
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
程序代码:
#include <iostream>
#include <string>

class Student
{
    friend std::istream& operator>>( std::istream&, Student& );
    friend std::ostream& operator<<( std::ostream&, const Student& );

public:
    Student() : stuno_(0),classno_(0),cscore_(0),pscore_(0),tscore_(0)
    {
    }

    double avgscore() const
    {
        return (cscore_+pscore_+tscore_)/3;
    }
private:
    int stuno_, classno_;
    std::string name_;
    double cscore_, pscore_, tscore_;
};

using namespace std;

istream& operator>>( istream& is, Student& s )
{
    return is >> s.stuno_ >> s.name_ >> s.classno_ >> s.cscore_ >> s.pscore_ >> s.tscore_;
}
ostream& operator<<( ostream &os, const Student& s )
{
    return os << s.stuno_ << ' ' << s.name_ << ' ' << s.classno_ << ' ' << s.cscore_ << ' ' << s.pscore_ << ' ' << s.tscore_;
}

#include <fstream>
#include <vector>

int main( void )
{
    vector<Student> s;
    {
        ifstream in("Text.txt");
        for( Student a; in>>a; )
            s.push_back( a );
    }
    {
        ofstream out("Text1.txt");
        for( vector<Student>::const_iterator itor=s.begin(); itor!=s.end(); ++itor )
            out << *itor << '\n';
    }
    return 0;
}

主要错在
while (in)
{
    in >> a;
    s.push_back(a); // 上一句未必读成功
}
2016-11-01 08:54
starlz
Rank: 2
等 级:论坛游民
帖 子:15
专家分:27
注 册:2016-10-22
得分:0 
谢谢楼上,我上网查了一下改成这样也可以:)
    vector<Students>s;
    Students a;
    ifstream in;
    in.open("Text.txt");
while (!in.eof())
        {
            in >> a;
            s.push_back(a);
        }
    in.close();
2016-11-01 09:36
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
以下是引用starlz在2016-11-1 09:36:43的发言:

谢谢楼上,我上网查了一下改成这样也可以:)
    vectors;
    Students a;
    ifstream in;
    in.open("Text.txt");
while (!in.eof())
        {
            in >> a;
            s.push_back(a);
        }
    in.close();

不可以
2016-11-01 14:08



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




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

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