After I enter 'y' and the loop repeats, I get "Would you like to search by item or expiration date? Would you like to search again?"
It seems like it's ignoring the getline and if/else statements...why?? And how can I fix this?
Thanks in advance, everyone.
#include <iostream>
#include <cstddef>
#include <string>
using namespace std;
struct Node
{
string item;
string date;
Node *link;
};
typedef Node* NodePtr;
NodePtr search(NodePtr head, string target);
void head_insert(NodePtr& head, string an_item, string a_date);
void show_list(NodePtr& head);
int main()
{
NodePtr head = NULL;
head_insert(head, "Tea", "01/01/2010");
head_insert(head, "Jam", "09/12/2003");
head_insert(head, "Rolls", "08/10/2003");
cout << "List contains:" << endl;
show_list(head);
string target;
string search_option;
char answer;
do{
cout << "Would you like to search by item or expiration date?" << endl;
getline(cin, search_option);
if (search_option == "expiration date")
{cout << "Please enter your search criteria: " << endl;
cin >> target;
if (search(head, target))
{cout << target << " is on the list." << endl;}
else
{cout << target << " is not on the list." << endl;}
}
if (search_option == "item")
{cout << "Please enter your search criteria: " << endl;
cin >> target;
if (search(head, target))
{cout << target << " is on the list." << endl;}
else
{cout << target << " is not on the list." << endl;}
}
cout << "Would you like to search again?" << endl;
cin >> answer;
}while (answer == 'Y' || answer == 'y');
return 0;
}
NodePtr search(NodePtr head, string target)
{
// Point to the head node
NodePtr here = head;
// If the list is empty nothing to search
if (here == NULL)
{
return NULL;
}
// Search for the item
else
{
//while you have still items and you haven't found the target yet
while (here-> item != target && here->link != NULL)
here = here->link;
// Found the target, return the pointer at that location
if (here-> item == target)
return here;
// Search unsuccessful, return Null
else
return NULL;
}
}
void head_insert(NodePtr& head, string an_item, string a_date)
{
NodePtr temp_ptr;
temp_ptr = new Node;
temp_ptr-> item = an_item;
temp_ptr-> date = a_date;
temp_ptr->link = head;
head = temp_ptr;
}
void show_list(NodePtr& head)
{
NodePtr here = head;
while (here != NULL)
{
cout << here-> item << "\t";
cout << here-> date << endl;
here = here->link;
}
}
***ALSO: If I enter one of the expiration dates I have listed in my program, it still says that the date was not on the list. If anyone can give me a hint on how to fix this, I would be grateful. :)