Thread: Problems Displaying the Information

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

    Problems Displaying the Information

    Hello I wrote this code. It's meant to ask the user for some inputs and stores all the input the user gives into 1 single element of an array. After the structure Car and the array is passed to the function display car. Here the information is meant to be printed out of what was in the array. I would very much appriciate some help.

    Code:
     #include <stdio.h>
    
    int displaycar(Car *c, DAte *d, price *p, 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<=10;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);
    
                                           
                                           
    }
    
    displaycar(c, d, p);
    
    return 0;
    }
    
    int displaycar (Car *c, Date *d, Price *p, 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 %lf \n",c[]->purchasep->cost);
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you want help doing what? have you tried to compile it? what errors, if any, does your compiler spit out? I see a couple problems, but you should learn how to identify them too. One error is a little suttle -- the for loop in main() iterates too many times, array car has only 10 elemenets, but the loop iterates 11 times (0 <= 10 == 11 iterations, count them on your fingers if you have to )

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    	Date date;
    	Price price;
    	Car car[sof];
    	Car *c;
    	Date *d;
    	Price *p;
    	int i;
    You only use two of those variables, prune the rest out.

    > for(i=0; i<=10;i++)
    You created a macro constant for the size, so use it.
    for ( i = 0 ; i < sof ; i++ )
    Yes, this fixes the problem Ancient Dragon has already mentioned.

    > displaycar(c, d, p);
    You declared this as taking 4 parameters.
    In fact, the only one you need is the car array.
    displaycar( car );

    Where you have
    int displaycar( Car car[] );

    Code:
    int displaycar (Car car[] )
    {
    	printf(" The name of the car is %s \n", c[0].make);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with displaying highscore (snprintf)
    By Turbolego in forum C Programming
    Replies: 3
    Last Post: 11-29-2008, 10:07 AM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. Struct/parse returning wrong information
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 01:39 PM
  5. Displaying Process Information From task_struct?
    By smoothdogg00 in forum C Programming
    Replies: 2
    Last Post: 12-19-2006, 08:05 PM