View the step-by-step solution to:

Question

I need help debugging this code in Visual Studio 2019.... it has a few errors.


#include

<iostream>

#include <string>

#include <ctime>

#include <vector>


using namespace std;


typedef struct book

{

  string author;

  string title;

  string publisher;

  string description;

long isbn;

short year;

}

book;


struct Node 

 book data; 

 struct Node *next; 

}; 


void push(struct Node** head_ref, book new_data) 

  struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); 

  new_node->data = new_data; 

  new_node->next = (*head_ref); 

  (*head_ref)  = new_node; 

}  


string tolower(string s){

string ls = "";

for(int i=0; i<s.size(); i++){

if(s[i]>='A' && s[i]<='Z')

ls += s[i]-'A'+'a';

else

ls += s[i];

}

return ls;

}


int strGtEq(string s1, string s2)

{

if(tolower(s1).compare(tolower(s2))>=0)

return 1;

return 0;

}


void sortedInsert(struct Node** head_ref, struct Node* new_node) 

  struct Node* current; 

  if (*head_ref == NULL || strGtEq((*head_ref)->data.title , new_node->data.title)) 

  { 

    new_node->next = *head_ref; 

    *head_ref = new_node; 

  } 

  else

  { 

    current = *head_ref; 

    while (current->next!=NULL && 

        !strGtEq(current->next->data.title , new_node->data.title)) 

    { 

      current = current->next; 

    } 

    new_node->next = current->next; 

    current->next = new_node; 

  } 


void insertionSort(struct Node **head_ref) 

  struct Node *sorted = NULL; 

  

  struct Node *current = *head_ref; 

  while (current != NULL) 

  { 

    struct Node *next = current->next; 

  sortedInsert(&sorted, current); 

    current = next; 

  } 

  

  *head_ref = sorted; 


int deleteBook(struct Node **head_ref, int key)

  struct Node* temp = *head_ref, *prev; 

  

  if (temp != NULL && temp->data.isbn == key) 

  { 

    *head_ref = temp->next;  

    free(temp);        

    return 1; 

  } 

  

  while (temp != NULL && temp->data.isbn != key) 

  { 

    prev = temp; 

    temp = temp->next; 

  } 

  

  if (temp == NULL) return 0; 

  

  prev->next = temp->next; 

  free(temp); 

  return 1;


book createBook(){

book b;


cout << "Enter author's name : ";

getline( cin, b.author );


cout << "Enter title : ";

getline( cin, b.title );


cout << "Enter publisher : ";

getline( cin, b.publisher );


cout << "Enter description : ";

getline( cin, b.description );


cout << "Enter ISBN number : ";

cin >> b.isbn;


cout << "Enter year : ";

cin >> b.year;

cin.ignore();


return b;

}


void showAll(struct Node *head) 

  struct Node *temp = head; 

  int counter = 1;

  while(temp != NULL) 

  { 

    cout << counter++ << " " << temp->data.title << endl;

    temp = temp->next; 

  } 



void display(book b){

cout << "--------------------------n";

cout << "Author : " << b.author << endl;

cout << "Title : " << b.title << endl;

cout << "Publisher : " << b.publisher << endl;

cout << "Description : "<< b.description << endl;

cout << "ISBN : " << b.isbn << endl;

cout << "Year : " << b.year << endl;

cout << "--------------------------n";

}


void welcome(){

time_t now = time(0);

char *dt = ctime(&now);

cout << "--------------------------n";

cout << "--------------------------n";

cout << "WELCOME to Library Service" << endl;

cout << dt;

cout << "--------------------------n";

}


int menu(){

int option;


cout << "--------------------------n";

cout << "MENUn" ;

cout << "1 - Add new bookn";

cout << "2 - Remove bookn";

cout << "3 - Remove Alln";

cout << "4 - Show Alln";

cout << "5 - Sortn";

cout << "6 - Quitn";

cout << "Your option : ";

cin >> option;

cin.ignore();

cout << "--------------------------n";

if(option<1 || option>6 )

return -1;

return option;

}



int main(){


Node* books = NULL;

welcome();


while(1){

int option = menu();

if(option == -1){

cout << "Incorrect input, please try againn";

continue;

}

if(option == 1){

book new_book = createBook();

push(&books, new_book); 

}

if(option == 2){

long ISBN;

cout << "Enter ISBN of book to be deleted : ";

cin >> ISBN;

cin.ignore();

if(deleteBook(&books,ISBN))

cout << ">> Book deleted succefullyn";

else

cout << ">> Book not found, delete unsuccessfulln";  

}

if(option == 3){

books = NULL;

}

if(option == 4){

showAll(books);

}

if(option == 5){

insertionSort(&books);

if(option == 6){

cout << "Thank you for using the softwaren";

break;

}

}


return 0;

}

Top Answer

in the main routine you... View the full answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question