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.
Well in the
forloop, you are marking that the book wasn’t found immediately after the firstif (ptr[i].title == book_rem). What if the book is present as the 2nd element or thereafter.You should divide your work into two parts as follows:
He is what it may look like inside the loop:
And then outside the loop, check if the book was found, and do the required stuff: