Search sub function for a linked list
May 15, 2015 at 2:14am UTC
Good day. :)
I need help with the snippet of my code. My search sub function is not working and I haven't got the slightest to fix it.
The problem is. When I enter more than two records in my linked list, the search sub function will only recognize the first one I entered, and will display "Invalid Input" if I try to search the other records. I included the other sub functions just in case the problem is not in the search sub. Thank you :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
void search()
{
char a[size];
system("cls" );
cout << "Enter the Item Code: " << endl;
cin.ignore();
cin.getline(a, size);
p = front;
if (p != NULL)
{
if ((strcmp(a, p->icode)) == 0)
{
cout << "Name: " << p->name << endl << "Manufacter: " << p->manufacturer << endl << "Price: " << p->price << endl << "Item Code: " << p->icode << endl << "Barcode: " << p->barcode << endl << "Color: " << p->color << endl << "Category: " << p->categ << endl << "Stock: " << p->stock << endl << "Purchase Type: " << p->purchase << endl << "Store Availability: " << p->store << endl;
p = p->next;
}
else
{
cout << "Invalid Input\n" ;
}
}
system("pause" );
}
void add()
{
getData();
if (front == NULL)
{
front = p;
}
else
{
rear->next = p;
}
rear = p;
}
void getData()
{
system("cls" );
p = new Node;
cin.ignore();
cout << "Name: " ; cin.getline(p->name, size);
cin.ignore();
cout << "Manufacturer: " ; cin.getline(p->manufacturer, size);
cin.ignore();
cout << "Price: " ; cin.getline(p->price, size);
cin.ignore();
cout << "Item Code: " ; cin.getline(p->icode, size);
cin.ignore();
cout << "Barcode: " ; cin.getline(p->barcode, size);
cin.ignore();
cout << "Color: " ; cin.getline(p->color, size);
cin.ignore();
cout << "Category: " ; cin.getline(p->categ, size);
cin.ignore();
cout << "Stock: " ; cin.getline(p->stock, size);
cin.ignore();
cout << "Purchase: " ; cin.getline(p->purchase, size);
cin.ignore();
cout << "Store: " ; cin.getline(p->store, size);
cin.ignore();
p->next = NULL;
system("pause" );
}
May 15, 2015 at 3:27am UTC
Your `search()' function does not contain any loop, it only checks the node pointed by `front'
May 15, 2015 at 6:42am UTC
Okay then. I removed the "p = front". But now, the first record I inputted isn't read. What can I do to fix this?
May 15, 2015 at 9:04pm UTC
loop
Topic archived. No new replies allowed.