| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1234 人关注过本帖
标题:为什么读出来的输出结果多一个?求教
只看楼主 加入收藏
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
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册: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
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册: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
快速回复:为什么读出来的输出结果多一个?求教
数据加载中...
 
   



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

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