Thread: please help

  1. #1
    Unregistered
    Guest

    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;
    }

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    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)
     ....

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    Try the standard function strcmp() to compare the surnames. Dont ask how to use it please.
    I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.

    Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed