Hello everyone

im currently making a library, and im new to using arrays, i have added some books to my library file using an array. i was wondering how would i go back and select the element in the file to delete. the area that im trying to code for delete is in red. can anyone please help

bool addbook(char books[MAX_BOOKS][5][SIZE], int cnt)
{     

		
     cin.ignore(80,'\n'); 
					               
     
     system("CLS");                
     cout<<"***************************\n";
     cout<<"           Add Book      \n";
     cout<<"***************************\n";
     
     cout<<"Title: ";
     cin.getline(books[cnt][0],SIZE);
               
     cout<<"\n\nAuthor: ";
     cin.getline(books[cnt][1],SIZE);

     cout<<"\n\nPublisher: ";
     cin.getline(books[cnt][2],SIZE);        

     cout<<"\n\nISBN: ";
     cin.getline(books[cnt][3],SIZE);
               
     cout<<"\n\nStatus 1=IN,2=OUT: ";
     cin.getline(books[cnt][4],SIZE);                    
               
      
      system("CLS");
      cout<<"Book added.\n\n";
}


bool deletebook (char books[MAX_BOOKS][5][SIZE], int cnt)
{
     
     char title[SIZE];  
     char author[SIZE];  
     char publisher[SIZE];  
     char isbn[SIZE];  

     system("CLS");                
                 
               
     cout<<"***************************\n";
     cout<<"           Delete Book      \n";
     cout<<"***************************\n;

					

char deleteChoice;
     
     cout<<"\n\nThis will delete the book. Are you sure (Y/N)?";
     cin>>deleteChoice;
     system("CLS");
     
     if(deleteChoice == 'Y' || deleteChoice == 'y'){
                     
                     ofstream outfile;
                     outfile.open(bookFilename);
                     
	
        cout<<"Book Deleted.\n\n";
    }else{
        cout<<"Book Delete Cancelled.\n\n";
    }
}




void manageBooks(){
     
     int currentCount =0;
     char books[MAX_BOOKS][5][SIZE];
     
     int choice = -1;     
    
    
    do{
      
       system("CLS");

      
       choice=  bookMenu();

    
         if(choice ==1){
            if(addbook(books, currentCount))
                              currentCount++;
                              system ("pause");
                              
                              
            cout<<"Current Count: "<<currentCount<<endl;
            }
         else if (choice ==2)
            editbook();
         else if (choice ==3){
            if(deletebook(books, currentCount))
                                 currentCount++;
                                 system ("pause");
            }
            
         else if (choice ==4)
            listBooks(false,books,currentCount);
         else if (choice ==5)
            checkinCheckout();
         else if (choice ==6)
         {
           cout<<"\n\nSearch called\n\n";
           system("PAUSE");
         }
         else if (choice ==7)
         {
           cout<<"\n\nSort called\n\n";
           system("PAUSE");
         }
         else if (choice ==8)
              
              cout<<"just going to exit the book section.";
         else
         {
               cout<<"Invalid Book option.\n\n";
               system("PAUSE");
           }
      }while (choice != 8);
}

To delete an element from anywhere, you'll want to use a linked list (std::list) instead of an array. Google "data structures" and you should find some overviews of when/why to use lists/maps/vectors/queues/stacks. Each has advantages and disadvantages.

Hope that helps,
Dave

Quick question.... Are you storing the information about the books in a data structure or in a file ?
The reason I ask is that you have a ofstream object in your delete file function

quick question.... Are you storing the information about the books in a data structure or in a file ?
The reason i ask is that you have a ofstream object in your delete file function

i was storing the information in a file, but now i have to store it in the array...i actually tried a couple times but i just couldnt get it to work so im not sure where to start.

You cant delete an element from an array as such. So you have 2 alternatives .

1. Shift all the elements from one place to the right of the element to be deleted till the last element of the array, one place to the right.

2. Change the entry to be deleted to -1 (or any invalid number) and later when ever you are doing any processing on the array, ignore the invalid entry

Or you could use a linked list. Deleting a node is much more easier

You cant delete an element from an array as such. So you have 2 alternatives .

1. Shift all the elements from one place to the right of the element to be deleted till the last element of the array, one place to the right.

2. Change the entry to be deleted to -1 (or any invalid number) and later when ever you are doing any processing on the array, ignore the invalid entry

Or you could use a linked list. Deleting a node is much more easier

which one is the easiest to apply, and if you can...can help me start on the right path

which one is the easiest to apply, and if you can...can help me start on the right path

I would say the first one is the easiest .... All you need is a simple for loop

This article has been dead for over six months. Start a new discussion instead.