How Do I Update a Record?

I am trying to get my program to do the following:

If the student ID for the mark to enter has already got a record in the program, then the newly entered mark will replace the previous mark corresponding to the student ID. This is essentially an update of the student mark.

At the moment, I have successfully allowed the program to create the student record, but I have no idea as to updating it.

Here is what my code looks like:

Declarations outside main():
1
2
3
4
5
6
7
8
9
10
const int SIZE=30;
struct StudentRecord
     {
       string studentID;
       float studentMark;
     };

void populate(StudentRecord *,string);
void dispOne(StudentRecord *);
int linearSearch (StudentRecord*student,const string id,int totalStudents);


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


Function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
                cout << "Enter a Student Record: " <<endl;
                cout << "Student ID -> ";
                cin >> id;
                found = linearSearch(student,id,totalStudents);
                if (found!=-1)
                  {
                    cout << "Record already exists." << endl << endl;
                  }
                else
                  {
                    recPoint = new StudentRecord;
                    populate(recPoint,id);
                    dispOne(recPoint);
                    student[totalStudents].studentID=recPoint->studentID;
                    student[totalStudents].studentMark=recPoint->studentMark;
                    totalStudents++;
                  }  


Function prototypes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void populate(StudentRecord *record, string stId)
{
     record->studentID = stId;
     cout << "Enter the corresponding mark: ";
     cin >> record->studentMark;
     return;
}

void dispOne(StudentRecord *contents)
{
     cout << "\nThe contents of the record just created is:"
          << "\nStudent ID: " << contents->studentID
          << "\nMark: " << contents->studentMark << endl << endl;
     return;
}

int linearSearch(StudentRecord*student,const string id,int totalStudents)
{
    int retValue=-1;
    for(int i=0;i<totalStudents;++i)
    {
            if(student[i].studentID == id)
            {
              retValue=i;
              break;
            }
    }
    return retValue;
}


Could somebody help on or show me how to update please.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    found = linearSearch(student,id,totalStudents);
    if (found!=-1)
    {
        cout << "Record already exists." << endl << endl;
    }
    else
    {
        recPoint = new StudentRecord;
        populate(recPoint,id);
        dispOne(recPoint);
        student[totalStudents].studentID=recPoint->studentID;
        student[totalStudents].studentMark=recPoint->studentMark;
        totalStudents++;
    }

Why have you dynamically allocated a temporary StudentRecord when adding a new one? You could declare one in the same place you've allocated one from the heap. It's not released back to the heap, so you leak a StudentRecord object everytime you add one.

Update might look something like:
1
2
3
4
5
    int index = linearSearch(student,i d, totalStudents);
    if (index >= 0)
    {
        student[index]->studentMark = revised_score;
    }


You really should move all this Student stuff into a class and use some kind of data abstraction rather than this procedural style.
Topic archived. No new replies allowed.