简体   繁体   中英

Program is going into infinite loop C++

Below is the Program in which I have overloaded ">>" operator

#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;
}

Here while loop in function find_marks() goes into infinite loop if entry_no does not match. Can anyone explain why it is happening ?

Testing for eof() is only really useful to determine whether you want to print an error because the previous conversion failed or not. It is a really bad loop condition:

  1. It isn't necessarily ever reached. For example, if the conversion fails at some point, the stream turns into fail-state where it refuses to extract further characters until the state is cleared.
  2. The std::ios_base::eofbit flag isn't set when EOF is reached (at least, it isn't guaranteed to be set) but it is only guaranteed to be set once it was attempted to read past the end of the file. Thus, the last set of data tends to be processed twice.

The proper way is to just use the conversion to bool after the data was read:

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

If your C++ tutorial advised you to use eof() you should probably burn it and advise others not to buy a copy (they would need to burn). If your teacher told you to use eof() - ... well, it is my understanding that burning people has fallen out of fashion. You should, at least, tell him that he is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM