There is a function to enter a new book (author, title, year, price) NewBook . After entering the second book, the program crashes. I don’t know what the reason is (probably something somewhere with memory allocation, but I don’t know where I wrote something wrong.) Before that, I made two exactly the same tasks, and there everything was fine, and then it crashes. In debugger watched - everything seems to be OK. I throw the code all. The second book is entered, but after entering crashes.

 #include <iostream> using namespace std; struct Books { char *author; char *nazva; int year; int price; }; void NewBook(Books*&, int&); void Deleting(Books*&, int&); void Del(Books*&, int); void Print(Books*&, int&); void main() { Books *boo = NULL; int size = 0; for (int i = 0; i < 3; i++) { NewBook(boo, size); } Print(boo, size); Deleting(boo, size); } void Deleting(Books*&boo, int &size) { if (boo = NULL) cout << "Its okay=) You have'nt any book \n;)" << endl; else { } } void Del(Books *&boo, int size) { for (int i = 0; i < size; i++) delete[]boo[i].author; for (int i = 0; i < size; i++) delete[]boo[i].nazva; delete[]boo; } void Print(Books *&boo, int &size) { for (int i = 0; i < size; i++) { cout << boo[i].author << "\t" << boo[i].nazva << "\t" << boo[i].year << "\t" << boo[i].price << " UAH" << endl; } } void NewBook(Books*&boo, int &size) { if (boo == NULL) { boo = new Books[size + 1]; cout << "Author? " << endl; char*aut = new char[40]; cin.getline(aut, 40); boo[size].author = new char[strlen(aut) + 1]; strcpy_s(boo[size].author, strlen(aut) + 1, aut); //delete[]aut; cout << "Nazva? " << endl; char*aut1 = new char[40]; cin.getline(aut1, 40); boo[size].nazva = new char[strlen(aut1) + 1]; strcpy_s(boo[size].nazva, strlen(aut1) + 1, aut1); cout << "Year? " << endl; cin >> boo[size].year; cout << "Price?" << endl; cin >> boo[size].price; } else { Books *tmp = new Books[size + 1]; for (int i = 0; i < size; i++) { tmp[i].author = new char[strlen(boo[i].author) + 1]; strcpy_s(tmp[i].author, strlen(boo[i].author) + 1, boo[i].author); tmp[i].nazva = new char[strlen(boo[i].nazva) + 1]; strcpy_s(tmp[i].nazva, strlen(boo[i].nazva) + 1, boo[i].nazva); tmp[i].price = boo[i].price; tmp[i].year = boo[i].year; } cout << "Author? " << endl; char*aut = new char[40]; cin.get(); cin.getline(aut, 40); tmp[size + 1].author = new char[strlen(aut) + 1]; strcpy_s(tmp[size + 1].author, strlen(aut) + 1, aut); //delete[]aut; cout << "Nazva? " << endl; char*aut2 = new char[40]; cin.getline(aut2, 40); tmp[size + 1].nazva = new char[strlen(aut2) + 1]; strcpy_s(tmp[size + 1].nazva, strlen(aut2) + 1, aut2); cout << "Year? " << endl; cin >> tmp[size + 1].year; cout << "Price?" << endl; cin >> tmp[size + 1].price; Del(boo, size); /*for (int i = 0; i < size; i++) delete[]boo[i].author; for (int i = 0; i < size; i++) delete[]boo[i].nazva; delete[]boo;*/ boo = tmp; } size++; } 
  • one
    and specifically where flies? - Senior Pomidor
  • And it does not compile. On the other hand, I see no reason why here the tag is c ++ 11, if for good, it’s time to start the “with +” tag (yes, with one plus). - KoVadim
  • one
    First you need to correct syntax errors. - drstannum
  • Funny if you check if (boo = NULL) . However, the problem is not in it, it does not reach it ... - Harry
  • @KoVadim, yes, c+ :) Is it really interesting that this is some kind of flash mob source, where do the functions have a lot of parameter references to pointers? Or some new video course out? - PinkTux

1 answer 1

I rewrote it all, making it more positive than it was before. It has even become shorter.

Of course, experienced pluses will come along and say that this is no longer a +++ and can be more beautiful, but I'm afraid in this case the code will be incomprehensible not only @anonimoys, but also its teacher.

 #include <iostream> #include <vector> using namespace std; class Book { public: string author; string nazva; int year; int price; void print() { cout << author << "\t" << nazva << "\t" << year << "\t" << price << endl; } }; class Books { private: vector<Book> mBooks; public: void print() { for (int i = 0; i < mBooks.size(); i++) { mBooks[i].print(); } } void add(Book b) { mBooks.push_back(b); } }; Book inputBook() { Book b; cout << "Author? " << endl; getline(cin, b.author); cout << "Name? " << endl; getline(cin, b.nazva); cout << "Year? " << endl; cin >> b.year; cout << "Price?" << endl; cin >> b.price; cin.ignore(1000, '\n'); return b; } int main() { Books boo; for (int i = 0; i < 3; i++) { Book b = inputBook(); boo.add(b); } boo.print(); return 0; }