Thread: I really really need help

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

    I really really need help

    Hello I wrote this code it's meant to take in a few inputs by the user and store them in an element in an array. Then the display car function is meant to display each element of the array. I can really use some help please. If someone fix my code I will be very greatful. Becuse I can'nt figure out what's wrong with it.

    Code:
    include <stdio.h>
    
    int displaycar(Car car[]);
    
    #define s 20
    
    #define sof 10
    
    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[sof];
    	//Car *c;
    	//Date *d;
    	//Price *p;
    	int i;
    	for(i=0; i<sof; i++)
    	{
    			printf("Please Enter the name of the car: ");
    			scanf("%s",&car[i].make);
    
                                            printf(" Please enter the day of purchase: ");
                                            scanf("%d", &car[i].purchaseDate.day);
    
                                            printf(" Please enter the month of purchase: ");
                                            scanf("%d", &car[i].purchaseDate.month);
    
                                            printf(" Please enter the year of purchase: ");
                                            scanf("%d", &car[i].purchaseDate.year);
    		
                                            printf(" Please enter the day of manufacture: ");
                                            scanf("%d", &car[i].manufactureDate.day);
    
                                            printf(" Please enter the month of manufacture: ");
                                            scanf("%d", &car[i].manufactureDate.month);
    
                                            printf(" Please enter the year of manufacture: ");
                                            scanf("%d", &car[i].manufactureDate.year);
    		
                                           printf(" Please enter the price the car was bought for: ");
                                           scanf("%f", &car[i].purchasePrice.cost);
    
                                           //printf(" The name of the car is %s \n", car[i].make);
    		
                                           //printf(" The car was purchased on the %d of the %d month of the year %d \n", car[i].purchaseDate.day, car[i].purchaseDate.month, car[i].purchaseDate.year);
    
                                           //printf(" The car was manufactured on the %d of the %d month of the year %d \n", car[i].manufactureDate.day, car[i].manufactureDate.month, car[i].manufactureDate.year);
    
                                           //printf(" The car was bought for %lf \n",car[i].purchasePrice.cost);
                                           
    }
    
    displaycar(Car car[]);
    
    return 0;
    }
    
    int displaycar (Car car[] )
    {
    	
    	printf(" The name of the car is %s \n", c->make);
    	
    printf(" The car was purchased on the %d of the %d of the year %d\n", c->purchased->day, c->purchased->month, c->purchased->year);
    
    printf(" The car was manufactured on the %d of the %d month of the year %d \n", c->manufactured->day, c->manufactured->month, c->manufactured->year);
    
    printf(" The car was bought for %f \n",c->purchasep->cost);
    
    return 0;
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    here is the working code, well, i have rectified all the erroe from your program.
    Code:
    #include<stdio.h>
    #define s 20
    #define sof 2
    
    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 displaycar(Car car[]);
    
    int main()
    {
    	Date date;
    	Price price;
    	Car car[sof];
    	//Car *c;
    	//Date *d;
    	//Price *p;
    	int i,ch;
    	for(i=0; i<sof; i++)
    	{
    			printf("Please Enter the name of the car: ");
    			scanf("%s",car[i].make);
    
                                            printf(" Please enter the day of purchase: ");
                                            scanf("%d", &car[i].purchaseDate.day);
    
                                            printf(" Please enter the month of purchase: ");
                                            scanf("%d", &car[i].purchaseDate.month);
    
                                            printf(" Please enter the year of purchase: ");
                                            scanf("%d", &car[i].purchaseDate.year);
    		
                                            printf(" Please enter the day of manufacture: ");
                                            scanf("%d", &car[i].manufactureDate.day);
    
                                            printf(" Please enter the month of manufacture: ");
                                            scanf("%d", &car[i].manufactureDate.month);
    
                                            printf(" Please enter the year of manufacture: ");
                                            scanf("%d", &car[i].manufactureDate.year);
    		
                                           printf(" Please enter the price the car was bought for: ");
                                           scanf("%f", &car[i].purchasePrice.cost);
    
                                           //printf(" The name of the car is %s \n", car[i].make);
    		
                                           //printf(" The car was purchased on the %d of the %d month of the year %d \n", car[i].purchaseDate.day, car[i].purchaseDate.month, car[i].purchaseDate.year);
    
                                           //printf(" The car was manufactured on the %d of the %d month of the year %d \n", car[i].manufactureDate.day, car[i].manufactureDate.month, car[i].manufactureDate.year);
    
                                           //printf(" The car was bought for %lf \n",car[i].purchasePrice.cost);
                                           
    }
    
    
    displaycar( car);
    while((ch=getchar())!='\n' && ch!=EOF);
    getchar();
    
    return 0;
    }
    
    int displaycar (Car car[])
    {
        int i;
    	for(i=0;i<sof;i++)
    	{
    	printf(" The name of the car is %s \n", car[i].make);
    	
        printf(" The car was purchased on the %d of the %d of the year %d\n", car[i].purchaseDate.day, car[i].purchaseDate.month, car[i].purchaseDate.year);
    
        printf(" The car was manufactured on the %d of the %d month of the year %d \n", car[i].manufactureDate.day, car[i].manufactureDate.month, car[i].manufactureDate.year);
    
        printf(" The car was bought for %f \n",car[i].purchasePrice.cost);
        }
        return 0;
    }
    NOTE:check out the changes made
    ssharish

Popular pages Recent additions subscribe to a feed