tags:

views:

265

answers:

3
+2  Q: 

Linked list in C

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... ???

+1  A: 

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;
};
wallyk
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().
Jonathan Sternberg
Almost just float price;
ScReYm0
A: 

This is how i search and find out that my "books" are missing:

void search_for_book(char book_catalog_number[STRMAX], BOOK *current)
{
    while(current->next != NULL)
    {
        if(strcmp(current->catalog_number, book_catalog_number) == NULL)
        {
            printf_s("\n\n\n");
            printf_s("%s, ", current->author);
            printf_s("%s, ", current->title);
            printf_s("%s, ", current->author);
            printf_s("%s, ", current->publisher);
            printf_s("%.2f, ", current->price);
            printf_s("%d\n\n\n", current->year_published);

        }
        break;
        current = current->next;
    }


}
ScReYm0
This is not an answer, this text belongs in the question. Please delete.
unwind
A: 

I found my mistake, my code was right buttt that BREAK; was braking my while and after move it on right place everything back to normal...

ScReYm0