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:
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
usingnamespace 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();
}
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?