Hello there, I was hoping you all could help me with some issues I'm having. It is a program that creates a linked list of Books and their prices. It's a very simple program, but I'm still really new with c++. The program successfully creates the list and is able to add nodes. It can display them and it can delete them.
I have noticed that it does have some issues with deleting nodes. It can't delete the first node without crashing the program. I'm not too sure how to use the debugging in MS Visual Studio so I'm not sure what causes the problem. However if you delete another node first then you can delete other nodes without any issues. I've tried a few rewrites to the code, but I can't seem to quite fix the problem.
The program also needs to be able to search for a book by the title. My search function seems to work fine for since word titles but if there are spaces it doesn't find the book. I've tried using different comparison commands, but I wasn't able to find one that worked.
Any help you could give me would be awesome, also if you have any pointers on how to make the code better written would be even better. Thanks in advance for any help.
#include <iostream>
#include <string.h>
usingnamespace std;
int i = 0;
struct Book {
char title[1024]; // Book name
int id; // Book id or ISBN number
float price; // Book price
struct Book* next;
} *COLLECTION = NULL;
//-- Forward Declaration --//
void menu();
void branching(char option);
void insertion();
void printall();
struct Book* search();
void deletion();
void quit(struct Book* HEAD);
int main()
{
char ch;
cout << "\n\nWelcome to CSE240: Bookstore\n";
do {
menu();
ch = tolower(getchar()); // read a char, convert to lower case
cin.ignore();
branching(ch);
} while (ch != 'q');
return 0;
}
void menu()
{
cout << "\nMenu Options\n";
cout << "------------------------------------------------------\n";
cout << "i: Insert a book\n";
cout << "d: Delete a book\n";
cout << "s: Search a book\n";
cout << "p: Review your list\n";
cout << "q: Quit\n";
cout << "\n\nPlease enter a choice (i, d, s, p, or q) ---> ";
}
void branching(char option)
{
switch(option)
{
case'i':
insertion();
break;
case'd':
deletion();
break;
case's':
search();
break;
case'p':
printall();
break;
case'q':
quit(COLLECTION);
COLLECTION = NULL;
break;
default:
cout << "\nError: Invalid Input. Please try again...";
break;
}
}
void insertion() //Creates a link and adds it to the beginning of linked list
{
struct Book *p;
p = (struct Book *) malloc(sizeof(struct Book)); //Checks memory usage
if( p == 0)
cout <<"out of memory\n";
cout <<"\nWhat is the name of book?\n";
cin.getline(p->title, 1024);
cout <<"\nWhat is the price?\n";
cin >>p->price;
cin.ignore();
p->id = i; //Sets id to unique incremented value
i++;
p->next = COLLECTION; //Adds to beginning of list
COLLECTION = p;
// add code to insert a new book into the COLLECTION linked list.
// HINT: You can insert a new book at the beginning of the linked list
}
struct Book* search() //Searches linked list for book based on title
{
char sname[30];
struct Book *p = COLLECTION, *b = p;
cout <<"\nPlease enter the name to be searched for:\n";
cin >> sname;
cin.ignore();
while(p != 0)
if((strcmpi(sname, p->title)) == 0){ //Compares string and returns previous reference
cout <<"ID:" <<p->id;
cout <<"\nPrice:" <<p->price;
return b->id;
}
else{
b = p;
p = p->next;
}
cout <<"\nThe name does not exist.\n"; //Returns 0 if not found
return 0;
// add code to search for an existing book in the COLLECTION linked-list
// HINT: If no book matches the tile return a error messag
}
void deletion() //Deletes link based on entered ID
{
int sID;
struct Book *p = COLLECTION, *b = p;
cout <<"\nPlease enter the ID to be deleted:\n";
cin >> sID;
cin.ignore();
while(p != 0) //Loops through list to compare ID's
if(b->next == NULL){ //Case: If link is the first and only link deletes and sets head to null
free(b);
b = NULL;
COLLECTION = NULL;
return;
}
elseif(sID == b->next->id){ //Case: Link found sets previous link to point to the link after the found link, then deletes
b = b->next;
p->next = p->next->next;
delete b;
return;
}
else{ //Case: not found moves to next link
b = p;
p = p->next;
}
cout <<"\nThat ID does not exitst.\n";
}
void printall() //Prints all entered links, with fixed output format of 2 decimal places
{
struct Book *p = COLLECTION;
cout.setf(std::ios::fixed);
cout.precision(2);
cout <<"\n{Book Id} : {Book Title} for {Book Price}.";
while(p != 0){ //Moves through list and prints info
cout <<"\n" <<p->id <<": " <<p->title <<" for $" <<p->price <<".";
p = p->next;
}
// Add code to print the book collection. (HINT: You will need to use a loop.)
}
void quit(struct Book* HEAD) //Deletes linked list before closing
{
if(HEAD == NULL)
return;
else{
quit(HEAD->next);
delete HEAD;
}
// Add code to delete the objects/books from the lniked-list.
// HINT: Refer to the slides in the homework assignmetn
}