Thread: Where are the Errors in this Programs? I can not understand.

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    22

    Post Where are the Errors in this Programs? I can not understand.

    Code:
    #include <stdio.h>
    
    struct Bookinfo
    {
          char[20] bname;
          int pages;
          int price;
    }book[3];
    
    int main(int argc, char *argv[])
    {
    int i;
    
    for(i=0;i<3;i++)
        {
        printf("\nEnter the Name of Book    : ");
        gets(book[i].bname);
        printf("\nEnter the Number of Pages : ");
        scanf("%d",book[i].pages);
        printf("\nEnter the Price of Book   : ");
        scanf("%f",book[i].price);
        }    
    
    printf("\n--------- Book Details ------------ ");
        
    for(i=0;i<3;i++)
        {
        printf("\nName of Book    : %s",book[i].bname);
        printf("\nNumber of Pages : %d",book[i].pages);
        printf("\nPrice of Book   : %f",book[i].price);
        }    
    
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,112
    It looks like a "fix the errors in this program" homework to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    22
    No it is not for homework.I was trying to write a program for storing book information using structure and array in c program.I am new in c programming Language.For this i find this kind of program in ww.google.com.But i find it in this website.So it is not homework type Thing. C array of structure - C Programming - c4learn.com

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Have a closer look at the line
    Code:
    char[20] bname;
    Is this really the correct way to declare an c-style string?
    Maybe have another look at your textbook.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,112
    That site is a train-wreck!
    You'd do better to delete it from your browser and seek other learning resources.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2017
    Posts
    22
    char bname [20];
    but after solve this problem i got more error after compile and run this program.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,112
    Because the program and site are rubbish.
    Throw it away.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,762
    The website, apart from providing non-compiling code doesn't even indent the code consistently :-o LOL

    Anyway, since the OP is working from such a tragic example it cannot hurt to provide them with how the code should be

    Code:
    #include <stdio.h>
    
    struct Bookinfo
    {
          char bname[20];
          int pages;
          double price;
    } book[3];
    
    int main(void)
    {
        int i;
    
        for(i = 0; i < 3; i++)
        {
            printf("Enter the Name of Book    : ");
            scanf("%s", book[i].bname);
            printf("Enter the Number of Pages : ");
            scanf("%d", &book[i].pages);
            printf("Enter the Price of Book   : ");
            scanf("%lf", &book[i].price);
        }
    
        printf("\n--------- Book Details ------------\n");
    
        for(i = 0; i < 3; i++)
        {
            printf("Name of Book    : %s\n", book[i].bname);
            printf("Number of Pages : %d\n", book[i].pages);
            printf("Price of Book   : %f\n", book[i].price);
        }
    
        return 0;
    }
    It's still not great, but at least it compiles HAHAHAHA

  9. #9
    Registered User
    Join Date
    Nov 2017
    Posts
    22
    Thanks a lot Hodor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C] Let me understand the errors
    By akula.c in forum C Programming
    Replies: 2
    Last Post: 02-04-2011, 07:15 AM
  2. Server code Errors that I do not understand
    By cloudsword in forum C Programming
    Replies: 5
    Last Post: 12-04-2009, 10:45 AM
  3. Errors I don't understand (overloading)
    By fuh in forum C++ Programming
    Replies: 5
    Last Post: 01-03-2003, 07:23 PM
  4. Some errors and warnings i dont understand
    By lakai02 in forum C Programming
    Replies: 6
    Last Post: 10-18-2002, 11:16 AM
  5. I'm a newbie and I can't understand the errors in my program
    By iluvmyafboys in forum C++ Programming
    Replies: 19
    Last Post: 02-20-2002, 10:40 AM

Tags for this Thread