2012-11-04 4 views
1

아래는 프로그램하는 내가 오버로드 ">>"여기 운영자프로그램은

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

입니다 무한 루프 C++로 것입니다. 왜 그런 일이 일어 났는지 설명 할 수 있습니까?

+5

while 루프는 'while (file_read >> stud)'이어야합니다. – chris

+0

파일을 읽는 중 오류가 발생했지만 eof가 아닌 것으로 판단됩니다. 실패한 science.txt 파일의 작은 예를 들어 줄 수 있습니까? –

답변

7

eof()에 대한 테스트는 이전 변환이 실패했기 때문에 오류를 인쇄할지 여부를 결정하는 데 정말로 유용합니다. 정말 나쁜 루프 상태입니다.

  1. 반드시 도달하지는 않습니다. 예를 들어 어떤 시점에서 변환이 실패하면 스트림은 상태가 지워질 때까지 추가 문자 추출을 거부하는 실패 상태가됩니다.
  2. EOF에 도달하면 std::ios_base::eofbit 플래그가 설정되지 않습니다 (적어도 설정되지는 않음). 그러나 파일 끝을지나 읽으려고 시도한 후에 만 ​​설정됩니다. 따라서 마지막 데이터 세트는 두 번 처리되는 경향이 있습니다.

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

    당신의 C++의 튜토리얼 eof()를 사용하도록 조언하는 경우 당신은 아마 그것을 점화하고 조언해야 다른 사람을하지 :

적절한 방법은 후 bool에 데이터를 읽을 된 변환을 사용하는 것입니다 사본을 사기 위해 (그들은 화상을 입을 필요가있다). 선생님이 eof()을 사용하라고 말하면 ... 불타는 사람들이 유행에 빠졌다는 것은 내 이해입니다. 적어도 잘못했다고 말해야합니다.

+0

그냥 대단한 사람인 것 같습니다. –

+0

위의 조건을 사용했지만이 경우 루프에 들어 가지 않습니다. – neel

+0

@neel 파일을 성공적으로 열 었는지 확인 했습니까? – moooeeeep