0

Hi I was trying to implement a library system using C. I defined an abstract type of data called Book with the following fields:

typedef char StringBook     [MAX_LEN];
typedef char ISBN           [ISBN_LEN];

typedef struct {
    StringBook  title;          /// Title of the book
    StringBook  author;         /// Author
    StringBook  editorial;      /// Editorial
    ISBN        isbn;           /// ISBN
    int         date;           /// Publication year
    int         num;            /// Number of copies
}Book;

I defined several functions to set and get the fields of the structure.

In addition in the main function I defined a function, that creates a new book with the data introduced by the keyboard.

void

 introduce_book_details(Book* book){
    StringBook string;
    ISBN isbn;
    int date;
    fprintf(stdout, "\nIntroduce the book details: ");
    fprintf(stdout, "\n\nTitle: ");
    fgets(string, MAX_LEN, stdin);
    delete_char(string);
    fflush(stdin);
    set_title(book, string);
    fprintf(stdout, "Author: ");
    fgets(string, MAX_LEN, stdin);
    delete_char(string);
    fflush(stdin);
    set_author(book, string);
    fprintf(stdout, "Editorial: ");
    fgets(string, MAX_LEN, stdin);
    delete_char(string);
    fflush(stdin);
    set_editorial(book, string);
    fprintf(stdout, "ISBN: ");
    fgets(isbn, MAX_LEN, stdin);
    delete_char(isbn);
    set_isbn(book, isbn);
    fprintf(stdout, "Date: ");
    fscanf(stdin, "%d", &date);
    fflush(stdin);
    set_date(book, date);
    set_num(book, 1);
}

So far so good. In the main function I create a book and call introduce_book_details and works. The problem comes if I use the function fscanf before. For instance, I want to create a menu with several options from 1 to 8. The option is introduced by the keyboard, and if it is 1, then the function introduce_book_details is called. However if I introduce this in the main function as:

int main() {
    Book book;
    int option;

    fprintf(stdout, "Introduce the option: ");
    fscanf(stdin, "%d", &option);
    fflush(stdin);

    if(option == 1) {
       introduce_book_details(&book);
       print_book(book);
    }
    return 0;
}

Then the console shows this:

Introduce the option: 1
Introduce the book details: 
Title: Author: 

Meaning that the I am not able to introduce the title and the first field I can introduce is the author (being title, NULL). If in the main function I remove the lines to introduce an option by keyboard the program works good. So I believe this has something to do with the fscanf or fflush functions. Any tip or help is welcome. Thanks beforehand.

wogsland
  • 8,299
  • 18
  • 54
  • 83
ggaliero
  • 9
  • 2
  • 1
    What operating system? What C compiler? What C standard library? Did you *carefully* read the documentation of `fgets` & of `fscanf` ? Which one did you read? Did you debug your code using a debugger? Why don't you test against failure (e.g. of `fscanf`)? – Basile Starynkevitch May 31 '16 at 16:28
  • 4
    Don't `fflush(stdin)`. It's behavior is undefined. – Eugene Sh. May 31 '16 at 16:31
  • The operating system is ubuntu 14.04 lts. I am using eclipse Luna as IDE. I checked the documentation of both fgets and fscanf but could not finde any relevant description. I used www.tutorialspoint.com. Eventually I did not try a debugger. Thanks for the tips I will try what you said! – ggaliero May 31 '16 at 16:34
  • " I checked the documentation of both fgets and fscanf but could not finde any relevant description" - Sorry, but this is unbelievable. Ubunut provides comprehensive man-pages for both and any simple online-search will show the required information. – too honest for this site May 31 '16 at 16:36
  • @user3121023 I think thins could be worth an answer – Ingo Leonhardt May 31 '16 at 16:41
  • I tested your code (both linux and windows platform) and it works fine. However I would avoid the ```fflush(stdin)``` statements. – kaspersky May 31 '16 at 16:44
  • 1
    what does delete_char do? – pm100 May 31 '16 at 17:45
  • delete_char looks for the char '\n' and replaces it by '\0'. If '\n' is not within the char array, it does nothing. – ggaliero Jun 10 '16 at 13:40

0 Answers0