我的代码有问题,我想知道是否有人可以看看,我创建了一个函数来从数组中删除特定元素。我使用线性搜索来查找元素,然后用后面的元素覆盖我想要删除的元素,因为我还没有找到专门删除元素的方法。我的问题是代码并没有真正起作用,因为元素没有被覆盖,还有一种方法可以在元素被覆盖后在数组中留下一个空格。
下面是我的代码:
void deleteinfo()
{
string search ;
int found ;
cout << "\n Delete A Player's Information \n\n" ;
cout << "Please Enter The Player's Last Name : " ;
cin >> search ;
found=linsearch(search);
if (found==-1)
{
cout << "\n There is no player called " << search ;
}
else
{
player[found].getFirstName() = player[found + 1].getFirstName() ;
player[found].getLastName() = player[found + 1].getLastName() ;
player[found].getAge() == player[found + 1].getAge() ;
player[found].getCurrentTeam() = player[found + 1].getCurrentTeam() ;
player[found].getPosition() = player[found + 1].getPosition() ;
player[found].getStatus() = player[found + 1 ].getStatus() ;
cout << "\n Player has been deleted." ;
}
cin.get() ;
menu() ;
}
int linsearch(string val)
{
for (int j=0; j <= 3; j++)
{
if (player[j].getLastName()==val)
return j ;
}
return -1 ;
}