Im working on a program that inputs book data in a struct, and deletes an element upon the user's request. However, Im having difficulty figuring out the best way to do delete the requested book. Do you think Im headed in the right direction?
struct Data{ //struct of data
string title;
string author;
string publisher;
};
void remove (Data *ptr, string title, string author, string publisher, int num)
{
string book_rem;
cout << "What book do you want to remove?" << endl;
getline (cin, book_rem);
for (int i=0;i<num;i++)
{
if (ptr[i].title == book_rem) // check for equality
{
for (int j = 0; j < num; j++) //shift over elements in new array
ptr[j] = ptr[j+1];
ptr[j-1] = 0;
}
else
{
cout << "book not found!" << endl;
}
}
}
I know my logic is off ?.. but my tutor said i was close..if you have any good resources or links please send them this way these pointers have me slightly confused on the best way to get it done.
ptr? Each element was allocated dynamic memory usingnew? In that case you need to deallocate the memory by callingdelete.