Почему программа завершается аварийно?
Ошибка возникает при поиске книг по автору. Сначала ввыводиться правильная информация, но по завершению цикла программа ломается.Скажите, пожалуйста, что не так.
#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
struct book {
char bookname[70];
char author[50];
char publisher[20];
char genre[20];
};
book EditBook(book* books);
book searchAuth(book* books);
void print_one(book exp);
void print_all(book* exp);
int main()
{
book* BOOK;
book books[10] = { {"Caroline","Neil","Bloomsbury","horror"},
{"Alice","Neil","Bloomsbury","fantasy"},
{"HarryPotter","Rowling","Bloomsbury","fantasy"},
{"MartinEden","Martin Lee","NewDay","sci-fi"},
{"SherlockHolmes","Sd","OldDay","detective"},
{"C++","FF","HTEW","horror"},
{"Python","Rer","Kwed","adventure"},
{"Harry Grotter","Ann","Bloom","fantasy"},
{"Mart","Scier","Toyota","sci-fi"},
{"Sher","Sd","OldDay","psychology"},
};
int choice;
bool isOn = true;
enum opt { exit = 0, edit = 1, showAll = 2, sortAuth = 3, sortTitle = 4 };
while (isOn) {
cout << "\n\t\t" << "WELCOME!\n";
cout << "(0)Exit (1)Edit (2)Show all books (3) Sort by author (4) Sort by title: ";
cin >> choice;
switch (choice)
{
default:
isOn = false;
break;
case edit:
EditBook(books);
break;
case showAll:
print_all(books);
break;
case sortAuth:
searchAuth(books);
break;
case sortTitle:
break;
}
}
}
void print_one(book exp)
{
cout << "BOOK INFO:\n" << "\tTitle: " << exp.bookname << "\t\t" << "\tAuthor: " << exp.author << "\t\t" << " \tPublisher: " << exp.publisher;
cout << "\t\t" << "\tGenre: " << exp.genre << endl;
}
void print_all(book* exp)
{
cout << "Name:" << "\t\t" << "Author:" << "\t\t" << "Publisher:";
cout << "\t" << "Genre:";
cout << "\n\n";
for (int i = 0; i < 10; i++)
print_one(exp[i]);
}
book EditBook(book* BOOK) {
char title[30];
char newtitle[30];
cout << "Enter search book title: \n";
cin >> title;
for (size_t i = 0; i < 10; i++)
{
if (strcmp(BOOK[i].bookname,title)==0) {
cout << "Enter new title: \n";
cin >> newtitle;
*BOOK[i].bookname = *newtitle;
return BOOK[i];
}
}
cout << "No such book title\n";
throw "";
}
book searchAuth(book* BOOK) {
char Author[30];
cout << "Enter search book author: \n";
cin >> Author;
for (size_t i = 0; i < 10; i++)
{
if (strcmp(BOOK[i].author, Author) == 0) {
print_one(BOOK[i]);
}
}
cout << "No such book title\n";
throw "";
}
Ответы (1 шт):
Автор решения: 19g9nd
→ Ссылка
Рабочая программа:
#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
struct book {
char bookname[70];
char author[50];
char publisher[20];
char genre[20];
};
book EditBook(book* books);
void searchAuth(book* books);
void print_one(book exp);
void print_all(book* exp);
void searchTitle(book* books);
int main()
{
book* BOOK;
book books[10] = { {"Caroline","Neil","Bloomsbury","horror"},
{"Alice","Neil","Bloomsbury","fantasy"},
{"HarryPotter","Rowling","Bloomsbury","fantasy"},
{"MartinEden","Martin Lee","NewDay","sci-fi"},
{"SherlockHolmes","Sd","OldDay","detective"},
{"C++","FF","HTEW","horror"},
{"Python","Rer","Kwed","adventure"},
{"HarryGrotter","Ann","Bloom","fantasy"},
{"Mart","Scier","Toyota","sci-fi"},
{"Sher","Sd","OldDay","psychology"},
};
int choice;
bool isOn = true;
enum opt { exit = 0, edit = 1, showAll = 2, sortAuth = 3, sortTitle = 4 };
while (isOn) {
cout << "\n\t\t" << "WELCOME!\n";
cout << "(0)Exit (1)Edit (2)Show all books (3) Sort by author (4) Sort by title: ";
cin >> choice;
switch (choice)
{
default:
isOn = false;
break;
case edit:
EditBook(books);
break;
case showAll:
print_all(books);
break;
case sortAuth:
searchAuth(books);
break;
case sortTitle:
searchTitle(books);
break;
}
}
}
void print_one(book exp)
{
cout << "BOOK INFO:\n" << "\tTitle: " << exp.bookname << "\t\t" << "\tAuthor: " << exp.author << "\t\t" << " \tPublisher: " << exp.publisher;
cout << "\t\t" << "\tGenre: " << exp.genre << endl;
}
void print_all(book* exp)
{
cout << "Name:" << "\t\t" << "Author:" << "\t\t" << "Publisher:";
cout << "\t" << "Genre:";
cout << "\n\n";
for (int i = 0; i < 10; i++)
print_one(exp[i]);
}
book EditBook(book* BOOK) {
char title[30];
char newtitle[30];
cout << "Enter search book title: \n";
cin >> title;
for (size_t i = 0; i < 10; i++)
{
if (strcmp(BOOK[i].bookname,title)==0) {
cout << "Enter new title: \n";
cin >> newtitle;
*BOOK[i].bookname = *newtitle;
return BOOK[i];
}
}
cout << "No such book title\n";
throw "";
}
void searchAuth(book* BOOK) {
char Author[30];
cout << "Enter search book author: \n";
cin >> Author;
for (size_t i = 0; i < 10; i++)
{
if (strcmp(BOOK[i].author, Author) == 0) {
print_one(BOOK[i]);
}
}
}
void searchTitle(book* BOOK) {
char title[30];
cout << "Enter search book title: \n";
cin >> title;
for (size_t i = 0; i < 10; i++)
{
if (strcmp(BOOK[i].bookname, title) == 0) {
print_one(BOOK[i]);
}
}
}