Hey guys I'm finally understanding linked lists to an extent, but I ran into this problem. Everything works fine until I go to delete a node from the list.
Code:
#include <iostream> //for standard i/o
#include <cstddef> //for NULL
#include <iomanip> //for setprecision
#include <string> //for string
using namespace std;
struct Book
{
string author;
string title;
int date;
float price;
};
struct Node
{
Book data;
Node* next;
};
Node* head = NULL; //start of list is NULL
Node* prev;
Node* curr;
void PrintContents(Node* head);
void AddNode(Node* head);
void DeleteNode(Node* head, string targettitle);
int main()
{
int numBooks;
string targettitle;
head = new Node; //create first node in the list
cout << "How many books would you like to enter into the list?: ";
cin >> numBooks;
cout << endl;
cin.ignore(100, '\n');
system("cls"); //clear screen
//Enquire for head node
cout << "Please enter the author of a book (e.g. Stephen King): ";
getline(cin, head->data.author);
cin.ignore(100, '\n');
cout << "Please enter the title of " << head->data.author << "'s book: ";
getline(cin, head->data.title);
cin.ignore(100, '\n');
cout << "Please enter the date of " << head->data.title << "'s publication: ";
cin >> head->data.date;
cin.ignore(100, '\n');
cout << endl;
cout << "Please enter the price of " << head->data.title << " at print time (e.g. $25.00): $";
cin >> head->data.price;
cin.ignore(100, '\n');
head->next = NULL;
system("cls");
for(int i = 0; i < (numBooks - 1); i++)
{
Node* temp = head; //assign node to be created to the first node in the list
temp->next = new Node; //create Node at the start of the list
temp = temp->next;
//Enquire for subsequent nodes
cout << "Please enter the author of a book (e.g. Stephen King): ";
getline(cin, temp->data.author);
cin.ignore(100, '\n');
cout << "Please enter the title of " << temp->data.author << "'s book: ";
getline(cin, temp->data.title);
cin.ignore(100, '\n');
cout << "Please enter the date of " << temp->data.title << "'s publication: ";
cin >> temp->data.date;
cin.ignore(100, '\n');
cout << endl;
cout << "Please enter the price of " << temp->data.title << " at print time (e.g. $25.00): $";
cin >> temp->data.price;
cin.ignore(100, '\n');
temp->next = NULL;
cout << endl << endl;
system("cls");
}//end for
cout << "List as originally entered: " << endl << endl;
PrintContents(head);
system("pause");
system("cls");
AddNode(head);
cout << "List after a new node is added: " << endl << endl;
PrintContents(head);
system("pause");
system("cls");
cout << "Please enter the name of the title whose book you will delete: ";
cin >> targettitle;
DeleteNode(head, targettitle);
cout << "List after a node is deleted: " << endl << endl;
PrintContents(head);
system("pause");
system("cls");
delete head;
return 0;
}
void PrintContents(Node* head)
{
Node* temp = head;
cout << fixed << showpoint << setprecision(2);
cout << left << setw(20) << "Author" << setw(15) << "Title";
cout << setw(10) << "Date" << "Price" << endl;
cout << "--------------------------------------------------" << endl << endl;
while(temp != NULL)
{
cout << left << setw(20) << temp->data.author << setw(15) << temp->data.title;
cout << setw(10) << temp->data.date << "$" << temp->data.price << endl << endl;
temp = temp->next; //Traverse
}
cout << endl;
}
void AddNode(Node* head)
{
Node* temp = head;
Node* temp2;
Node* start_ptr = head;
temp = new Node;
cout << "Please add another book to the list" << endl << endl;
cout << "Please enter the author of a book (e.g. Stephen King): ";
getline(cin, temp->data.author);
cin.ignore(100, '\n');
cout << "Please enter the title of " << temp->data.author << "'s book: ";
getline(cin, temp->data.title);
cin.ignore(100, '\n');
cout << "Please enter the date of " << temp->data.title << "'s publication: ";
cin >> temp->data.date;
cin.ignore(100, '\n');
cout << endl;
cout << "Please enter the price of " << temp->data.title << " at print time (e.g. $25.00): $";
cin >> temp->data.price;
cin.ignore(100, '\n');
temp->next = NULL;
if (start_ptr == NULL)
start_ptr = temp;
else
{
temp2 = start_ptr;
while (temp2->next != NULL)
temp2 = temp2->next;
temp2->next = temp;
}
system("cls");
}
void DeleteNode(Node* head, string targettitle)
{
prev = NULL;
curr = head;
while((curr != NULL) && (curr->data.title != targettitle));
{
prev = curr;
curr = curr->next;
}
prev->next = curr->next;
delete curr;
}
All is good until I input the title of the book to be deleted and return -- I then get only the cursor at the next line. Is the problem with how I'm using getline()?
Thanks in advance for any help