title: Student Management System C++
body: <p>I have this program in C++ for Student Management System , Everything is working fine , except one place where i try to delete a student based on his roll number . 
  What it should do : After asking the roll number search the record and delete it
  What is it doing : It deletes all the other records which do not match that roll number</p>
  
  <p>Here is my code :</p>
  
  <pre><code>    #include &lt;iostream&gt;
  
      #include &lt;cstdio&gt;
  
      #include &lt;cstring&gt;
  
      #include &lt;cstdlib&gt;
  
      #include &lt;conio.h&gt;
  
      #include &lt;iomanip&gt;
  
  
  
  using namespace std;
  int main() {
  
      FILE *fp, *ft;
  
      char another, choice;
  
  
  
  struct student {
  
          char first_name[50], last_name[50];
          int roll_num; //new code added
          char course[100];
          int section;
      };
  
      struct student e;
      char xfirst_name[50], xlast_name[50];
      int xroll_num ; // new code added
      long int recsize;
  
      fp=fopen("users.txt","rb+");
  
      if (fp == NULL) {
          fp = fopen("users.txt","wb+");
  
          if (fp==NULL)
          {
               puts("Cannot open file");
               return 0;
          }
      }
  
  
   recsize = sizeof(e);
  
   while(1) {
       system("cls");
  
       cout &lt;&lt; "\t\t====== STUDENT INFORMATION SYSTEM ======";
       cout &lt;&lt;"\n\n                                          ";
       cout &lt;&lt; "\n\n";
       cout&lt;&lt;" \n\t\t\t======================";
       cout &lt;&lt; "\n \t\t\t  1. Add    Records";
       cout &lt;&lt; "\n \t\t\t  2. List   Records";
       cout &lt;&lt; "\n \t\t\t  3. Modify Records";
       cout &lt;&lt; "\n \t\t\t  4. Delete Records";
       cout &lt;&lt; "\n \t\t\t  5. Exit   Program";
       cout&lt;&lt;" \n\t\t\t======================";
       cout &lt;&lt; "\n\n";
       cout &lt;&lt; "\t\t\t Select Your Choice ::";
       fflush(stdin);
       choice = _getche();
       switch(choice)
       {
        case '1' :
              fseek(fp,0,SEEK_END);
              another ='Y';
              while(another == 'Y' || another == 'y')
              {
                    system("cls");
                  cout &lt;&lt; "Enter the First Name : ";
                  cin &gt;&gt; e.first_name;
                  cout &lt;&lt; "Enter the Last Name : ";
                  cin &gt;&gt; e.last_name;
                  cout &lt;&lt; "Enter the Course    : ";
                  cin &gt;&gt; e.course;
                  cout &lt;&lt; "Enter the Section   : ";
                  cin &gt;&gt; e.section;
                  cout &lt;&lt; "Enter the roll number :";
                  cin &gt;&gt; e.roll_num;
                  fwrite(&amp;e,recsize,1,fp);
                  cout &lt;&lt; "\n Add Another Record (Y/N) ";
                  fflush(stdin);
                  another = getchar();
              }
              break;
        case '2':
              system("cls");
             rewind(fp);
             cout &lt;&lt; "=== View the Records in the Database ===";
             cout &lt;&lt; "\n";
             while (fread(&amp;e,recsize,1,fp) == 1){
             cout &lt;&lt; "\n";
             cout &lt;&lt;"\nName     :: " &lt;&lt;e.first_name &lt;&lt;" "&lt;&lt;e.last_name;
             //cout &lt;&lt; "\n";
             cout &lt;&lt;"\nRoll Number :: " &lt;&lt; e.roll_num ;
             cout &lt;&lt;"\nCourse   :: " &lt;&lt;e.course ;
             cout &lt;&lt;"\nSection  :: "&lt;&lt;e.section;
             }
             cout &lt;&lt; "\n\n";
             system("pause");
             break;
  
         case '3' :
              system("cls");
            another = 'Y';
            while (another == 'Y'|| another == 'y')
            {
           //     cout &lt;&lt; "\n Enter the last name of the student : ";
                cout &lt;&lt; "\n Enter the Roll number of the student : ";
                cin &gt;&gt; xroll_num;
  
              rewind(fp);
              while (fread(&amp;e,recsize,1,fp) == 1)
              {
                  //if (strcmp(e.last_name,xlast_name) == 0)
                  if(e.roll_num == xroll_num )
                  {
                  cout &lt;&lt; "Enter the new Firt Name : ";
                  cin &gt;&gt; e.first_name;
                  cout &lt;&lt; "Enter the new Last Name : ";
                  cin &gt;&gt; e.last_name;
                  cout &lt;&lt; "Enter the new Roll Number : ";
                  cin &gt;&gt; e.roll_num;
                  cout &lt;&lt; "Enter the new Course    : ";
                  cin &gt;&gt; e.course;
                  cout &lt;&lt; "Enter the new Section   : ";
                  cin &gt;&gt; e.section;
                  fseek(fp, - recsize, SEEK_CUR);
                  fwrite(&amp;e,recsize,1,fp);
                  break;
                  }
                  else
                  cout&lt;&lt;"record not found";
              }
              cout &lt;&lt; "\n Modify Another Record (Y/N) ";
                  fflush(stdin);
                  another = getchar();
              }
              break;
  
  
           case '4':
         system("cls");
             another = 'Y';
            while (another == 'Y'|| another == 'y')
            {
             //   cout &lt;&lt; "\n Enter the last name of the student to delete : ";
                cout &lt;&lt;"\n Enter the roll number of the student to delete : ";
                cin &gt;&gt; xroll_num;
  
                ft = fopen("temp.dat", "wb");
  
                rewind(fp);
                while (fread (&amp;e, recsize,1,fp) == 1)
  
                  // if (strcmp(e.last_name,xlast_name) != 0)
                      if(e.roll_num == xroll_num )
                  {
                      fwrite(&amp;e,recsize,1,ft);
                  }
                  fclose(fp);
                  fclose(ft);
                  remove("users.txt");
                  rename("temp.dat","users.txt");
  
                  fp=fopen("users.txt","rb+");
  
                  cout &lt;&lt; "\n Delete Another Record (Y/N) ";
                  fflush(stdin);
                  another = getchar();
                }
  
                break;
  
                case '5':
                fclose(fp);
                cout &lt;&lt; "\n\n";
                cout &lt;&lt; "\t\t     THANK YOU FOR USING THIS SOFTWARE";
                cout &lt;&lt; "\n\n";
                exit(0);
            }
            }
       system("pause");
  return 0;
  }
  </code></pre>