Exercise for a Library

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.

CODE:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <fstream>
using namespace 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;
};

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;
}
First, read about streams. Nicer than fopen() and scanf().

std::string is safer than strcat() and char arrays. std::vector<Book> is safer than Book *.

You must decide the format of the file. What separates author names? Do you read first number of entries, or check for "end" after each entry?
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
getline() function can have a delimeter parameter which as name implied specify delimeter of string tokens, i.e. you can do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
struct bookInfo
{
    int ID;
    std::string name;
    std::string author_firstName
    std::string author_lastName;
}

bookInfo readInfo(std::istream& in)
{
    bookInfo tmp;
    std::string data;
    getline(in, data, '.');
    tmp.ID = std::stoi(data);
    getline(in, tmp.name, '.');
    getline(in, tmp.author_firstName, '.');
    getline(in, tmp.author_lastName, '\n'); //reading till the end of line here
    return tmp;
}

int main()
{
    std::ifstream input("bookdata.txt");
    std::vector<bookInfo> books;
    while(input) {
        books.push_back(readInfo(input))
    }
}
Thanks all! I have new instructions:

The start code is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cstdio>
#include <iostream>
#include <cstring>
#include <fstream>
using namespace 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!!!
Last edited on
closed account (ETAkoG1T)
Use code tags, "my eyes!", it hurts looking at that text.
Hahaha! Sorry, hope now is better! :)
Any suggestion? Thanks!
Topic archived. No new replies allowed.