Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:07:29+00:00 2026-05-30T15:07:29+00:00

Im working on a program that inputs book data in a struct, and deletes

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:07:30+00:00Added an answer on May 30, 2026 at 3:07 pm

    Well in the for loop, you are marking that the book wasn’t found immediately after the first if (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:

    • Search for the book.
    • Delete it if found, otherwise print “Not Found”.

    He is what it may look like inside the loop:

    for (int i = 0 ; i < num ; i++) {
        if (ptr[i].title == book_rem) // check for equality
        {
            break;
        }  
    }
    

    And then outside the loop, check if the book was found, and do the required stuff:

    if ( i < num ) {// Book was found as i is less than size.
        /*
            You don't have to shift over elements in the array.
            I'm assuming your list isn't sorted, so you can just transfer 
            the last element in the array to the position pointed by i, and 
            then decrement the size by 1. 
        */
        // Copy all the elements from ptr[num-1] to ptr[1].
        num = num - 1;
    }
    else {
        cout << "book not found!" << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Serpinski triangle program that asks the user for the levels
I'm working on a simple program that collects and checks user-input. In addition to
I am working on a small program that requires the user to insert a
I am working on a program that fills an array with data from a
I'm working on a program where the user inputs 3 values (one of which
I'm working on a program that takes in book records in the form <book
I'm working on a program that asks for input, then calculates and posts the
I'm working on a program that reads in users input as a string, which
I'm writing a wrapper program that loads Winamp input plugins. I've got it working
I am working on a program that needs to create a multiple temporary folders

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.