Доброго усім дян. Допоможіть, будь ласка розібратися.Створюю програму "бібліотека" і запоровся на додаванні джерела. Я вже погуглив і туди прохання не відправляти.
Проблема в наступному: коли функція get_sources_from_user переходить до заповнення другого елементу структури source_type, програма просто вирубається. Допоможіть розібратися в чому проблема.
#include <ctime>
#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>
#include<windows.h>
using namespace std;
struct Item
{
int serial_number;
string source_type;
string name;
string author;
int year;
string genre;
string publisher;
string is_given;
};
#pragma region Show_library
//Вивід бібліотеки на екран
void print_source(Item *source)
{
cout << setw(17) << "Serial number" << " : " << source->serial_number << endl
<< setw(17) << left << "Type" << " : " << source->source_type << endl
<< setw(17) << left << "Name" << " : " << source->name << endl
<< setw(17) << left << "Author" << " : " << source->author << endl
<< setw(17) << left << "Date" << " : " << source->year << endl
<< setw(17) << left << "Genre" << " : " << source->genre << endl
<< setw(17) << left << "Publiher" << " : " << source->publisher << endl
<< setw(17) << left << "Given to" << " : " << source->is_given << endl
<< "--------------------------------------------------------------------------------" << endl;
}
void print_sources(Item *sources, const int quantity)
{
//cout << setw(20) << "Book name" << setw(20) << "Author" << setw(15) << "Publisher" << setw(15) << "Genre" << endl;
cout << "List of sources" << endl << endl;
for (Item *ptr = sources; ptr < sources + quantity; ++ptr)
{
print_source(ptr);
}
}
#pragma endregion
#pragma region Add_source
//заповнення бібліотеки користувачем
void get_sources_from_user(Item *source, int index)
{
source->serial_number = index; // копіюється без проблем
int type = 0;
do
{
cout << "Choose source type :" << endl
<< "1. Book" << endl
<< "2. Magazine" << endl
<< "3. News paper" << endl << endl
<< "Make a choice : ";
cin >> type;
} while (type < 1 || type > 3);
switch (type) // тут починаються проблеми. який би вибір не зробили - програма "вилітає"
{
case 1: source->source_type = "book";
break;
case 2: source->source_type = "magazine";
break;
case 3: source->source_type = "news paper";
break;
default:
cout << "Wrong choice!" << endl;
break;
}
cin.ignore();
cout << "Type the name of a source : ";
getline(cin, source->name);
cout << "Type the author of a source : ";
getline(cin, source->author);
cin >> source->year;
cout << "Type the publisher of a source : ";
getline(cin, source->publisher);
cout << "Type the genre of a source : ";
getline(cin, source->genre);
}
void move_source(Item *src, const int count, Item *dst)
{
for (Item *src_ptr = src, *dst_ptr = dst; src_ptr < src + count; ++dst_ptr, ++src_ptr)
{
*dst_ptr = *src_ptr;
//src_ptr = nullptr;
}
}
Item* AddStruct(Item* source, int *file_size)
{
int newSize = *file_size + 1;
if (file_size == 0)
{
source = new Item[newSize]; // виділення пам'яті для першої структури
}
else
{
Item *new_source = new Item[newSize]; //створюємо масив структур розміром +1 книга
move_source(source, *file_size, new_source); //копіюємо в новий масив існуючі книги
get_sources_from_user((new_source + newSize), newSize); // в останній елемент масиву вводимо дані нової книги
delete[] source;
source = new_source;
delete[] new_source;
}
*file_size = newSize;
return source;
}
void add_source(Item *sources, int file_size) // додаємо нові книги
{
char YesOrNot;
do
{
sources = AddStruct(sources, &file_size); //додаємо до масиву нову структуру
file_size++;
do
{
cout << "Add onather source? (y/n): ";
cin >> YesOrNot;
cin.get();
} while (YesOrNot != 'y' || YesOrNot != 'n');
if (YesOrNot = 'n')
{
save_file(sources, file_size);
}
} while (YesOrNot != 'n');
print_source(sources + file_size);
}
#pragma endregion