Hi everyone,

I wrote something like that:

book *library = NULL;
    customer *service = NULL;
        printf("* #1 List of books      *\n");
        printf("* #2 Add book           *\n");
        printf("* #3 Search by title    *\n");
        printf("* #4 Search by category *\n");
        printf("* #5 Search by author   *\n");
        printf("* #6 Borrow book        *\n");
        printf("* #7 Dump library       *\n");
        printf("* #8 Exit               *\n");
        printf(">: ");
        scanf("%d", &choice);

            case 1:
                countBooks(library) > 0 ? showBooks(library) : printf("##########\nLibrary is empty\n##########\n");
            }   break;
            case 2:
                library = addFirstBook(2, 13, 124094712, "John", "Bean", "How to be feeling green?", "2001-11-23", "Bean Company");
            }   break;
            case 3: printf("3");        break;
            case 4: printf("4");        break;
            case 5: printf("5");        break;
            case 6: printf("6");        break;
            case 7: printf("7");        break;

    while(choice != 8);

Everything is fine if I don't use scanf(). This code is working properly:

short choice;
    book *library = NULL;
    customer *service = NULL;
        library = addFirstBook(2, 13, 124094712, "John", "Bean", "How to be feeling green?", "2001-11-23", "Bean Company");

    library = addBook(library, 3, 93, 363012991, "Keith", "Alison", "Alone in the dark", "2010-06-14", "Glory House");
    library = addBook(library, 2, 1, 152402833, "Jenny", "Richardson", "Alone in the light", "2010-07-14", "Rights");
    library = addBook(library, 4, 37, 849232157, "Tom", "Simon", "Cats and rats", "2009-03-09", "New World");
    library = addBook(library, 1, 14, 987561270, "Keith", "Alison", "Defeat yourself!", "2011-03-25", "Glory House");
    library = addBook(library, 1, 104, 781001381, "Mark", "Gimbon", "Stronghold", "2008-04-11", "Bookhouse");
    printf("\n\nItems: %d", countBooks(library));

showBooks function:

void showBooks(book *header)
        printf("ISBN: %d | Catalog: %d\n", header->isbn, header->catalog);
        printf("Title: %s\nAuthor: %s %s\n", header->title, header->authorSurname, header->authorName);
        printf("Category: %s\n", categoryName(header->category));
        printf("Publisher: %s\nPublished: %s\n", header->publisher, header->published);
        printf("Avail.: %d of %d\n", header->free, header->amount);
        header = header->next;

Implementation with do ... while loop and scanf() is bad becouse program has failure during printing showBooks(). I don't know what happened and I'm sure that reason is scanf(). This is single linked list.

How to solve this?

Recommended Answers

All 2 Replies

I don't know what happened and I'm sure that reason is scanf().

Then let's start by using scanf() correctly. When your choice variable is a short int, the correct format specifier is %hd . Fix that and return if the problem persists.

Love you, problem solved. My horrible mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.