Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,086
    Rep Power
    2222
    Nope, not according to the code listing you gave us:
    Code:
        enum{ARRAY_SIZE=2};
        Book *array[ARRAY_SIZE];
        
        //(?)array[0]=malloc(sizeof(Book));
        //(?)array[1]=malloc(sizeof(Book));
        
        int i;
        for(i=0;i<ARRAY_SIZE;++i)
        {
            printf("\n\nEnter details of book number %d--book name, author name, name of publisher, year of publishing, cost of book and serial number- all seperated by newlines: \n\n", i+1);
            input_book(array[i]);
        }
    array contains two uninitialized pointers. You need to perform those two mallocs you have commented out. Place them after the int i; so that it will compile.
  2. #17
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,086
    Rep Power
    2222
    Hmm, no feedback on whether that solved the problem.

    Here is why I was so insistent on the exact input you were giving your program. Correcting that one scanf we got the warning on, here's the code you gave us for input_book():
    Code:
    void input_book(Book *pbook)
    {
         char c;
         String temp;
         scanf("%s", temp);
         scanf("%c", &c);  /*For the newline character */
         pbook->book_name=strdup(temp);
         scanf("%s", temp);
         pbook->author_name=strdup(temp);
         scanf("%s", temp);
         pbook->name_of_publisher=strdup(temp);
         scanf("%d", &pbook->year_of_publishing);
         scanf("%c", &c); /*For the newline character */
         scanf("%f", &pbook->cost);
         scanf("%c", &c); /*For the newline character */
         scanf("%d", &pbook->serial_number);
         scanf("%c", &c);  /*For the newline character */
    }
    Input in the first loop:

    "Harry Potter"
    "\n"
    -----now---->------"hello world" is printed for DEBUG
    "J.K."
    "\n"
    "good morning"
    "\n"
    "123"
    "\n"
    "123"
    "\n"
    "123"
    "\n"
    The thing about telling scanf, "%s", is that it will read in one string. Let me share what the man page says:
    Originally Posted by man page
    s

    Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null byte ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.
    So then, Strings are delimited by white space, which includes spaces and newlines. Knowing that, here is what your struct would contain:

    book_name: "Harry"
    author_name: "Potter"
    name_of_publisher: "J.K."
    year_of_publishing: <garbage> -- because 'g' is not a valid digit, the conversion would fail and scanf would have returned a zero.
    cost: <garbage> -- for the same reason, though I don't know whether an invalid character is consumed by a failed conversion.
    serial_number: <garbage> -- for the same reason

    Then if you succeeded in trying to enter a second book, then the program would have fed it the rest of "good morning" and the three numeric values. You couldn't tell what was happening since you have no prompts to tell the user which field he's inputting for; having prompts would have also been your first clue that the values weren't getting read in as you thought they would.

    In order to use scanf to read in an entire line of input as one string, you need to give it a slightly different format string:
    scanf("%[^\n]", temp);
    That tells scanf to read in every character until it hits a newline; ie, accept every character except a newline. Read the documentation on scanf (ie, the man page) and read up on how square brackets are used as well as the circumflex (^). Then write a simple test program to try out various combinations so that you can play with the possibilities and learn from them.

    Do not use gets(). Instead of gets, you should use fgets(stdin); read the man page for gets to learn why. Of course, with fgets(stdin), it will also read in the newline character(s), so you would need to detect and remove them. Or you could use scanf with the proper instructions to get the job done right.

    PS

    Have you set your IDE to display warnings, all warnings? If so, have you tested it yet to see whether it works?

    In the past, I've been told by Dev-C++ users that that IDE does not display warnings even when the option to turn them on has been selected. That is why I am asking you to verify that.

    It will do you no good to claim that the compiler does not generate warnings. Dev-C++ uses the MinGW gcc compiler, which is the exact same compiler that I used to show you the warnings. If Dev-C++ will not show you the warnings that your code generates, then you will need to find some other way to see those warnings, such as compiling your code from the command line, which is what I do.

    Until you have resolved this issue, do not dare try to claim that your code compiles without warnings.
    Last edited by dwise1_aol; July 31st, 2013 at 11:24 AM.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo