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. :)

Recommended Answers

All 2 Replies

I think this has to do with a disconnect between cin/cout token and line reads. As I understand it, when you cout<<"Text"; the console gets "Text" put into it and the cin read point goes to 5 (the position after "Text") but getline doesnt use this read point, so it reads starting at "Text". As such if after that you type "test", getline will fill your string with "Texttest" which is not what you want. I would suggest testing this by turning your if (str==option1){stuff} if (str==option2){stuff} into if (str==option1){stuff}else if (str==option2){stuff}else{output some error message}

Hope this helps.

The >> operator leaves the newline in the buffer. You need to get rid of that before you can use getline() since getline() stops reading once it sees a newline. Check out this guide that Narue created. It should tell you how to fix your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.