I've been trying to get my delete function to work but it doesn't. Can someone help me with this. It deletes the first record instead of the record I search for.
here are the search and delete functions
1 2 3 4 5 6 7
int Search(Student List[], int Size, string Target)
{
for(int i = 0 ; i < Size ; i++)
if (Target == List[i].LastName)
return i;
return -1;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void Delete_Record(Student List[], int&Size)
{
Student Temp;
int Index;
string Target;
cout << "Enter student Last Name: ";
cin >> Temp.LastName;
Index = Search(List,Size,Target);
for (int i = Index; i < Size;i++)
{
List[i] = List[i+1];
}
Size--;
cout << "The record was deleted.\n";
}
Well you have the Target string on line 5. For some reason though you have line 3 which you don't need and on line 7 you are reading into the "Temp" student instead of the the "Target" lastname. So when you call the search function on line 8 you are searching for someone who does not exist so that sets i == to -1 on line 9. You may want to put an if statement in there because -1 is less than the size so the for loop will start from -1 and go to size -1.
Basically this:
Remove line 3 and change line 7 to cin >> Target;
*edit then you may want an if statement around that for loop forgot to mention