MSc Ag Genetics & Plant Breeding: Insights from Previous Year JNKVV Entrance ...
(C++ programming)menu quit, book show, book change, book remo.docx
1. (C++ programming)
menu:
* quit, book show, book change, book remove, book insert,
member show, member change, member remove, member insert,
borrow, return.
*Store the book and member information to the “book.txt” and
“member.txt” file
* Store the borrow and return information to the “manage.txt”
file
Solution
#include<iostream.h>
#include<string.h>
#include<iomanip.h>
#include<fstream.h>
#include<strstream.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
#include<conio.h>
#include<ctype.h>
2. void create_book(void);
void create_member(void);
void search_book(char [][3],int );
void search_member(char [][3],int);
void book_issue(char [][3],char[][3],int,int,int [],char,char, int
[]);
void book_return(char [][3],char[][3],int,int,int [],char,char, int
[]);
char due_date(void);
char date_format(void);
void
check_due_date(char[],int,char[][3],int,char[][3],char[][5]);
int main ()
{
char pass[20];
top:
clrscr();
for(int star = 0 ; star < 80 ; star++)
cout << "_";
cout << "" << setw(50) << "Welcome to The Library :" <<
setw(29) << "";
for(int star1 = 0 ; star1 < 80 ; star1++)
cout << "_";
cout << endl;
cout << "Please Enter the Password to Gain Access to This
3. Program :";
cin >> pass;
if(pass == "project")
{
cin.ignore();
char date;
date = date_format(); //Calling function for date
clrscr();
top2:
int choice_section; //Choice of section inside the library
(admin/library)
int choice_database; //Choice of database in the admin
section(seeing lists, adding books etc)
int choice_lib; //Choice of database in the library section
int choice_issue; //Choice for searching or entering code
of the book while issuing it
int count_book[100]; //number of books each user has
issued
int user_issue[100][5]; //member code and the code of the
book that the user has issued
int user_count[100]={0}; //number of books issued by user
int avail_book[100]; //book availibilty(1 when available
and 0 when not available)
for (int i = 0 ; i < 100 ; i++)
{
4. avail_book[i] = 1;
count_book[i] = 1;
}
char pass[20]; //password for entering in the library
char pass_admin[20]; //password for entering in the admin
section of the library
char book[100][3]; //array for storing a book's code with its
name, author, publisher etc
char member[100][3]; //array for storing a member's code
with his/her name, address etc
char username; //username of member
char date_book[100]; //date on which the book was issued
char date_member[100][5]; //date on which a particular
member issued a book
char due_date[100]; //due date of book (date on which the
book is to be returned)
char due_date_member[100][5]; //date on which a particular
member has to return a book
int access_number = 1; //a unique book code for every book
int membership_number = 1; //a unique member code for
every member
int access_number_enter; //book code entered by the user
while issuing/returning it
int membership_book_issue[100] = {0}; //code of the
member who has issued the book
5. void read_books();
void read_members();
void book_avail();
void member_status();
//CLEARING SCREEN
clrscr();
//main options
for (int star = 0 ; star < 49 ; star++)
cout<<"_";
cout << endl;
cout << "tTo Access the Admin Section Press 1t tTo
Access The Library Section Press 2 t tTo Quit Program
Press 3 tt" << endl;
for (int star2= 0 ; star2< 49 ; star2++)
cout << "_";
cout<<endl;
cout << "Enter your choice(1, 2 or 3): ";
cin >> choice_section;
cin.ignore();
if (choice_section == 3)
goto end;
if (choice_section == 1)
{
cout << "Enter the Admin Password ";
cin >> pass_admin;
6. cin.ignore();
if (pass_admin == "admin")
{
clrscr();
for (int star = 0 ; star < 65 ; star++)
cout << "_";
cout << endl;
cout << "" << setw(35) << "Welcome" << setw(29)
<< "" << endl;
cout << "tTo Enter New Books in Database press
1ttt tTo Enter New Members in Member Database Press
2t tTo See The List of All Members press 3ttt tTo See
The List of All Books Press 4ttt tTo Go Back To The
Previous Menu Press 5:tt" << endl;
for (int star3 = 0 ; star3 < 65 ; star3++)
cout << "_";
cout << endl;
cout << "Enter your choice(1-5): ";
cin >> choice_database;
cin.ignore();
clrscr();
if (choice_database == 1)
{
create_book(); //CALLING FUNCTION CREATE
BOOK
7. }
else if (choice_database == 2)
{
create_member(); //CALLING FUNCTION CREATE
MEMBER
}
else if (choice_database == 3)
{
cout << "member_name" << "ttt" <<
"member_address" << "ttt" << "UserName" << "t"
<< endl;
for(int i = 0 ; i < membership_number; i++)
{
//here i represents respective member code
cout << member[i][0] << "ttt" <<
member[i][1] << "ttt" << member[i][2] << "t" << endl;
if (i == 15)
system("pause");
}
system ("pause" );
}
else if (choice_database == 4)
{
cout << "book_name" << "tt" << " Author_1_Name"
<< "t" << "Author_2_Name" << "tt" << "code" <<
8. endl;
for(int i = 1 ; i < access_number ; i++)
{
//here i represents book code
cout << book[i][0] << "tt" << book[i][1] <<
"tt" << book[i][2] << "tt" << i << "t" << endl;
//program will display 10 books and then wait for
user to press any key before it continues
if(i == 10)
system ("pause");
}
system ("pause");
}
else if (choice_database == 5)
goto top2;
else
{
cout << "Wrong Choice "<< endl;
clrscr();
goto top2;
}
}
else
{
cout<<"Wrong Password Entered ";
9. system ("pause");
clrscr();
goto top2;
}
}
else if(choice_section == 2)
{
clrscr();
for (int star = 0 ; star < 57 ; star++)
cout << "_";
cout << endl;
cout << "tTo Search For a Books Code Press 1tt tTo
Search For A Member Press 2ttt tTo Issue A Book Press
3tttt tTo Return a Book Press 4ttt tTo Check
Status Of A member Press 5tt tTo Check Staus Of A Book
Press 6tt tTo Check If Any Book Is Due Today Press 7t
tTo Go Back To The Previous Menue Press 8t"<<endl;
for (int star4 = 0 ; star4 < 57 ; star4++)
cout << "_";
cout << endl;
cout << "Enter your choice (1-8): ";
cin >> choice_lib;
cin.ignore();
clrscr();
if (choice_lib == 1 || choice_lib == 2 || choice_lib == 3 ||
10. choice_lib == 4 || choice_lib == 5 || choice_lib == 6 ||
choice_lib == 7 || choice_lib ==8) {
if (choice_lib == 8)
goto top2;
if (choice_lib == 1)
{
search_book (book,access_number); //CALIING
SEARCH BOOK FUNCTION
goto top2;
}
if (choice_lib == 2)
{
search_member (member,membership_number);
//CALIING SEARCH MEMBER FUNCTION
goto top2;
}
if (choice_lib == 3)
{
cout << "If You know the Code of the Book Press 1 to
enter it If You Want to Search for the book first press 2" <<
endl;
cout << "Enter your choice (1-2): ";
cin >> choice_issue;
cin.ignore();
if (choice_issue == 2)
11. {
search_book (book,access_number);
}
cout << "Enter The Username of The Member Who
Wants to Issue the Book :";
get(username);
cout << "Enter the Book Code :";
cin >> access_number_enter;
cin.ignore();
// if the code entered by the user is more than the total
number of book codes in memory give an error
if (access_number_enter > access_number-1)
{
cout << " Wrong Book Code Entered" << endl;
}
else
book_issue (book,member, access_number_enter,
membership_number, count_book, date, username, avail_book);
system ("pause");
goto top2;
}
if(choice_lib == 4)
{
cout << "If You Want to Enter The Code of the Book
Press 1 If You Want to Search for the book First press 2" <<
12. endl;
cin >> choice_issue;
cin.ignore();
if (choice_issue == 2)
{
search_book (book, access_number);
}
cout << "Enter The Username of The Member Who
Wants to Return the Book :";
get(username);
cout << "Enter the Book Code :";
cin >> access_number_enter;
cin.ignore();
if (access_number_enter > access_number-1)
{
cout << " Wrong Book Code Entered" << endl;
}
else
book_return (book, member, access_number_enter,
membership_number, count_book, date, username, avail_book);
system ("pause");
clrscr();
goto top2;
}
if (choice_lib == 5)
13. {
int user_code_found, choice_member;
cout << "To Enter username Of The Member You Want
to Check Status Of Press 1 To Search For The Member
Press2:"<< endl;
cin >> choice_member;
cin.ignore();
if(choice_member== 2)
{
search_member(member, membership_number);
goto member;
}
if(choice_member== 1)
{
member:
cout << "Enter The username :";
get(username);
int check = 0;for (user_code_found = 1 ;
user_code_found < membership_number ; user_code_found++)
{
if (member[user_code_found][2] == username)
{
check = 1;
break;
}
15. {
cout << " No Books Have Been Issued by " <<
member[user_code_found][0] << endl;
system("pause");
goto top2;
}
}
}
else
{
cout << "Wrong Choice Entered" << endl;
system ("pause");
clrscr();
goto top2;
}
}
if (choice_lib == 6)
{
int choice_book_status;
cout << "If You Want To Enter The Code of The Book
Press 1 If You Want To Search For The Book Press 2" <<
endl;
cin >> choice_book_status;
cin.ignore();
16. if (choice_book_status == 2)
{
search_book (book, access_number);
}
else if(choice_book_status == 1)
{
cout << "Enter The Code of The Book :";
cin >> access_number_enter;
//checking if the book code is valid
if(access_number_enter > access_number-1)
cout << "Wrong access_number entered ";
else
{
// 1 represents that book is available
if (avail_book[access_number_enter] == 1)
{
// printing the name of the book (stored in
subscript [0])
cout << book[access_number_enter][0] << " is
available" << endl;
}
//0 represents that book is not available
else if (avail_book[access_number_enter] == 0)
{
//printing the name of the book and to whome it
17. has been issued to and when
cout << book[access_number_enter][0] << "
Has Been Issued to " << member
[membership_book_issue[access_number_enter]][0] << " On "
<< date_book[access_number_enter] << " Till The Due Date :"
<< due_date[access_number_enter] << endl;
}
}
}
system ("pause");
clrscr();
goto top2;
}
if (choice_lib == 7)
{
check_due_date (due_date, access_number, book,
membership_number, member, due_date_member);
system("pause");
goto top2;
}
}
else
{
cout << "Wrong Choice Entered " << endl;
system ("pause");
19. {
char book_name, author_1_name, author_2_name;
char choice;
ofstream OutBook;
//opening the book txt file
OutBook.open ("books.txt", ios::app);
do
{
//inputing the info about the book
cout << "Enter Book Name :";
getline (cin, book_name);
cout << "Enter first Aurthors Name :";
getline (cin, author_1_name);
cout << "Enter second authors name :";
getline (cin, author_2_name);
//storing it in the file
OutBook << book_name << "#" << author_1_name << "$"
<< author_2_name<< endl;
cout << "If You Want to Enter Another Book press Y else
Press N :";
cin >> choice;
cin.ignore();
choice = tolower(choice);
//creating a seperate txt file for the book to store its data in
ostrstream os;
20. os << book_name << ".txt";
ofstream OutBookAvail( os.str().c_str() );
OutBookAvail << 1 << "$" << 0 << "&" << "1/1/2010"
<< "*"<< "2/1/2010" << endl;
OutBookAvail.close();
} while (choice == 'y');
OutBook.close();
}
void create_member(void)
{
char choice;
char member_name, address, user_name;
ofstream OutMember;
//opening member txt file
OutMember.open ("member.txt",ios::app);
do
{
cout << "Enter members Name :";
getline (cin, member_name);
cout << "Enter members address :";
getline (cin, address);
cout << "Enter The Username :";
getline (cin, user_name);
//storing necessary data in it
21. OutMember << member_name << "#" << address <<
"$" << user_name << endl;
cout << "Enter Y if You want to Enter Data of Another
member else Press N :";
cin >> choice;
cin.ignore();
//creating a seperate txt file for the member
ostrstream ss;
ss << member_name << ".txt";
ofstream OutMemberAvail ( ss.str().c_str(),ios::app );
OutMemberAvail << 0 << "$" << "1/1/2010" << '*' <<
"2/2/2010" << endl;
OutMemberAvail.close();
} while (choice == 'y');
}
void search_book(char book[][3], int access_number)
{
clrscr();
int i, j, length1, length2, match = 0;
char book_search, book_new;
cout << "Enter The Name of The Book You Want to Search
:";
getline (cin, book_search);
cout << " Following Are The Books That Matched Your
22. Given Book Name" << endl;
cout << "book name" << "tt" << " author 1 name" <<
"t" << "author 2 name" << "tt" << "code" << endl;
for ( i = 1 ; i <= access_number ; i++)
{
length2 = book_search.length();
length1 = book[i][0].length();
match = 0;
// runs till the length of the book name entered for
searching
for (j = 0 ; j < length2-1 ; j++)
{
// runs till the length of book name in database
for (int k = 0 ; k < length1-1 ; k++)
{
//lowering all charecters for easy access
book[i][0][k] = tolower(book[i][0][k]);
book_search[j] = tolower(book_search[j]);
// if they are equal then counter match increases by 1
if (book[i][0][k] == book_search[j])
{
match++;
break;
}
}
23. }
//if match is greater than 1 less then the length of the name
given by user .
if (match >= length2-1)
{
//printing the book name author and code .. here i
represents the book code
cout << book[i][0] << "tt" << book[i][1] <<
"tt" << book[i][2] << "tt" << i << "t" << endl;
}
}
system ("pause");
}
void search_member(char member[][3],int
membership_number)
{
// searching member by same criteria as well
clrscr();
int i, j, length1, length2, match = 0;
char member_search, member_new;
cout << "Enter The Name of The Member You Want to
Search :";
getline (cin, member_search);
cout << " Following Are The Members That Matched Your
24. Given Book Name" << endl;
cout << "member name" << "ttt" << "address" <<
"ttt" << "UserName" << "t" << endl;
for (i = 1 ; i <= membership_number-1 ; i++)
{
length2 = member_search.length();
length1 = member[i][0].length();
match = 0;
//runs till length of the name entered for search
for (j = 0 ; j < length2-1 ; j++)
{
// runs till length of the name in database
for (int k = 0 ; k < length1-1 ; k++)
{
member[i][0][k] = tolower(member[i][0][k]);
member_search[j] = tolower(member_search[j]);
if (member[i][0][k] == member_search[j])
{
match++;
break;
}
}
}
if (match >= length2-1)
{
25. cout << member[i][0] << "ttt" << member[i][1]
<< "ttt" << member[i][2] << "t" << endl;
}
}
system ("pause");
clrscr();
}
void book_issue(char book [][3], char member[][3], int
access_number, int membership_number, int count_book[], char
date, char username, int avail_book[])
{
int check = 0;
// 0 represents that book isn't available. checking if it is or
not
if (avail_book[access_number] == 0)
{
cout << "Book Is Not Available " << endl;
}
else
{
int i;
for (i = 1 ; i < membership_number ; i++)
{
// searching for the user whose username has been entered
26. if (member[i][2] == username)
{
check = 1;
break;
}
else
check = 0;
}
if (check == 0)
{
cout << " Wrong User Name " << endl;
}
else
{
// here i represents member code . if that member has
already issued 4 books i.e his counter is more than 4 it gives an
error
if (count_book[i] > 4)
cout << " You Have Issued 4 Books Already, Please
Return a Book to get More Books Issued" << endl;
else
{
// setting the availbility status of that book to 0 (0
represents its not available)
avail_book[access_number] = 0;
27. //using due date function to store the due date
char date_due = due_date();
//displaying book name and who has issued it and other info
cout << book[access_number][0] << " By " <<
book[access_number][1] << " Has Been Issued to " <<
member[i][0] << " on " << date << " Till The Due Date :" <<
date_due << endl;
//increasing the count of the student by 1
count_book[i]++;
//opening the txt file of the respective book
ostrstream os;
os << book[access_number][0] << ".txt";
ofstream OutBookAvail ( os.str().c_str() );
OutBookAvail << avail_book[access_number] << "$" << i <<
"&" << date << "*" << date_due << endl;
OutBookAvail.close();
ostringstream ss;
ss << member[i][0] << ".txt";
ofstream OutMemberAvail ( ss.str().c_str(),ios::app );
OutMemberAvail << access_number << "$" << date << "*"
<< date_due << endl;
OutMemberAvail.close();
}
}
}
28. }
void book_return(char book [][3], char student[][3], int
access_number, int membership_number, int count_book[], char
date, char username, int avail_book[])
{
int check = 0;
// if the status of book is 1 it means it hasent been issued
yet
if (avail_book[access_number] == 1)
{
cout << "The Book Hasent Been Issued yet" << endl;
}
else
{
int i;
int check = 0;
//searching for the member with the given username
for (i = 1 ; i < membership_number ; i++)
{
if (member[i][2] == username)
{
check = 1;
break;
}
29. else
check = 0;
}
if (check == 0)
{
cout << " Wrong User Name " << endl;
}
else
{
// opening the book file and storing the data in it
temporary
int temp_user, temp_avail;
char temp_Date, temp_Due_Date;
ostringstream os;
os << book[access_number][0] << ".txt";
ifstream BAvail( os.str().c_str() );
while (!BAvail.eof())
{
BAvail >> temp_avail;
BAvail.ignore();
// code of the user who has issued the book
BAvail >> temp_user;
BAvail.ignore();
getline (BAvail, temp_Date,'*');
getline(BAvail, temp_Due_Date);
30. }
// if the username of the user wanting tor return the book
matches with the user whose code is stored in the book's txt
file then program continues
if (temp_user == i)
{
count_book[i]--;
avail_book[access_number] = 1;
cout << book[access_number][0] << " by " <<
book[access_number][1] << " Issued on : " << temp_Date <<
" has been returned by " << member[i][0] << " on " << date
<< endl;
//opening the text file of the book;
ostringstream os;
os << book[access_number][0] << ".txt";
ofstream OutBookAvail( os.str().c_str() );
//setting the values back to default as they were when we
created the new text file
OutBookAvail << 1 << "$" << 0 << "&" << "1/1/2010"
<< "*" << "2/1/2010" << endl;
OutBookAvail.close();
int temp_code, temp_count;
char temp_date;
//opening the members txt file.
ostringstream ss;
31. ss << member[i][0] << ".txt";
ifstream mAvail( ss.str().c_str(),ios::app );
// reading the data
// storing book code
mAvail >> temp_code;
mAvail.ignore();
//temp date
getline (mAvail, temp_date);
while (!mAvail.eof())
{
// if the code of the book is equal to the code of the book
returned then its not copied hence removing the code of the
book that has been returned
if (temp_code == access_number)
{
}
else
{
// storing data of other books in a new temp txt file
ostringstream ss;
ss << member[i][0] << "_temp.txt";
ofstream OutMemberAvail( ss.str().c_str(),ios::app);
OutMemberAvail << temp_code << "$" << temp_date <<
endl;
OutMemberAvail.close();
32. }
mAvail >> temp_code;
mAvail.ignore();
getline (mAvail, temp_date);
}
mAvail.close();
ostringstream ds;
//removing the text file of the student
ds << member[i][0] << ".txt";
remove ( ds.str().c_str());
ostringstream es;
es << member[i][0] << "_temp.txt";
ostringstream ps;
ps << member[i][0] << ".txt";
//renaming the temporary file to the original . in this way we
have modified the txt file of the member removing data of the
book returned and it only contain data of books that are still
issed to him (if any)
rename ( es.str().c_str(),ps.str().c_str());
}
// if the username given does not match with the username of
member stored in the books txt file while issuing than it gives
an error
else
cout << " The Book Has Not Been Issued By This User :"
33. << endl;
}
}
}
char due_date(void)
{
char due_date;
int date = 0, month = 0 , year = 0;
//calling date from computer
char date_cal[9];
_strdate (date_cal);
ofstream OutFile;
OutFile.open("date.txt");
//storing it into date.txt
OutFile << date_cal;
OutFile.close();
ifstream InFile;
//reading date.txt and storing days months and year seperatly
in different int
InFile.open ("date.txt");
InFile >> month;
//to ignore /
InFile.ignore();
InFile >> date;
34. // to ignore /
InFile.ignore();
InFile >> year;
// calculating for feburary
if (month == 2)
{
//checking if leap year
if (year%400 == 0 || (year%100 != 0 && year%4 == 0))
{
//making necesarry calculations
if (date <= 8)
date = date+21;
else if(date > 8)
{
date = (date+21) - 29;
month = month++;
}
}
else if (date <= 7)
date = date+21;
else if (date > 7)
{
date = (date+21) - 28;
month = month++;
if (month > 12)
35. {
//resetting month to 1 and increasing year month is
greater than 12
month = 1;
year++;
}
}
}
/