Question

In: Computer Science

The code following is what I have so far. It does not meet my requirements. My...

The code following is what I have so far. It does not meet my requirements. My problem is that while this program runs, it doesn't let the user execute the functions of addBook, isInList or compareLists to add, check, or compare. Please assist in correcting this issue. Thank you!

Write a C++ program to implement a singly linked list of books. The book details should include the following: title, author, and ISBN. The program should include the following functions:

  • addBook: This is a function to add a new book to the list.
  • isInList: This is a function to check if a book exists in the list.
  • compareLists: This is a function to check whether two lists have the same books.

#include <iostream>
#include <string>

using namespace std;

struct Book {
string title;
string author;
string isbn;
Book(string t, string a, string is)
{
title = t;
author = a;
isbn = is;
}
Book* next;
};

class BookList {
private:
Book* head;

public:
BookList();
void addBook(Book* b);
bool isInList(string isbn);
bool compareLists(BookList& other);
};

BookList::BookList()
{
head = NULL;
}

void BookList::addBook(Book* b)
{
b->next = head;
head = b;
}

bool BookList::isInList(string isbn)
{
Book* temp = head;
while (temp != NULL) {
if (temp->isbn == isbn) {
return true;
}
temp = temp->next;
}
return false;
}

bool BookList::compareLists(BookList& other)
{
Book* temp = head;
while (temp != NULL) {
if (!other.isInList(temp->isbn)) {
return false;
}
temp = temp->next;
}
return true;
}

int main()
{
BookList list1, list2;
list1.addBook(new Book("title1", "author1", "isbn1"));
list1.addBook(new Book("title2", "author1", "isbn2"));
list1.addBook(new Book("title1", "author2", "isbn3"));
list1.addBook(new Book("title3", "author3", "isbn4"));

list2.addBook(new Book("title1", "author1", "isbn1"));
list2.addBook(new Book("title2", "author1", "isbn2"));
list2.addBook(new Book("title1", "author2", "isbn3"));
list2.addBook(new Book("title4", "author4", "isbn5"));

cout << "list1.isInList(\"isbn4\")" << list1.isInList("isbn4") << endl;
cout << "list1.isInList(\"isbn5\")" << list1.isInList("isbn5") << endl;
cout << "list1.isInList(\"isbn2\")" << list1.isInList("isbn2") << endl;

cout << "list1.compareLists(list2)" << list1.compareLists(list2) << endl;
return 0;
}

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.

Code:

#include <iostream>
#include <string>

using namespace std;

struct Book {
   string title;
   string author;
   string isbn;
   Book(string t, string a, string is)
   {
       title = t;
       author = a;
       isbn = is;
   }
   Book* next;
};

class BookList {
private:
   Book* head;

public:
   BookList();
   void addBook(Book* b);
   bool isInList(string isbn);
   bool compareLists(BookList& other);
   void printList();
};

BookList::BookList()
{
   head = NULL;
}

void BookList::addBook(Book* b)
{
   b->next = head;
   head = b;
}

bool BookList::isInList(string isbn)
{
   Book* temp = head;
   while (temp != NULL) {
       if (temp->isbn == isbn) {
           return true;
       }
       temp = temp->next;
   }
   return false;
}

void BookList::printList()
{
   Book* temp = head;
   while (temp != NULL) {
       cout<<"Author :"<<temp->author<<endl;
       cout<<"ISBN :"<<temp->isbn<<endl;
       cout<<"Title :"<<temp->title<<endl<<endl<<endl;
       temp = temp->next;
   }
}

bool BookList::compareLists(BookList& other)
{
   Book* temp = head;
   while (temp != NULL) {
       if (!other.isInList(temp->isbn)) {
           return false;
       }
       temp = temp->next;
   }
   return true;
}

int showMenu(){
   int choice;
   cout<<"\n\nWhat do you want to do?\n"<<endl;
   cout<<"1: Add a book in first list.\n";
   cout<<"2: Add a book in second list.\n";
   cout<<"3: Search if book exist in first list.\n";
   cout<<"4: Search if book exist in second list.\n";
   cout<<"5: Compare two lists.\n";
   cout<<"6: Print first list.\n";
   cout<<"7: Print second list.\n";
   cout<<"8: To exit\n";
   cin>>choice;
   cin.ignore();
   cin.clear();
   return choice;
}
Book* getBook(){
string author,isbn,title;
cout<<"Please give Title:";
getline(cin,title);
cout<<"Please give Author Name :";
getline(cin,author);
cout<<"Please give ISBN:";
getline(cin,isbn);
Book* obj=new Book(title, author, isbn);
return obj;
}

int main()
{
   BookList list1, list2;
   int choice=1;
   Book* book;
   string isbn;
   while(choice!=8){
       choice=showMenu();
       switch (choice)
       {
       case 1:
           book=getBook();
           list1.addBook(book);
           break;
       case 2:
           book=getBook();
           list2.addBook(book);
           break;
       case 3:
           cout<<"Please give isbn:\n";
           cin>>isbn;
           if(list1.isInList(isbn))
               cout<<"This ISBN exists in list 1.\n";
           else
               cout<<"This ISBN does not exists in list 1.\n";
           break;
       case 4:
           cout<<"Please give isbn:\n";
           cin>>isbn;
           if(list2.isInList(isbn))
               cout<<"This ISBN exists in list 2.\n";
           else
               cout<<"This ISBN does not exists in list 2.\n";
           break;
       case 5:
           if(list1.compareLists(list2))
               cout<<"Both lists are same.\n";
           else
               cout<<"Lists are different.\n";
           break;
       case 6:
           list1.printList();
           break;
       case 7:
           list2.printList();
           break;
       case 8:
           break;
       default:
           cout<<"Invalid choice!!!\n";
           break;
       }
   }
   /*\list1.addBook(new Book("title1", "author1", "isbn1"));
   list1.addBook(new Book("title2", "author1", "isbn2"));
   list1.addBook(new Book("title1", "author2", "isbn3"));
   list1.addBook(new Book("title3", "author3", "isbn4"));

   list2.addBook(new Book("title1", "author1", "isbn1"));
   list2.addBook(new Book("title2", "author1", "isbn2"));
   list2.addBook(new Book("title1", "author2", "isbn3"));
   list2.addBook(new Book("title4", "author4", "isbn5"));

   list1.printList();
   cout << "list1.isInList(\"isbn4\")" << list1.isInList("isbn4") << endl;
   cout << "list1.isInList(\"isbn5\")" << list1.isInList("isbn5") << endl;
   cout << "list1.isInList(\"isbn2\")" << list1.isInList("isbn2") << endl;

   cout << "list1.compareLists(list2)" << list1.compareLists(list2) << endl;*/
   system("pause");
   return 0;
}

Output:

What do you want to do? 1: Add a book in first list. 2: Add a book in second list. 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit Please give Title:Title1 Please give Author Name : Authori Please give ISBN: isbn1 What do you want to do? 1: Add a book in first list. 2: Add a book in second list. 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit Lists are different. What do you want to do? 1: Add a book in first list. 2: Add a book in second list. 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit

Please give Title:Title1 Please give Author Name : Authori Please give ISBN:isbn1 What do you want to do? 1: Add a book in first list. 2: Add a book in second list 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit Both lists are same. What do you want to do? 1: Add a book in first list. 2: Add a book in second list 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit Please give isbn: isbn1 This ISBN exists in list 1. What do you want to do? 1: Add a book in first list. 2: Add a book in second list 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list.

4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit Author : Author1 ISBN :isbn1 Title :Title1 What do you want to do? 1: Add a book in first list. 2: Add a book in second list. 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit Author : Authori ISBN :isbn1 Title :Title1 What do you want to do? 1: Add a book in first list. 2: Add a book in second list. 3: Search if book exist in first list. 4: Search if book exist in second list. 5: Compare two lists. 6: Print first list. 7: Print second list. 8: To exit


Related Solutions

How can I improve my code below to meet the requirements of this assignment with syntax...
How can I improve my code below to meet the requirements of this assignment with syntax add to my code to complete the assignment below: #include <iostream> #include <vector> using namespace std; class Inventory { private: int itemNumber; int quantity; double cost; double totalCost; public: Inventory() { itemNumber = 0; quantity = 0; cost = 0; totalCost = 0; } Inventory(int n, int q, double c) { itemNumber = n; quantity = q; cost = c; setTotalCost(); } void setItemNumber(int...
Here is what I have so far. I have created a code where a user can...
Here is what I have so far. I have created a code where a user can enter in their information and when they click submit all of the information is shown. How can I add a required field for the phone number without using an alert? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Login and Registeration Form Design</title> <link rel="stylesheet" type="text/css" href="signin.css"> <script> function myFunction(){ document.getElementById('demo').innerHTML = document.getElementById('fname').value + " " + document.getElementById('lname').value + " " + document.getElementById('street').value + " "...
How would I make it so that when I run my code it does not ask...
How would I make it so that when I run my code it does not ask for input (not having to enter after the statement and enter 0 for example) after ROXY (Forever ROXY Enterprises) appears? Like it would tell me the second statement right away along with the Roxy phrase. This is in C++. My code: #include / #include using std::endl; int main() {    void readAndConvert();    unsigned int stockSymbol;    unsigned int confNum;    std::cout << "ROXY...
This is what I have so far. The out print I am looking for is Date...
This is what I have so far. The out print I am looking for is Date is 7/22/2020 New month: 9 New day: 5 New Year: 2020 Date is 9/5/2020 #include    class Date {    public:        Date::Date(int dateMonth, int dateDay, int dateYear)        month{dateMonth}, day{dateDay}, int dateYear}{}               //void displayDate();         //set month        void Date::setMonth(std::int dateMonth){            month = dateMonth;        }        //retrieve month   ...
In Java please. I put down my code and what I was able to achieve so...
In Java please. I put down my code and what I was able to achieve so far: public class Animal {   private String gender; //stores the gender of the animal    private String type; //stores the type of the animal(bear of fish)    private int strength; //stores the strength of the animal    public Animal() {        gender = "none";        type = "none";        strength = 0;    }        public Animal (String g, String...
How to schematize a diagnostic argument? This is what i have so far not sure if...
How to schematize a diagnostic argument? This is what i have so far not sure if its right, need help with this one in order to figure out rest of homework. ----------- IQ: Is drinking milk healthy? (5 items of Support TD or NTD) S1: S2: S3: S4: S5: C1: More than half of the population in north America are lactose intolerant meaning they cannot digest milk properly. C2: Article: https://milk.procon.org/
I have solved most of question and the answers I have so far are correct, I...
I have solved most of question and the answers I have so far are correct, I just need help finding out the lower bound and upper bound (All answers were generated using 1,000 trials and native Excel functionality.) Galaxy Co. distributes wireless routers to Internet service providers. Galaxy procures each router for $75 from its supplier and sells each router for $125. Monthly demand for the router is a normal random variable with a mean of 100 units and a...
Here is my C++ program so far. Is there anyway I can make it display an...
Here is my C++ program so far. Is there anyway I can make it display an error if the user enters a float? Thanks #include <iostream> using namespace std; // Creating a constant for the number of integers in the array const int size = 10; int main() { // Assigning literals to the varibles int a[size]; int sum=0; float avg; // For loop that will reiterate until all 10 integers are entered by the user for(int i=0; i<size; i++)...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
MATLAB script Gaussian Elimination without pivoting, not working no matter what I try. So far my...
MATLAB script Gaussian Elimination without pivoting, not working no matter what I try. So far my script is : function [x] = myGauss( A, b ) n = size(A,1); % getting n Ab = [A,b];      % produces the augmented matrix x = zeros(n,1); % solution fprintf('Augmented matrix \n') %FORWARD ELIMINATION   for k=1:n-1     for i=k+1:n         lambda = A(i,k)/A(k,k);         for j=k+1:n             A(i,j) = A(i,j) - lambda*A(k,j);         end;         b(i) = b(i) - lambda*b(i)     end; end; %Backwards...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT