Question

IN C++ add Inheritance, Class and Pointers to program This is a library management program, users...

IN C++ add Inheritance, Class and Pointers to program

This is a library management program, users can borrow books and return books.

Just incorporate the use of Inheritance, Class and Pointers into this program.
The program should work the same after your edits.

#include

using namespace std;

//structure to store library data

struct library

{

int code;

char title[20];

int status;

};

//structure to store user data

struct users

{

int id;

char name[20];

int booksNumber;

};

int main()

{

int tempId, i, flag = 0, j;

char tempName[20];

int tempCode;

struct users user_details[100]; //variable to hold 100 userdetails

struct library book_details[100]; //variable to hold 100 book details

int user_count = 0, books_count = 0;

int choice;

//this loop runs until 7 is entered

do

{

cout << "MENU \n";

cout << "1. Add a new book\n";

cout << "2. Add a new user\n";

cout << "3. Borrow a book\n";

cout << "4. Return a book\n";

cout << "5. Search a book\n";

cout << "6. Displays users and number of books they have\n";

cout << "7. Exit\n";

cout << "Enter your choice : ";

cin >> choice;

switch (choice)

{

//Adds a new book in to the database and increase book count by 1

case 1:

cout << "Bookname : ";

//cin.ignore() is used to clean the buffer to take input

cin.ignore();

//cin.getline is used to take input with space. Enter key is used to finish taking input. 100 is size of input character

cin.getline(book_details[books_count].title, 100);

cout << "Bookcode : ";

cin >> book_details[books_count].code;

book_details[books_count].status = 1;

books_count++;

break;

case 2:

//adds a new user details in to database

cout << "Enter UserId : ";

cin >> tempId;

//flag variable to check whether user exists or not

flag = 1;

for (i = 0; i < 100; i++)

{

if (tempId == user_details[i].id)

{

cout << "UserId already taken\n";

flag = 0;

break;

}

}

//if user is not taken creating user id and increase user_count by 1

if (flag)

{

user_details[user_count].id = tempId;

cout << "Enter Username : ";

cin.ignore();

cin.getline(user_details[user_count].name, 100);

user_details[user_count].booksNumber = 0;

user_count++;

}

break;

case 3:

cout << "Please enter your userId : ";

cin >> tempId;

flag = 0;

//check whether user exists or not using flag variable

for (i = 0; i < 100; i++)

{

if (user_details[i].id == tempId)

{

flag = 1;

break;

}

}

//if user exists then print his name and check fof the requested book

if (flag)

{

cout << "Welcome " << user_details[i].name << "\n";

//if user has taken less than 5 books

if (user_details[i].booksNumber <= 5)

{

cout << "Please enter book code you want to borrow : ";

cin >> tempCode;

for (j = 0; j < 100; j++)

{

if (book_details[j].code == tempCode && book_details[j].status != 0)

{

book_details[j].status = 0;

user_details[i].booksNumber++;

flag = 0;

break;

}

}

if (flag)

{

cout << "Book not found or it has been currently borrowed\n";

}

else

{

cout << "thank you for borrowing a book\n";

}

}

else

{

cout << "Username incorrect ;";

}

}

break;

case 4:

// to return the book issued to a user

cout << "Please enter book code :";

cin >> tempCode;

flag = 0;

for (i = 0; i < 100; i++)

{

if (book_details[i].code == tempCode)

{

flag = 1;

break;

}

}

//once book is found then check for user id

if (flag)

{

if (book_details[i].status == 0)

{

cout << "Enter user id ";

cin >> tempId;

flag = 0;

for (j = 0; j < 100; j++)

if (user_details[j].id == tempId)

{

flag = 1;

break;

}

//if user and book details are found

if (flag)

{

user_details[j].booksNumber--;

book_details[i].status = 1;

}

else

{

cout << "User not found\n";

}

}

else

{

cout << "Book hasn't been borrowed\n";

}

}

else

{

cout << "Invalid code\n";

}

break;

case 5:

//Searching a book using bookcode

cout << "Enter book code : ";

cin >> tempCode;

cout << " Book ID Book Name Availability \n";

for (i = 0; i < 100; i++)

{

if (book_details[i].code == tempCode)

{

cout << book_details[i].code << "\t" << book_details[i].title << "\t" << book_details[i].status << "\n";

break;

}

}

break;

case 6:

//printing details of user using user_details structure

cout << "UserID UserName Books borrowed\n";

for (i = 0; i < user_count; i++)

{

cout << user_details[i].id << "\t" << user_details[i].name << "\t" << user_details[i].booksNumber << "\n";

}

cout << "\n";

break;

case 7:

cout << "ThankYou";

break;

//if user input doesnot match with any of the case then ask him to enter data again

default:

cout << "Please enter valid input \n";

}

} while (choice != 7);

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

using namespace std;

//structure to store library data
class BookDetails{

public:
struct library

{

int code;

char title[20];

int status;

};
library book_details[100];
};
//structure to store user data

class UserDetails:public BookDetails{

public:
struct users

{

int id;

char name[20];

int booksNumber;
};

users user_details[100];
};

int main()

{

UserDetails ob1;
UserDetails *ob;
ob=&ob1;
int tempId, i, flag = 0, j;

char tempName[20];

int tempCode;
//variable to hold 100 userdetails
//variable to hold 100 book details

int user_count = 0, books_count = 0;

int choice;

//this loop runs until 7 is entered

do

{

cout << "MENU \n";

cout << "1. Add a new book\n";

cout << "2. Add a new user\n";

cout << "3. Borrow a book\n";

cout << "4. Return a book\n";

cout << "5. Search a book\n";

cout << "6. Displays users and number of books they have\n";

cout << "7. Exit\n";

cout << "Enter your choice : ";

cin >> choice;

switch (choice)
{
//Adds a new book in to the database and increase book count by 1

case 1:

cout << "Bookname : ";

//cin.ignore() is used to clean the buffer to take input

cin.ignore();

//cin.getline is used to take input with space. Enter key is used to finish taking input. 100 is size of input character

cin.getline(ob->book_details[books_count].title,100);

cout << "Bookcode : ";

cin >> ob->book_details[books_count].code;

ob->book_details[books_count].status = 1;

books_count++;

break;

case 2:

//adds a new user details in to database

cout << "Enter UserId : ";

cin >> tempId;

//flag variable to check whether user exists or not

flag = 1;

for (i = 0; i < 100; i++)

{

if (tempId == ob->user_details[i].id)

{

cout << "UserId already taken\n";

flag = 0;

break;

}
}

//if user is not taken creating user id and increase user_count by 1

if (flag)

{

ob->user_details[user_count].id = tempId;

cout << "Enter Username : ";

cin.ignore();

cin.getline(ob->user_details[user_count].name, 100);

ob->user_details[user_count].booksNumber = 0;

user_count++;

}

break;

case 3:

cout << "Please enter your userId : ";

cin >> tempId;

flag = 0;

//check whether user exists or not using flag variable

for (i = 0; i < 100; i++)

{

if (ob->user_details[i].id == tempId)

{

flag = 1;

break;

}

}

//if user exists then print his name and check fof the requested book

if (flag)

{

cout << "Welcome " << ob->user_details[i].name << "\n";

//if user has taken less than 5 books

if (ob->user_details[i].booksNumber <= 5)

{

cout << "Please enter book code you want to borrow : ";

cin >> tempCode;

for (j = 0; j < 100; j++)

{

if (ob->book_details[j].code == tempCode && ob->book_details[j].status != 0)

{

ob->book_details[j].status = 0;

ob->user_details[i].booksNumber++;

flag = 0;

break;

}

}

if (flag)

{

cout << "Book not found or it has been currently borrowed\n";

}

else

{

cout << "thank you for borrowing a book\n";

}

}

else

{

cout << "Username incorrect ;";

}

}

break;

case 4:

// to return the book issued to a user

cout << "Please enter book code :";

cin >> tempCode;

flag = 0;

for (i = 0; i < 100; i++)

{

if (ob->book_details[i].code == tempCode)

{

flag = 1;

break;

}

}

//once book is found then check for user id

if (flag)

{

if (ob->book_details[i].status == 0)

{

cout << "Enter user id ";

cin >> tempId;

flag = 0;

for (j = 0; j < 100; j++)

if (ob->user_details[j].id == tempId)

{

flag = 1;

break;

}

//if user and book details are found

if (flag)

{

ob->user_details[j].booksNumber--;

ob->book_details[i].status = 1;

}

else

{

cout << "User not found\n";

}

}

else

{

cout << "Book hasn't been borrowed\n";

}

}

else

{

cout << "Invalid code\n";

}

break;

case 5:

//Searching a book using bookcode

cout << "Enter book code : ";

cin >> tempCode;

cout << " Book ID Book Name Availability \n";

for (i = 0; i < 100; i++)

{

if (ob->book_details[i].code == tempCode)

{

cout << ob->book_details[i].code << "\t" << ob->book_details[i].title << "\t" << ob->book_details[i].status << "\n";

break;

}

}

break;

case 6:

//printing details of user using user_details structure

cout << "UserID UserName Books borrowed\n";

for (i = 0; i < user_count; i++)

{

cout << ob->user_details[i].id << "\t" << ob->user_details[i].name << "\t" << ob->user_details[i].booksNumber << "\n";

}

cout << "\n";

break;

case 7:

cout << "ThankYou";

break;

//if user input doesnot match with any of the case then ask him to enter data again

default:

cout << "Please enter valid input \n";

}

} while (choice != 7);

return 0;

}

Add a comment
Know the answer?
Add Answer to:
IN C++ add Inheritance, Class and Pointers to program This is a library management program, users...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C++ Help to fix code Library management program. Error is when i type a letter...

    IN C++ Help to fix code Library management program. Error is when i type a letter into options menu the code breaks. after your edits, the code should still run as normal. CODE: #include<iostream> using namespace std; //structure to store library data class BookDetails{ public: struct library { int code; char title[20]; int status; }; library book_details[100]; }; //structure to store user data class UserDetails:public BookDetails{ public: struct users { int id; char name[20]; int booksNumber; }; users user_details[100]; };...

  • IN C++ My project will be a library management system. It will have seven functions. It...

    IN C++ My project will be a library management system. It will have seven functions. It HAS to contain: classes, structures, pointers and inheritance. fix code according to below 1. Add a new book allows the user to enter in book name and book code Book code has to be in # “Invalid book code” “book name has been added to library” book code will be used to borrow books 2. Add a new user must use first user must...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • please Code in c++ Create a new Library class. You will need both a header file...

    please Code in c++ Create a new Library class. You will need both a header file and a source file for this class. The class will contain two data members: an array of Book objects the current number of books in the array Since the book array is moving from the main.cc file, you will also move the constant array size definition (MAX_ARR_SIZE) into the Library header file. Write the following functions for the Library class: a constructor that initializes...

  • I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for.....

    I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for..while..do while,loops,pointer,address,continue,return,break. Create a C++ program which contain a function to ask user to enter user ID and password. The passwordmust contain at least 6 alphanumeric characters, 1 of the symbols !.@,#,$,%,^,&,* and 1 capital letter.The maximum allowable password is 20. Save the information. Test the program by entering the User ID and password. The...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • I created a struct for Car and now I need to turn it into a class...

    I created a struct for Car and now I need to turn it into a class for Car. Below is the code that I wrote for the structure. For the class, we are suppose to make the data in the class Car private, revise the input so the input function will only read the data from the user, and then it will call an additional function named setUpCar which will put the data into the object. Not sure how to...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT