#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Need a solution! STRUCTURE!!(i am learner)


    Hello friends. I am following a book. and this is a sample program in that book for STRUCTURE.

    but when i run the program
    1. it should ask 4 ques but after entering 3rd answer it start the new iteration and jump to the 2nd question....please help me.


    Code:
    #include "bookInfo.h"
    #include <stdio.h>
    
    main()
    
    {
        int ctr;
        struct bookInfo books[3];
    
        for (ctr = 0; ctr < 3; ctr++)
        {
            printf("What is the name of the books #%d?\n", (ctr+1));
            gets(books[ctr].title);
            puts("Whos the author? ");
            gets(books[ctr].author);
            puts("How much did the book cost? ");
            scanf(" $%.2f", &books[ctr].price);
            puts("How many pages in the books? ");
            scanf(" %d", &books[ctr].pages);
        getchar();
    
        }
    
        printf("\n\nHere is the collection of books: \n");
        for (ctr = 0; ctr <3; ctr++)
        {
        printf("#%d: %s by %s", (ctr+1), books[ctr].title, books[ctr].author);
        printf("\nIt is %d pages and costs $%.2f", books[ctr].pages, books[ctr].price);
        printf("\n\n");
        }
        return(0);
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0
    Originally Posted by arifuddin
    Hello friends. I am following a book. and this is a sample program in that book for STRUCTURE.

    but when i run the program
    1. it should ask 4 ques but after entering 3rd answer it start the new iteration and jump to the 2nd question....please help me.


    Code:
    #include "bookInfo.h"
    #include <stdio.h>
    
    main()
    
    {
        int ctr;
        struct bookInfo books[3];
    
        for (ctr = 0; ctr < 3; ctr++)
        {
            printf("What is the name of the books #%d?\n", (ctr+1));
            gets(books[ctr].title);
            puts("Whos the author? ");
            gets(books[ctr].author);
            puts("How much did the book cost? ");
            scanf(" $%.2f", &books[ctr].price);
            puts("How many pages in the books? ");
            scanf(" %d", &books[ctr].pages);
        getchar();
    
        }
    
        printf("\n\nHere is the collection of books: \n");
        for (ctr = 0; ctr <3; ctr++)
        {
        printf("#%d: %s by %s", (ctr+1), books[ctr].title, books[ctr].author);
        printf("\nIt is %d pages and costs $%.2f", books[ctr].pages, books[ctr].price);
        printf("\n\n");
        }
        return(0);
    }
    Get rid of the loop,
    it is not needed here.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,143
    Rep Power
    2222
    What did the warnings tell you? And why are you ignoring warnings?
    C:\otros\dcw>gcc -Wall arifuddin01.c
    arifuddin01.c:14: warning: return-type defaults to `int'
    arifuddin01.c: In function `main':
    arifuddin01.c:25: warning: unknown conversion type character `.' in format
    arifuddin01.c:25: warning: too many arguments for format

    C:\otros\dcw>
    To begin with, you need to actually declare main to return int; you must not do it implicitly.

    Here is Line 25:
    Code:
            scanf(" $%.2f", &books[ctr].price);
    The compiler is telling you that it doesn't know anything about periods within a conversion format string. When you read the documentation on scanf (eg, at http://linux.die.net/man/3/sscanf, obtained by Google'ing on man page scanf), did it say anything about periods in a format string? %, yes. Maximum field width, yes. Type modifier character (in this case, the f for float), yes. Periods? Not a single word (I even did a find on that page).

    The next warning, "warning: too many arguments for format", is because the format string was invalid, so no conversion could be performed, hence the address you provided scanf (ie, &books[ctr].price) had no format and hence no conversion was even attempted.

    The rest falls right into place. I will assume that you entered something like 12.34. The next scanf converted the 12 and stopped at the period. The getchar swallowed the period, and the next input operation, the gets for the title, was satisfied by the 34.

    Lose that period. You might also not want to specify the maximum length of the price field; no damage done if you make it large enough for any price, but then what benefit would you get by doing that? After all, you're leaving yourself wide open for buffer overflow run-time headaches by using gets, which is deprecated and whose use is advised against -- read the documentation on gets.

    Also, you are requiring the user to enter a dollar sign every time he enters the price. If he neglects to, then that conversion fails and the price amount gets accepted by the next scanf as the number of pages.

    RTFM! ("Read The Manual!") scanf has a return value. Read what that return value is and realize how you can use it to tell you whether it had succeed or failed in performing all the conversions you told it to perform. A very experienced forum member would have told you that with just a few keystrokes he could send your program running out into the weeds to vomit all over its own shoes. Your program is very brittle and will break very easily.

    Also, never ignore warnings! If you had heeded the warnings your compiler was throwing at you, you would have been able to solve your problem on your own four and a half hours ago. Instead, you waited all that time for somebody else to show you those warnings again. Programmers cannot afford to waste that kind of time.

    Warnings are more important than error messages. Always turn warnings on and up! And never ignore warnings!

    PS
    daveyerwin is wrong about that loop. But maybe he can try to explain and justify his reply.
    Last edited by dwise1_aol; December 30th, 2013 at 11:00 AM.

IMN logo majestic logo threadwatch logo seochat tools logo