Thread: help with structures

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65

    help with structures

    Hi, i'm trying to understand structures so i tried with this example:
    Code:
    #include <stdio.h>
    int main()
    {
    	struct book {
    		char name;
    		float price;
    		int pages;
    	};
    	struct book b1, b2, b3;
    	
    	printf("\nEnter names, prices, and number of pages of 3 books:\n");
    	scanf("%c %f %d", &b1.name, &b1.price, &b1.pages);
    	scanf("%c %f %d", &b2.name, &b2.price, &b2.pages);
    	scanf("%c %f %d", &b3.name, &b3.price, &b3.pages);
    	printf("\nAnd this is what you entered:\n");
    	printf("\n%c %f %d", b1.name, b1.price, b1.pages);
    	printf("\n%c %f %d", b2.name, b2.price, b2.pages);
    	printf("\n%c %f %d\n", b3.name, b3.price, b3.pages);
    	
    	return 0;
    }
    the output is wrong though, the program lets me input only the data for the first and second books but not for the third, heres what happens:
    Code:
    Enter names, prices, and number of pages of 3 books:
    a 234 234
    b 243 235
    
    And this is what you entered:
    
    a 234.000000 234
    
     0.000000 0
    b 243.000000 235
    can someone explain me why this happens? thanks!

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    scanf leaves the newline in the input buffer, so the second call to scanf will first see the newline character and decide it can't do anything. Then the third call to scanf gets the second line you entered.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You can modify your scanf() calls to handle the newline's that are left in the input buffer:
    Code:
        scanf("%c %f %d", &b1.name, &b1.price, &b1.pages);
        scanf("\n%c %f %d", &b2.name, &b2.price, &b2.pages);
        scanf("\n%c %f %d", &b3.name, &b3.price, &b3.pages);
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    Thanks, i really did think about it.. so the second scanf was receiving the newline character thats why it messed up

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    It'll mess up the second scanf because the %c specifier doesn't skip over whitespace.
    This means that b2.name will get the newline and other members of b2 will be zero.
    One way to get around this would be to convert all the %c specifiers to %s.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and dynamically allocated arrays
    By Bandizzle in forum C Programming
    Replies: 7
    Last Post: 10-04-2009, 02:05 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM