Auto Creating a library book id code

I am writing c++ code to develop a library database software.
This involves:
Adding a book to the library.
Ill ask the user for book name,author's name and year of publishing.

I want to be able to generate a book id on the basis of the initial letter of the name+ the initial letter of the author + the last two digits of a year + serial no of the book.

So if
Name of the book = Harry Potter and the philosophers stone.
Author's name = Jk Rowling
Year of publication: 1997.
And if there are total of 498 books added prior to this, and this is the 499th book added then,

Then the id will be automatically generated as: HJ97499.
I tried the following approach:
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #include <iostream>
#include <string>
#include <cstring>
#include <fstream>


using namespace std;



char id[8];

char *p1 = &id[4];
char *p2 = &id[5];
char *p3 = &id[6];



void addbook()
{
    cout << "Enter the name: " ;
    char name[50];
    cin.getline(name,50);
    
    cout << "Enter the author: ";
    char author[50];
    cin.getline(author,50);
    
    cout << "Enter the year: ";
    char year[5];
    cin.getline(year,5);
    
    
    
    
    *p1 = *p2 = *p3 = 48; // 48 is the ascii code of digit '0'.. the problem here is i want to initialise the 4th,5th and 6th elements of the array 'id' to ascii code 48. But i dont know how to do it. Initialising it inside the function obviously makes its value 0 each time a book is added. And i want the serial no to increase by 1 each time a book is added.
    
        if (id[6] == 57) // 57 is the ascii code of '9'...the rest of the code is pretty much self explanatory.
        {
            id[6] = 48;
            if ( id[5] == 57 )
            {
                id[5] = 48;
                id[4]++;
            }
            else
            {
                id[5]++;
            }
        }
        else
        {
            id[6]++;
        }

    
    
    id[0] = name[0];
    id[1] = author[0];
    id[2] = year[2];
    id[3] = year[3];
    id[7] = '\0';
    
    cout << "The id is: " << id << endl;
    cout << "If you want to add another book press 1";
    int choice;
    cin >> choice;
    cin.ignore();
    
    if (choice == 1)
    {
        addbook();
    }
    

}

int main()
{
    addbook();
    
    
}
*edit* nevermind... still bleary from waking up...

*edit* #2 nevermind that nevermind, I was right the first time...

line 38 will never evaluate as true, since you hard-code id[6] to be 48 (through p3) on the previous line.
Last edited on
Yes! i know, thats what i am asking you. How am i supposed to initialise the value the elements id[4], id[5] and id[6] to 48 outside the add book() function?

If i try to do it i get a error.
Please help,
move line 36 to just before the call to addbook() in main() at line 80.
omg! i love you. Thank you sooo much!!! Esslercuffi.
Topic archived. No new replies allowed.