Thread: help me to find out error

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    73

    help me to find out error

    Code:
    #include<stdio.h>
    struct book
    {
        char name;
        char author;
        int pages;
        float price;
    };
    void main()
    {
        struct book b1[100];
        int i; int n;
        printf("how many books");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            printf("Enter name of book %d\n",i+1);
    
            scanf("%s",&b1[i].name);
    
    
            printf("Enter author of book %d\n",i+1);
            scanf("%s",&b1[i].author);
    
    
            printf("Enter page length of book %d\n",i+1);
            scanf("%d",&b1[i].pages);
    
    
            printf("Enter price of book %d\n",i+1);
            scanf("%f",&b1[i].price);
        }
        printf("You entered");
        for(i=0;i<n;i++)
            printf("Name %s\n author %s\n pages %d\n price %f\n",b1[i].name,b1[i].author,b1[i].pages,b1[i].price);
    }

    the code is wroking it accepts user inputs but "You entered" not executes. that is i want to write information what user has type
    no errors in code but not showing me "you entered" information
    whats wrong in last printf() statement

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Are you sure it doesn't execute? Let me guess: it executes normally, but the console closes so fast afterwards that you miss it.

    Add this at the end of main, it should work. It empties the input buffer and waits for the user to press enter:
    Code:
    {
        int c;
        while ((c = getchar()) != '\n' && c != EOF)
            continue;
        puts("Press [Enter] to continue.");
        getchar();
    }
    Last edited by GReaper; 02-28-2016 at 06:22 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    73
    its not working... last printf() statemment not executes and i got program has stoped wroking error

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Your structure is a bit ridiculous.
    Code:
    struct book
    {
        char name;
        char author;
        int pages;
        float price;
    };
    This structure has enough space for a book with a single-letter title, and a single-letter author's name.

    So the unwitting user types something in his collection and sees:
    Code:
    how many books2
    Enter name of book 1
    Where the Red Fern Grows
    Enter author of book 1
    Enter page length of book 1
    Enter price of book 1
    Enter name of book 2
    Enter author of book 2
    Enter page length of book 2
    Enter price of book 2
    You entered
    And it crashes.

    You need to review strings.
    Code:
    char title[1000];
    char author[1000];
    
    scanf("%999s %999s", title, author);
    You never really know how big a string needs to be, but something is better than nothing.

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    73
    thxx problem solved...i just use name[100] and author[100] at start of program and problem solved...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can't find the error
    By endrick in forum C Programming
    Replies: 4
    Last Post: 09-21-2015, 05:53 PM
  2. can't find error
    By loongkee in forum C Programming
    Replies: 5
    Last Post: 12-10-2012, 09:52 AM
  3. Can't find error
    By shadowsora15 in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2007, 04:00 AM
  4. Cant find the error
    By Coder87C in forum C Programming
    Replies: 8
    Last Post: 06-19-2005, 01:57 AM
  5. I cant find the error
    By soka in forum C++ Programming
    Replies: 7
    Last Post: 07-14-2003, 08:12 PM