lundi 21 mai 2018

comparing strings doesnt work

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

            }
        }

Aucun commentaire:

Enregistrer un commentaire