Deleting Records

I am trying to get my program to delete existing student records.

I have discovered that my program WILL NOT delete student P1001 if there are still remaining records. For example, if there are only 2 records left (P1001 and P1002), and I enter P1001 to be deleted, it will instead delete P1002. And when I delete all records, this is what it shows on the statistics (case 1):
Number of records: 0
Average mark: -1.#IND
Standard deviation: -1.#IND

Note: P1001 and P1002 are default student records.

Here is what my coding looks like:

Declarations outside main()
1
2
3
4
5
6
7
const int SIZE=30;
struct StudentRecord
     {
       string studentID;
       float studentMark;
     };
int linearSearch (StudentRecord*student,const string id,int totalStudents);


Declarations inside main()
1
2
3
4
5
6
7
8
9
10
11
    StudentRecord student[SIZE] = 
                  {
                  {"P1001",78.50},
                  {"P1002",66}
                  };
    StudentRecord *recPoint;
    int totalStudents=2;
    float totalMarks=0;
    float averageMark;
    string id;
    int found;


Function
1
2
3
4
5
6
7
8
9
10
11
12
13
                cout << "Enter a Student Record:" << endl;
                cout << "Student ID -> ";
                cin >> id;
                found = linearSearch(student,id,totalStudents);
                if (found!=-1)
                  {
                     recPoint = new StudentRecord;
                     delete recPoint;
                     totalStudents--;
                     cout << endl << "Student Record has been deleted." << endl << endl;
                  }
                else
                  cout << "Record does not exist." << endl << endl;


What could be wrong?
I believe your problem is these 3 lines:
1
2
3
recPoint = new StudentRecord;
delete recPoint;
totalStudents--;


you create a completely new 'StudentRecord' struct in the first line, then in the second you destroy it already... but, in the third line you still decrease your counter, so even though you haven't actually deleted anything useful, your counter still shows there being one less student, and therefore, it only appears like the second record has been deleted when really nothing but the struct that you just created was deleted!
The problem is a bit more extensive than that. You are creating a static array of StudentRecords with 30 elements (and you are only initializing the first two elements, so the other 28 elements will contain garbage). This array is fixed at 30 elements. You can't remove or add elements to this array.

Now, you could simulate adding and removing elements by initializing all elements to zero, and assuming these are 'empty' records (or initializing them to -1, or some other arbitrary value to indicate it's 'empty'), and then the act of deleting elements is simply resetting their values to 'empty' value. There are a number of problems with doing this though, and it's really just the wrong way to implement this kind of functionality.

the right way to handle this is through the use of a dynamic container such as the STL list or vector (both of which have their pros and cons).
http://www.cplusplus.com/reference/stl/list/
http://www.cplusplus.com/reference/stl/vector/
how can I fix the problem?

I have tried fixing the code by replacing it with this:

1
2
3

delete student[found];
totalStudents--;


But without success.
Last edited on
Use an STL container.
what do i use an STL container? (sorry im a real newbie lol)
Topic archived. No new replies allowed.