Thread: structure

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    6

    structure

    I am having problem with the following code; plz help.....

    Code:
    #include<stdio.h>
    struct book
    {
        char name;
        float price;
        int pages;
    };
    main()
    {
        struct book b[10];
        int i,n;
        printf("\nEnter the no of entry\n");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            printf("Enter name ,price and pages");
            scanf("%c%f%d",&b[i].name,&b[i].price,&b[i].pages);
        }
        for(i=0;i<n;i++)
        {
            printf("%c\n%f\n%d\n",b[i].name,b[i].price,b[i].pages);
        }
    }
    IT OUTPUTS:
    ---------------------------------
    Enter the no of entry
    2
    Enter name ,price and pages d 5 34
    Enter name ,price and pages


    -1.594697
    12700610
    d
    5.000000
    34
    -----------------------------------
    On entering values for the first instance of the structure it prints the msg "Enter name ,price and pages" but doesn't wait for me to enter values this time.. and prints ..
    -1.594697
    12700610

    then it prints the values of the 1st instance... I m not being able to figure out whats happening... plz help

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    First in your structure name is a single character, so it can only hold 1 character. Is that what you are entering? Next scanf() leaves the new line character in the input buffer which is being extracted by the second scanf(). To solve this problem you need to extract the newline character before you try to retrieve the name. You can tell scanf() to skip leading whitespace by inserting a space before the character specifier, example:
    Code:
    scanf(" %c", &someValue);
    .

    I really think you want to accept more than one character for your variable name, and if so you need to make that variable a character string, not a single character. And then change the " %c" to " %s", note you will need to skip leading white space characters for the string as well.

    Jim

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    @Jim
    Thank you very much. My problem is solved by inserting a space before the character specifier. But I could not actually get what happend.

    As you said the '\n' on hitting ENTER is left in the input buffer which is extracted by the second scanf() and it worked as if i have pressed ENTER.
    But how is this solved by the space. Would you kindly elaborate or can you suggest me some good materials to clear these concepts .

    And about the name , i was trying with the single character name only.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by beginningC View Post
    Would you kindly elaborate or can you suggest me some good materials to clear these concepts .

    scanf format string - Wikipedia, the free encyclopedia

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    But how is this solved by the space. Would you kindly elaborate or can you suggest me some good materials to clear these concepts .
    The leading space forces scanf() to skip the leading white space. See this link and from that link:
    A directive is one of the following:



    A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input.

    • An ordinary character (i.e., one other than white space or '%'). This character must exactly match the next character of input.
    That first bullet is the reason that adding the space reads your new line from the buffer.

    Also from the conversion specifier section:
    c

    Matches a sequence of characters whose length is specified by the maximum field width (default 1); the next pointer must be a pointer to char, and there must be enough room for all the characters (no terminating null byte is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format.
    Jim

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    thank you very much..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find number of structure members in a given structure?
    By bhaskarReddy in forum C Programming
    Replies: 4
    Last Post: 01-16-2012, 05:37 AM
  2. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  3. Replies: 1
    Last Post: 04-02-2009, 06:51 AM
  4. Replies: 9
    Last Post: 05-21-2007, 12:10 AM
  5. Replies: 4
    Last Post: 11-22-2006, 12:20 PM

Tags for this Thread