-1

The purpose of the code is to store library card information and search for books using author's last name. When I search using author's name, it says "no books by this author last name found". Below is my code. What am I doing wrong here?

//Program to store Library card Information and search for record using last name
#include<stdio.h>

void search(struct lib_card *name)

struct author
{
    char fname[20];
    char lname[20];
};

struct pub_date
{
    int day;
    int month;
    int year;
};

struct lib_card //Structure declaration for storing Library card information
{
    char title[80];
    struct author name;
    struct pub_date date;
};

void main()
{
    struct lib_card card[2];
    int l; //l is a variable assigned for loop
    for (l = 0; l < 2; l++)
    {
        printf("********Enter Library Card Information********\n");
        printf("Card # %d\n", l + 1);
        printf("Enter Book Title:             ");
        scanf("\n%[^\n]s", card[l].title);

        printf("Enter Author's First name:  ");
        scanf("\n%s", card[l].name.fname);

        printf("Enter Author's Last name:   ");
        scanf("\n%[^\n]s", card[l].name.lname);

        printf("\nEnter Published date:\n");
        printf("\t\tdate(dd):");
        scanf("\n%d", &card[l].date.day);

        printf("\t\tMonth(mm):");
        scanf("\n%d", &card[l].date.month);

        printf("\t\t\Year(yy):");
        scanf("\n%d", &card[l].date.year);
    }

    search(card); //Structure's address is passed to search function
}

void search(struct lib_card *name)
{
    char llastname[20];
    int l;

    printf("\nEnter Author's Last name to search for books:");
    scanf("\n%[^\n]s", llastname); //Get author's last name to search the books

    for (l = 0; l < 2; l++)
    {
        printf("%s\n", (name + l)->name.lname);
        if ((name + l)->name.lname == llastname)
        {
            printf("%s\n", (name + l)->title);
        }
        else if (l == 1)
        {
            printf("\nNo books by this Author's Last name found");
        }
    }
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
Kiran C K
  • 61
  • 7
  • 1
    Please provide the errors that you are getting compiling or running your code. – ojblass Aug 12 '15 at 17:32
  • 1
    Have you tried to use debugger or some debug printing to find error? – user996142 Aug 12 '15 at 17:32
  • 1
    1) fix your code formatting so it can actually be read without turning one's brain into a pretzel, 2) use `strcmp()` or something similar to compare NULL-terminated C strings, not `==`. – twalberg Aug 12 '15 at 17:50
  • I started to edit your code, but I don't have time. First off, it needs to be formatted as code: highlight the code and click the `{}` icon (this indents it by 4 columns). Use consistent indentation, and don't double-space it; blank lines are good for showing overall structure, but you don't need a blank line after every line of code. `void main()` should be `int main(void)`. And you need a tag to specify what language you're using (I'll add that). – Keith Thompson Aug 12 '15 at 18:10
  • possible duplicate of [How do I properly compare strings in C?](http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – Marcelo Camargo Aug 12 '15 at 19:46
  • `l` is a poor name for a variable; it looks too much like the digit `1`. – Keith Thompson Aug 13 '15 at 18:22

1 Answers1

0

This is not the proper way to compare strings:

if ((name + l)->name.lname == llastname)

Use strcmp instead

abelenky
  • 63,815
  • 23
  • 109
  • 159