C++ exercises solutions: delete record

C++ structure exercise: delete record

Step 6: Defining the delete(struct student st[], int& itemcount) method to delete a target record from the array of student objects. The user will be prompted to enter the id of student record that his/her want to delete. Then this id will be checked to make sure it does exist in the list. If the target record or element really exists, the deletion process can be made. The deletion process starts by checking whether the target record is the last record, beginning or middle record. If the target record is the last record in the list, we simply delete the record by supplying it to the clean(struct student st[], int index) method. The last record is the record that has it index equal to itemcount subtracted by 1. If the target record stays at the beginning or in the middle of the list, we need to use a loop to allow the previous element to take over the next element. This process continue until it reaches the end of the list(itemcount-1). Then the clean() method is called to clean the last element of the list that should not exit. After the element is cleaned, the itemcount variable decreases by 1. This means that the number of elements in the list decreases.

void delete_rec(struct student st[], int& itemcount){
  string id;
  int index;
  if (itemcount > 0)
    cout<<"Enter student's ID:";
    index = search(st, id,itemcount);

    if (index!=-1)
       if (index == (itemcount-1)) //delete the last record

          clean(st, index);
          cout<<"The record was deleted.\n";

  else //delete the first or middle record
      for (int i = index; i < itemcount-1; i++)
          st[i] = st[i + 1];
          clean(st, itemcount);
          --itemcount ;


else cout<<"The record doesn't exist. Check the ID and try again.\n";

else cout<<"No record to delete\n";

void clean(struct student st[],int index)
st[index].stnumber ="";
st[index].sex =NULL;
st[index].quizz1 = 0;
st[index].quizz2 = 0;
st[index].assigment = 0;
st[index].midterm = 0;
st[index].final = 0;
st[index].total = 0;



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.