0

i can't understand why in the 7-6 line (for the end) when i input the same book name it doesn't get into the "if". i wrote the printf line above it to check if the print is any different but it is'nt. it's printed the same stuff for both a.name and bookz[r].name.

thanks!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#define BOOK_NUM        50
#define NAME_LENGTH     200
#define AUTHOR_NAME_LENGTH  100
#define PUBLISHER_NAME_LENGTH   50
#define GENRE_LENGTH        50

typedef struct book {
    char name[NAME_LENGTH];
    char author[AUTHOR_NAME_LENGTH];
    char publisher[PUBLISHER_NAME_LENGTH];
    char genre[GENRE_LENGTH];
    int year;
    int num_pages;
    int copies;
} Book;

int main()
{
    int opt = 1;
    int i = 0, r = 0, k = 0, years, pagess, copiess, copies_to_add;
    Book bookz[50];
    int zeros[5] = { 0, 0, 0 ,0 ,0 };
    Book a;
    char c;
        // Add book to library
        printf("Please enter book name: ");
        scanf("\n%[^\n]s", &a.name);
        for (i = 0; i < BOOK_NUM; i++)
        {
            if (zeros[i] == 0)
            {
                scanf("\n%[^\n]s", &a.author);
                scanf("\n%[^\n]s", &a.publisher);
                printf("Please enter book genre: ");
                scanf("\n%[^\n]s", &a.genre);
                scanf("%d", &a.year);
                scanf("%d", &a.num_pages);
                scanf("%d", &a.copies);
                k = i;
                bookz[k] = a;
                zeros[i] = 1;
                printf("The book %s was successfully added!", a.name);
                break;
            }
            else
            {
                for (r = 0; r < i+1; r++)
                {
                    printf("%s %s", bookz[r].name, a.name);
                    if (bookz[r].name == a.name)
                    {
                        printf("The book already exists. Please enter the number of copies to add:");
                        scanf("%d, &copies_to_add");
                        bookz[i].copies = bookz[i].copies + copies_to_add;
                        printf("Additional copies (%d) of book %s were successfully added!", copies_to_add, a.name);
                        break;
                    }
                }

            }
        }
din ohayon
  • 21
  • 1
  • 2
  • 2
    Possible duplicate of [How do I properly compare strings?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings) – Weather Vane May 21 '18 at 17:39
  • Possible duplicate of [How do I properly compare strings?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings) – Gerhardh May 21 '18 at 20:25
  • You have BOOK_NUM (aka 50), but you don't use it defining ``bookz`` (but you should), and you don't use it in defining `zeros` and you probably should since you use a number ranging over 0..49 to index into `zeros` which only ranges over 0..4. Be consistent with using array sizes. (There are also problems with string comparison — use `strcmp()` there!) – Jonathan Leffler May 21 '18 at 20:45

1 Answers1

1

String comparison in C is best achieved using strcmp().

Cowbolt
  • 158
  • 1
  • 10
  • @dinohayon `strcmp` does not return true or false, but a comparison result that informs three possibilities: less than, same, greater than. – Weather Vane May 21 '18 at 17:42
  • The break statement on line 48 breaks out of the for loop. You add a single book, then you exit the program. – Cowbolt May 21 '18 at 17:43