Note that there are some explanatory texts on larger screens.

plurals
  1. POProgram is going into infinite loop C++
    primarykey
    data
    text
    <p>Below is the Program in which I have overloaded ">>" operator </p> <pre><code>#include&lt;iostream&gt; #include&lt;fstream&gt; #include&lt;string&gt; 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&amp; operator &gt;&gt; (istream&amp; input, Science&amp; stud); }; istream&amp; operator &gt;&gt; ( istream&amp; input, Science&amp; stud){ input &gt;&gt; stud.name; input &gt;&gt; stud.entry_no; input &gt;&gt; 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 &lt;&lt; line; file_write &lt;&lt; '\n'; } } } class Art : public Student{ public : string marks; void create_file(); void highest(); friend istream&amp; operator &gt;&gt; (istream&amp; input, Art&amp; stud); }; istream&amp; operator &gt;&gt; ( istream&amp; input, Art&amp; stud){ input &gt;&gt; stud.name; input &gt;&gt; stud.entry_no; input &gt;&gt; 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 &lt;&lt; line; file_write &lt;&lt; '\n'; } } file_read.close(); file_write.close(); } void find_marks(){ string entry_no; cout &lt;&lt; "Enter entry_no of the student to find marks " &lt;&lt; endl; cin &gt;&gt; entry_no; ifstream file_read; file_read.open("science.txt"); string stud_entry; Science stud; bool found = false; if(file_read.is_open()){ cout &lt;&lt; (file_read &gt;&gt; stud) &lt;&lt; endl; while( file_read &gt;&gt; stud ){ cout &lt;&lt; "hi"; if(!entry_no.compare(stud.entry_no)){ cout &lt;&lt; stud.marks &lt;&lt; endl; found = true; break; } } } else cout &lt;&lt; "error in openning"&lt;&lt; endl; if(!found) cout &lt;&lt; "this student does not exist" &lt;&lt; endl; } int main(){ Science science_stud; Art art_stud; science_stud.create_file(); art_stud.create_file(); find_marks(); return 0; } </code></pre> <p>Here while loop in function find_marks() goes into infinite loop if entry_no does not match. Can anyone explain why it is happening ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload