I'm having a problem with my search function for my linked list. It has to do with my foodItem object. I'm not sure what to replace it with since it's part of the function protocol. The previous version of this code used an array and part of my problem is correctly converting to a linked list format. I'm really stuck, so any advice would be much appreciated. I hope I didn't leave out anything helpful. :confused: --Sheila

/**
*searchByName:search for foods by name
*in:name
*out:foodItem
*return:true if there is a match, or else false
**/
bool FoodList::searchByName(char name[],Food& foodItem)const
{
int i;
size = foodItem.size;
Node * current;
if(foodItem.head == NULL)
head = NULL;
else
{
for(current=head;current;current=current->next)
{
if(strcmp(head->data.name,name) == 0)
{
strcpy(head->data.name,foodItem.head->data.name);
head->data.category = foodItem.head->data.category;
head->data.calories = foodItem.head->data.calories;
head->data.carbohydrates = foodItem.head->data.carbohydrates;
head->data.fat = foodItem.head->data.fat;
head->data.cholesterol = foodItem.head->data.cholesterol;
head->data.sodium = foodItem.head->data.sodium;
head->data.protein = foodItem.head->data.protein;
return true;
}
else{
}
return false;
}

Recommended Answers

All 6 Replies

> if(strcmp(head->data.name,name) == 0) Hm, maybe you want to be comparing the name with the current node instead of 'head' each iteration?

Do you mean like this?

if(strcmp(current->data.name,name) == 0)

>Do you mean like this?
Well yeah, if searching through the linked list is your intention...

Another problem with your code: head is never assigned anything. There's no way your code is going to work unless you somehow give it a value.

[edit] Nevermind, I see now that 'head' is a class member, not a local variable. [/edit]

How come on this line:
bool FoodList::searchByName(char name[],Food& foodItem)const

you used the [] after the name and not these ?()
Joe head is asigned to a NULL. Does NULL mean is a empty
paramiter?

How come on this line:
bool FoodList::searchByName(char name[],Food& foodItem)const

you used the [] after the name and not these ?()

That means name is to be used as an array in the function.

Joe head is asigned to a NULL. Does NULL mean is a empty paramiter?

Yes.

Thanks did not know that.

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.