Thread: How to put it into an array

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    How to put it into an array

    Hello I wrote this code everything works fine but i don'nt know how to put it all into an array. The user enteres 8 piece of information and i want all of that to be put into 1 element in an array. I will be greatful if someone can fix it for me.

    Code:
    #include <stdio.h>
    
    #define s 20
    
    typedef struct { 
    	int day;
    	int month;
    	int year;
    } Date;
    
    typedef struct {
    	float cost;
    } Price;
    
    typedef struct {
    	char make[s];
    	Date purchaseDate;
    	Date manufactureDate;
    	Price purchasePrice;
    } Car;
    
    int main()
    {
    	Date date;
    	Price price;
    	Car car;
    	
    printf("Please Enter the name of the car: ");
    scanf("%s",&car.make);
    
    printf(" Please enter the day of purchase: ");
    scanf("%d", &car.purchaseDate.day);
    
    printf(" Please enter the month of purchase: ");
    scanf("%d", &car.purchaseDate.month);
    
    printf(" Please enter the year of purchase: ");
    scanf("%d", &car.purchaseDate.year);
    		
    printf(" Please enter the day of manufacture: ");
    scanf("%d", &car.manufactureDate.day);
    
    printf(" Please enter the month of manufacture: ");
    scanf("%d", &car.manufactureDate.month);
    
    printf(" Please enter the year of manufacture: ");
    scanf("%d", &car.manufactureDate.year);
    		
    printf(" Please enter the price the car was bought for: ");
    scanf("%f", &car.purchasePrice.cost);
    
    printf(" The name of the car is %s \n", car.make);
    		
    printf(" The car was purchased on the %d of the %d month of the year %d \n", car.purchaseDate.day, car.purchaseDate.month, car.purchaseDate.year);
    
    printf(" The car was manufactured on the %d of the %d month of the year %d \n", car.manufactureDate.day, car.manufactureDate.month, car.manufactureDate.year);
    
    printf(" The car was bought for %f \n",car.purchasePrice.cost);
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Do you mean that
    Code:
    int main()
    {
        // Date date;    // not used
        // Price price;  // not used
        Car car[10];    // array of 10 cars
    	
        printf("Please Enter the name of the car: ");
        scanf("%s",car[0].make);
    
       printf(" Please enter the day of purchase: ");
       scanf("%d", &car[0].purchaseDate.day);
       .....
    Kurt
    Last edited by ZuK; 10-10-2005 at 01:27 PM.

  3. #3
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    scanf("%s",&car.make);
    instead of this take a string variable and use strcpy().
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. put number in array
    By nevrax in forum C Programming
    Replies: 26
    Last Post: 03-27-2007, 04:20 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM