【问题标题】:Program is going into infinite loop C++程序进入无限循环 C++
【发布时间】:2012-11-04 18:13:02
【问题描述】:

下面是我重载了“>>”运算符的程序

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class Student{
    public :
        string name;
        string entry_no;
};

class Science : public Student{
    public :
    float marks;
    void create_file();
    void highest();

    friend istream& operator >> (istream& input, Science& stud);
};

istream& operator >> ( istream& input, Science& stud){
    input >> stud.name;
    input >> stud.entry_no;
    input >> stud.marks;
    return input;
}
void Science::create_file(){
    ifstream file_read;

    file_read.open("student.txt");

    ofstream file_write;
    file_write.open("science.txt");
    string line;

    while(!file_read.eof()){
        getline(file_read,line,'\n');

        if(line.find("Science") != string::npos){
            file_write << line;
            file_write << '\n';
        }
    }
}

class Art : public Student{
    public :
    string marks;
    void create_file();
    void highest();
    friend istream& operator >> (istream& input, Art& stud);
};

istream& operator >> ( istream& input, Art& stud){
    input >> stud.name;
    input >> stud.entry_no;
    input >> stud.marks;
    return input;
}

void Art::create_file(){
    ifstream file_read;

    file_read.open("student.txt");

    ofstream file_write;
    file_write.open("art.txt");
    string line;

    while(!file_read.eof()){
        getline(file_read,line,'\n');

        if(line.find("Art") != string::npos){
            file_write << line;
            file_write << '\n';
        }
    }
    file_read.close();
    file_write.close();
}

void find_marks(){

    string entry_no;
    cout << "Enter entry_no of the student to find marks " << endl;
    cin >> entry_no;

    ifstream file_read;
    file_read.open("science.txt");
    string stud_entry;
    Science stud;
    bool found = false;
    if(file_read.is_open()){
        cout << (file_read >> stud) << endl;
    while( file_read >> stud ){
        cout << "hi";
        if(!entry_no.compare(stud.entry_no)){
            cout << stud.marks << endl;
            found = true;
            break;
        }
    }
    }
    else
        cout << "error in openning"<< endl;

    if(!found)
        cout << "this student does not exist" << endl;
}

int main(){
    Science science_stud;
    Art art_stud;

    science_stud.create_file();
    art_stud.create_file();
    find_marks();   
    return 0;
}

如果 entry_no 不匹配,函数 find_marks() 中的 while 循环将进入无限循环。谁能解释为什么会这样?

【问题讨论】:

  • 你的while循环应该是while (file_read &gt;&gt; stud)
  • 我怀疑您在读取文件时遇到错误,但不是 eof。你能举一个 science.txt 文件失败的小例子吗?

标签: c++ operator-overloading file-handling


【解决方案1】:

测试eof() 仅在确定您是否要打印错误时非常有用,因为之前的转换失败了。这是一个非常糟糕的循环条件:

  1. 不一定能达到。例如,如果转换在某个时间点失败,则流将进入失败状态,拒绝提取更多字符,直到状态被清除。
  2. 到达 EOF 时不会设置std::ios_base::eofbit 标志(至少不保证会设置),但只有在尝试读取文件末尾之后才能保证设置。因此,最后一组数据往往会被处理两次。

正确的方法是在读取数据后使用到bool的转换:

while (file >> whatever) {
    ...
}

如果您的 C++ 教程建议您使用 eof(),您可能应该刻录它并建议其他人不要购买副本(他们需要刻录)。如果您的老师告诉您使用eof() - ...好吧,我的理解是燃烧人已经过时了。你至少应该告诉他他错了。

【讨论】:

  • 我使用了上述条件,但在这种情况下它永远不会进入循环
  • @neel 你检查文件是否打开成功了吗?
  • 如果我使用 eof 那么它会进入循环
  • @neel:这意味着您没有提供与您尝试读取的数据集相匹配的数据集!你没有提供任何关于你的班级成员是什么类型的指示,但我敢猜测namestd::stringentry_no 是一个整数。 ...而且,此外,我准备打赌输入以这样的内容开头:“John Smith 17”。在这种情况下,请注意读取name 只是读取"John" 并尝试将"Smith" 读取为整数,显然失败了。当然,连一个输入都读不出来,说明使用eof()是错误的……
  • 我已将 name、entry_no 转换为字符串,但仍无法正常工作
猜你喜欢
  • 1970-01-01
  • 2016-11-02
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多