Как добавить функции добавления и удаления книг?
Как стоит реализовать функцию добавления книги? Пыталась сделать что-то на подобие этого, но не уверена что это правильный способ.
//void AddBook(LIBRARY* books, int length, const LIBRARY& newBook) {
// LIBRARY* temp = new LIBRARY[length + 1];
// for (size_t i = 0; i < length; i++)
// {
// temp[i] = books[i];
//
// }
// delete[] books;
// temp[length] = newBook;
// length++;
// books = temp;
//
//}
Сам код:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "MenuFunctions.h"
#include <string>
#include <stdlib.h>
using namespace std;
struct LIBRARY {
char bookname[40];
char author[40];
char publisher[20];
char genre[20];
};
void print_one(LIBRARY p);
void print_all(LIBRARY* p);
LIBRARY EditBook(LIBRARY* p);
void searchAuth(LIBRARY* p);
void searchTitle(LIBRARY* p);
const int bookCount = 8;
int main()
{
LIBRARY books[bookCount] = {
{"Martin Eden", "Jack London", "Macmillan", "novel"},
{"The Picture of Dorian Gray","Oscar Wilde","Belknap Press","fiction"},
{"Harry Potter","JK Rowling","Bloomsbury","fantasy"},
{"To Build a Fire","Jack London","Century","adventure"},
{"No longer human","Dazai Osamu"," Chikuma Shobo","novel"},
{"Metro 2033"," Dmitry Glukhovsky","Eksmo","Post-apocalyptic"},
{"The Great Gatsby","Scott Fitzgerald","Charles Scribner's","tragedy"},
{"The Green Mile","Stephen King","Signet Books","Dark Fantasy"}
};
int choice;
int cursor = 0;
bool isOn = true;
while (isOn) {
system("cls");
PrintMenu(menu, menuLength, cursor);
int key = _getch();
if (key == KEYBOARD_KEYS::enter) {
switch (cursor)
{
case 0: {
print_all(books);
_getch();
break;
}
case 1: {
EditBook(books);
break;
}
case 2: {
searchAuth(books);
_getch();
break;
}
case 3: {
searchTitle(books);
_getch();
break;
}
case 4:
/* case 5:
isOn = false;
system("cls");
cout << "Goodbye!" << endl;
break;
}*/
}
}
else if (key == KEYBOARD_KEYS::down) {
if (cursor == menuLength - 1)
cursor = 0;
else
cursor++;
}
else if (key == KEYBOARD_KEYS::up) {
if (cursor == 0)
cursor = menuLength - 1;
else
cursor--;
}
else if (key == KEYBOARD_KEYS::esc) {
system("cls");
break;
}
}
}//end of main
void print_one(LIBRARY p) {
cout << '\t' << p.bookname << "\n" << "Author: " << p.author << "\n" << "Publisher: " << p.publisher;
cout << "\n" << "Genre: " << p.genre << '\n' << endl;
}
void print_all(LIBRARY* p) {
system("cls");
for (int i = 0; i < bookCount; i++)
print_one(p[i]);
}
LIBRARY EditBook(LIBRARY* p) {
char title[40];
char newtitle[40];
char newAuth[40];
char newPubl[20];
char newGenre[20];
cout << "Enter book title: \n";
cin.getline(title, sizeof(title));
for (size_t i = 0; i < bookCount; i++)
{
if (strcmp(p[i].bookname, title) == 0) {
cout << "Enter new title: \n";
cin >> newtitle;
cout << "Enter new author: \n";
cin >> newAuth;
cout << "Enter new publisher: \n";
cin >> newPubl;
cout << "Enter new genre: \n";
cin >> newGenre;
strcpy(p[i].bookname, newtitle);
strcpy(p[i].author, newAuth);
strcpy(p[i].publisher, newPubl);
strcpy(p[i].genre, newGenre);
return p[i];
}
}
}
void searchAuth(LIBRARY* p) {
char Author[40] = {};
cout << "Enter search book author: \n";
cin.getline(Author, sizeof(Author));
for (size_t i = 0; i < bookCount; i++)
{
if (strcmp(p[i].author, Author) == 0) {
print_one(p[i]);
}
}
}
void searchTitle(LIBRARY* p) {
char title[40];
cout << "Enter search book title: \n";
cin.getline(title, sizeof(title));
for (size_t i = 0; i < bookCount; i++)
{
if (strcmp(p[i].bookname, title) == 0) {
print_one(p[i]);
}
}
}