Hello all! I am new here and almost newbie with C++. I have an exercise for my Library and I would like to ask you for your help. This is the code I have already written. Could anyone help me and show me how to continue with my problem? I am stuck with the loading and the way to think about it.
struct Book{
int id;
char title[256];
char summary[2048];
int numberOfAuthors;
Author *authors;
};
struct Books{
Book *arr;
int numberOfBooks;
};
void load()
{
}
int main ()
{
int choice;
char filename[80];
cout<<"Enter file name: ";
scanf ("%123s", filename);
strcat (filename, ".txt");
FILE*inputf;
inputf = fopen (filename, "r");
if (inputf == NULL)
{
cout<<"We will call the default file!";
}
else
{
cout<<"We will use: "<<filename<<" file!\n";
}
cout<<endl<<"Enter your choice:\n1. Load books from file\n2. Save books to file\n3. Add a book\n4. Delete a book by id\n"
<<"5. Display a book by id\n6. Display a book by title\n7. Display books\n8. Display books by surname search\n"
<<endl<<"Choice: ";
cin>>choice;
while (choice<1||choice>8)
{
cout<<"Your choice must be a number from 1 to 8. Try again: ";
cin>>choice;
}
if (choice==1)
{
load();
cout<<"Books were loaded form file!";
}
return 0;
}
Thanks! I will have a .txt like i.e: 12.Harry Potter.<summary>.Joanne.Rowling
12 is the id of the book
Harry Potter the title
A short summary
First and Last name of the author
#include <cstdio>
#include <iostream>
#include <cstring>
#include <fstream>
usingnamespace std;
struct Author{
char firstName[56];
char lastName[56];
};
struct Book{
int id;
char title[256];
char summary[2048];
int numberOfAuthors;
Author *authors;
};
struct Books{
Book *arr;
int numberOfBooks;
};
int main ()
{
int choice=10;
while (choice!=0)
{
cout<<"Enter your choice:\n1. Load books from file\n2. Save books to file\n3. Add a book\n4. Delete a book by id\n"
<<"5. Display a book by id\n6. Display a book by title\n7. Display books\n8. Display books by surname search\n"
<<endl<<"Choice: ";
cin>>choice;
while (choice<0||choice>8)
{
cout<<endl<<"Your choice must be a number from 1 to 8. Try again: ";
cin>>choice;
}
if (choice==0)
{
break;
}
}
return 0;
}
Instructions: The file where you write or load data will be passed in main as argument. When you choose load (1st option) the array "arr" will take memory with malloc. The first element of the data will be the number of the books. When you add a Book, the array will be increased by 1 with realloc. At 4,5 we will use serial search by id and at 6,8 serial search by title and surname. At 4 in order to delete the Book we have to swap with the last element of the array and use realloc in -1 size.
That's the explanation of my work. Do you have any idea to help me or any example to see some parts of code to give me information? Thanks for all and for your help all!!!