2

I am still new at lists in C and i got a big problem... First i wanna show you my code for inserting item to the list:

void input_books_info(int number_of_books, BOOK *current)
{
    int i;
    for(i = 0; i < number_of_books; i++)
    {
        while(current->next != NULL)
            current = current->next;


        current->next = (BOOK *)malloc(sizeof(BOOK));

        printf_s("%d book catalog number: ", i + 1);
        scanf_s("%s", &current->next->catalog_number , 20);
        printf_s("%d book title: ", i + 1);
        scanf_s("%s", current->next->title ,80);
        printf_s("%d book author: ", i + 1);
        scanf_s("%s", current->next->author ,40);
        printf_s("%d book publisher: ", i+1);
        scanf_s("%s", current->next->publisher,80);
        printf_s("%d book price: ", i + 1);
        scanf_s("%f", &current->next->price, 5);
        printf_s("%d book year published: ", i + 1);
        scanf_s("%d", &current->next->year_published, 5);

        current->next->next = NULL;
        printf_s("\n\n");
    }



}

And this is my main function:

void main (void)
{
    int number_of_books, t = 1;
    char book_catalog_number[STRMAX];
    char book_title[STRMAX];
    char book_author[STRMAX];
    char reading_file[STRMAX];
    char saving_file[STRMAX];


    first = malloc(sizeof(BOOK));
    first->next = NULL;
    /*
    printf_s("Enter file name: "); gets(saving_file);
    first->next = book_open(first, saving_file);
    */
    while(t)
    {
        char m;
        printf_s("1. Input \n0. Exit \n\n");
        printf_s("Choose operation: ");
        m = getch();





        switch(m)
        {
            case '1':
                printf_s("\ninput number of books: ");
                scanf_s("%d", &number_of_books);
                input_books_info(number_of_books, first);
                printf_s("\n");
            break;


            default:
                printf_s("\nNo entry found!\n\n\n\n\n");
                break;
        }

    }

}

and last maybe here is the problem the printing function:

void print_books_info(BOOK *current)
{
    while(current->next != NULL && current != NULL)
    {

        printf_s("%s, ", current->next->catalog_number);
        printf_s("%s, ", current->author);
        printf_s("%s, ", current->next->title);
        printf_s("%s, ", current->next->author);
        printf_s("%s, ", current->next->publisher);
        printf_s("%.2f, ", current->next->price);
        printf_s("%d", current->next->year_published);
        printf_s("\n\n");
        current = current->next;
    }

}

And my problem is that, when i run the app, program is moving good. But when I start the app, the program is storing only first input of data second and third are lost ... Can you help me to figure out it... ???

3
  • 1
    In print_books_info, you should check current before current->next in the while condition. Commented May 17, 2010 at 1:17
  • 1
    Can you post the struct you're using for BOOK? Commented May 17, 2010 at 1:45
  • 1
    How does the problem manifest itself? In other words, how do you know that the second and third books are "missing"? And, what does "missing" mean? Can you show us how you're calling print_books_info? Commented May 17, 2010 at 2:15

1 Answer 1

1

I suspect it is the scanf_s which is reading more fields than intended. Instead, use a single scanf_s to read the whole line into a buffer, then use sscanf_s to parse up the buffer.

Also, since you don't show the declaration of BOOK, we're all shooting a little blindly, but are the elements in book properly dimensioned? If not, there would be element overwriting during read. BOOK should look something like this:

class BOOK {
        BOOK   *next;
        char   catalog_number [20];  // at least 20 chars
        char   title [80];
        char   author [40];
        char   publisher [80];
        double price;
        int    year_published;
};
Sign up to request clarification or add additional context in comments.

1 Comment

I can confirm it's the scanf. Your code works fine if I remove all the scanf's and replace them with cin (C++ only). I also suggest using getchar() instead of getch().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.