title: Deleting a record from text file
body: <p>I have got an assignment project to make a program using C++ which maintains a list of Students (their name, age, and GPA) in a text file. The program has the following functionality:</p>
  
  <ul>
  <li>Insertion of record</li>
  <li>Deletion of record</li>
  <li>Searching of record</li>
  <li>Updating a record</li>
  </ul>
  
  <p>When deleting a record my code takes a string name as input and removes it from the text file. However the next two lines in the file (age &amp; gpa for that student) are left. How do I remove those too?</p>
  
  <p>Following is the code for my program.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  void writing();
  void deleting();
  void searching();
  
  class student {
  
  public:
    int age = 0;
    string name;
  
    void SetGpa(float x)
    {
      gpa = x;
    }
    float GetGpa()
    {
      return gpa;
    }
  
  private:
    float gpa = 0.0;
  
  };
  
  
  int main()
  {
    int opt;
    cout &lt;&lt; "Please Enter an option number to continue:\n ";
    cout &lt;&lt; "\nPress 1 for New Record insertion";
    cout &lt;&lt; "\nPress 2 for Record Deletion";
    cout &lt;&lt; "\nPress 3 for Searching a Record";
    cout &lt;&lt; "\nPress 4 for Updating a Record";
    cout &lt;&lt; "\nEnter option Number: ";
    cin &gt;&gt; opt;
  
    switch (opt)
    {
    case 1:
    {
      writing();
      break;
    }
  
    case 2:
    {
      deleting();
      break;
    }
  
    case 3:
    {
      searching();
      break;
    }
    case 4:
    {
      deleting();
      writing();
      cout &lt;&lt; "Record has been updated! ";
      break;
    }
    }
  }
  
  void writing()
  {
    float a;
    student moiz;
    cout &lt;&lt; "Please enter name of student: ";
    cin &gt;&gt; moiz.name;
    cout &lt;&lt; "Please enter the age of student: ";
    cin &gt;&gt; moiz.age;
    cout &lt;&lt; "Pleae enter the Gpa of student: ";
    cin &gt;&gt; a;
    moiz.SetGpa(a);
  
    ofstream myfile;
    myfile.open("record.txt", ios::app | ios::out);
    myfile &lt;&lt; endl &lt;&lt; moiz.name &lt;&lt; endl;
    myfile &lt;&lt; moiz.age &lt;&lt; endl;
    myfile &lt;&lt; moiz.GetGpa();
    myfile.close();
  }
  
  void deleting()
  {
  
    string line, name;
    cout &lt;&lt; "Please Enter the name of record you want to delete: ";
    cin &gt;&gt; name;
    ifstream myfile;
    ofstream temp;
    myfile.open("record.txt");
    temp.open("temp.txt");
    while (getline(myfile, line))
    {
      if (line != name)
        temp &lt;&lt; line &lt;&lt; endl;
    }
    cout &lt;&lt; "The record with the name " &lt;&lt; name &lt;&lt; " has been deleted if it exsisted" &lt;&lt; endl;
    myfile.close();
    temp.close();
    remove("record.txt");
    rename("temp.txt", "record.txt");
  }
  
  void searching()
  {
    ifstream fileInput;
    fileInput.open("record.txt");
    string line, search;
    cout &lt;&lt; "Please enter the term to search: ";
    cin &gt;&gt; search;
    for (unsigned int curLine = 0; getline(fileInput, line); curLine++)
    {
      if (line.find(search) != string::npos)
      {
        cout &lt;&lt; "found: " &lt;&lt; search &lt;&lt; " on line: " &lt;&lt; curLine &lt;&lt; endl;
      }
      else
      {
        cout &lt;&lt; "Error! Not found on Line" &lt;&lt; curLine &lt;&lt; endl;
      }
    }
  }
  </code></pre>