-
please help
I need to search for a surname in an ordered list, when the user inputs the surname. I have tried using this code but it fails to work although it worked when I searched for files greater than or less than a certain number. I have used a switch statement depending on what the user inputs he can view a certain record etc, surname, address.....
please can you help me?
Will...
case 1 : printf ("\nPlease enter a surname: ");
scanf ("%s", &surtype);
while (element != NULL)
{
if (element->surname == surtype)
{
printf ("%s", element->surname,
}
else if (element->surname != surtype)
{
printf ("Sorry there is no records matching that surname\n");
}
element = element->next;
}
-
a few things:
you need to include a bit more code. (ie, the initialization of element, surname, etc...)
from what i see, i'm guessing you have forgetten to set the element pointer to the beginning of the linked list.
and some optional stuff:
1) use code tags. it makes code much more readable
2) you don't need brackets around one command code blocks. some will argue with me on this, and it's not really bad form to always include the brackets. (if you do, see #1)
3) if you're dealing with a case with a true or false value, it can be
Code:
if (this_pointer == NULL)
{
... }
else ....
that code is equal and more readable (imho) than this:
Code:
if (this_pointer == NULL)
{
... }
else if (this_pointer == NULL)
....
-
Try the standard function strcmp() to compare the surnames. Dont ask how to use it please.
-
Also, this
if (element->surname == surtype)
looks like it is supposed to be comparing strings. Use need to use strcmp() to compare strings, you cannot do it with the == operators.