Thread: Need help with C programming STRUCT!!!

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    2

    Need help with C programming STRUCT!!!

    Hello friends. here is my situation.
    1. I am learning C programming myself with the help of the book C PROGRAMMING ABSOLUTE BEGINNERS GUIDE.


    2. I am using code blocks ide compiler.


    3. in this book now i am learning SETTING UP DATA WITH STRUCTURES. And I have an example of the STRUCT.




    Code:
    //header file "bookInfo.h" saved in the same folder
    struct bookInfo {
        char title[40];
        char author[25];
        float price;
        int pages;
    };



    Code:
    #include "bookInfo.h" // header file already created and saved in the same folder.
    #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(" $%f", &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);
    }

    now the prob is when i compile the program
    1. it works ok upto
    puts("How many pages in the books? ");


    after this it does not take any value...just printing on the screen n jumping to new loop n start from 2nd iteration.


    i am completely lost here. dont understand what to do here. please HELP.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First you should never, Never, NEVER use gets(). This function is so unsafe it has been removed from the current C standard. I recommend you replace your gets() calls with fgets() which can be safely used.

    Next be careful when you place other characters in your scanf() specifier string. Remember if you place a spurious character in the specifier you must also enter this character. For example with your "price" field you must enter the '$' along with the numeric value. I recommend you place this character in the prior puts and only use the "%f" specifier, also note the lack of the space. Remember with numeric entries scanf() skips white space characters by default.

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    2
    Jim thanks a lot. this is such a small mistake which is entering $ along with numeric value but for me its something really big. i would never be able to find this. i have been waiting for one month and i was stuck at this point. amazing. thanks from my heart bro. jim. i am a beginner. i am learning myself. no teacher no proper guide line. can u give me some tips. or can i add u in facebook or can i get your email address. thanks again. arif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with struct, c-programming
    By jcun3 in forum C Programming
    Replies: 8
    Last Post: 12-14-2013, 10:01 AM
  2. need help with opaque programming with struct and addresses.
    By DarkAngel4ever in forum C Programming
    Replies: 11
    Last Post: 03-30-2011, 08:19 PM
  3. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  4. com programming - interface cannot use my struct
    By y_cant_i_C in forum Windows Programming
    Replies: 1
    Last Post: 12-04-2006, 11:06 PM

Tags for this Thread