View the step-by-step solution to:

Question

C++ Programming... I need help debugging and running the below code while using Visual Studios 2019... it has 2

errors



#include <iostream>

#include <fstream>

#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 - Search by Authorn";

cout << "7 - Search by Titlen";

cout << "8 - Search by ISBNn";

cout << "9 - Quitn";

cout << "Your option : ";

cin >> option;

cin.ignore();

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

if(option<1 || option>9 )

return -1;

return option;

}


int searchByAuthor(struct Node *head, string author) 

  struct Node *temp = head; 

  int counter = 0;

  while(temp != NULL) 

  { 

    if( temp->data.author.compare(author) == 0 ){

     display(temp->data);

     counter++;

    }

    temp = temp->next; 

  }

  return counter; 


int searchByTitle(struct Node *head, string title) 

  struct Node *temp = head; 

  int counter = 0;

  while(temp != NULL) 

  { 

    if( temp->data.title.compare(title) == 0 ){

     display(temp->data);

     counter++;

    }

    temp = temp->next; 

  }

  return counter; 


int searchByISBN(struct Node *head, long ISBN) 

  struct Node *temp = head; 

  int counter = 0;

  while(temp != NULL) 

  { 

    if( temp->data.isbn == ISBN ){

     display(temp->data);

     counter++;

    }

    temp = temp->next; 

  }

  return counter; 


void readFile(struct Node** head_ref, string filename){

ifstream file(filename);

string str;

int counter = 1;

book new_book;

while(getline(file,str)){

if(counter==1)

new_book.author = str;

if(counter==2)

new_book.title = str;

if(counter==3)

new_book.publisher = str;

if(counter==4)

new_book.description = str;

if(counter==5)

new_book.isbn = stol(str, nullptr, 10);

if(counter==6){

new_book.year = stoi(str);

counter = 0;

push(head_ref, new_book);

}

counter++;

}

}


void writeToFile(struct Node *head, string filename){

ofstream file;

file.open(filename);

struct Node *temp = head;

  while(temp != NULL) 

  { 

    file << temp->data.author << "n";

    file << temp->data.title << "n";

    file << temp->data.publisher << "n";

    file << temp->data.description << "n";

    file << temp->data.isbn << "n"; 

    file << temp->data.year << "n"; 

    temp = temp->next; 

  }

  file.close();

}


int main(){


Node* books = NULL;

readFile(&books, "data.txt");

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); 

writeToFile(books, "data.txt");

}

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";

writeToFile(books, "data.txt");

}

else

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

}

if(option == 3){

books = NULL;

}

if(option == 4){

showAll(books);

}

if(option == 5){

insertionSort(&books);

writeToFile(books, "data.txt");

}

if(option == 6){

string author;

cout << "Enter author name to be searched : ";

getline( cin, author );

int found = searchByAuthor(books, author);

cout << ">> " << found << " books foundn";

}

if(option == 7){

string title;

cout << "Enter title to be searched : ";

getline( cin, title );

int found = searchByTitle(books, title);

cout << ">> " << found << " books foundn";

}

if(option == 8){

long ISBN;

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

cin >> ISBN;

cin.ignore();

int found = searchByISBN(books, ISBN);

cout << ">> " << found << " books foundn";

}  

if(option == 9){

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

break;

}

}


return 0;

}

Top Answer

I have worked out the... View the full answer

Untitled115.png

Ci\Users'\Pranesh\documents\visual studio 2013\Projects\ ConsoleApplication62\Debug\ ConsoleApplication62.exe
WELCOME to Library Service
Tue Jan 07 09:13:04 2020
MENU
-
Add new book
-
Remove book...

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