Thread: Almost There Just Need A Little Help

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

    Almost There Just Need A Little Help

    HELLO I wrotr this code. It's meant to accept all the 8 inputs the user gives as an element in the array. I was just wondering if somebody could please help me do that.

    Code:
    #include <stdio.h>
    
    #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[sof];
    } Car;
    
    int main()
    {
    	Date date;
    	Price price;
    	Car car;
    	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);
    
    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[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);
    
    }
    return 0;
    }

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by HAssan
    Code:
    typedef struct {
    	char make[s];
    	Date purchaseDate;
    	Date manufactureDate;
    	Price purchasePrice;
    	car[sof];
    } Car;
    What is "car"? It's not been declared, and you've not given it a type.

    Code:
    Car car;
    Above, you are declaring one car, but you are referencing it as an array further down.

    Code:
    printf(" The name of the car is %s \n", car.make);
    Which car are you referring to? Shouldn't this also be car[i].make?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Hello Hassan!

    i agree with cwr on the part that inside ur struct

    Code:
      typedef struct {
    	char make[s];
    	Date purchaseDate;
    	Date manufactureDate;
    	Price purchasePrice;
    	car[sof]; // u have not defined what type.        //put maybe like 'char car[sof]'
    } Car;
    then your struct will read

    Code:
      typedef struct {
    	char make[s];
    	Date purchaseDate;
    	Date manufactureDate;
    	Price purchasePrice;
    	char car[sof];
    } Car;
    Yes, thank goodness, C is not case sensitive as Java. However this is bad programming practice, as you r confusing urself by having ur instance called 'Car' and then inside the instance 'Car' there is a member called 'car[sof]'. Name it something else.

    Of course, u can put something inside this member but now ur code will read

    Code:
    int main(void) {
    
    Car car;
    
    ...
    
      scanf("%s", car.car[i]);
    
    return 0;
    
    }
    so the first car before the dot (.) sign refers to the instance, and the second car (after the dot) refers to the member "car[sof]"

    cwr wrote:

    >>Code:

    >>Car car;

    >>Above, you are declaring one car, but you are referencing it as an >>array further down.

    >>Code:

    >>printf(" The name of the car is %s \n", car.make);

    >>Which car are you referring to? Shouldn't this also be car[i].make?

    Actually hassan's code is correct.

    From the instance created with

    Code:
    Car car ; // an instance of the struct is created called 'car' (small letter 'c')
    therefore,
    Code:
     
     printf(" The name of the car is %s \n", car.make);
    is refering to the member inside of struct car, which is:
    Code:
     char make[s];
    Cheerz!

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by sidideas
    C is not case sensitive
    C is case-sensitive. Car is not the same as car or caR or any other permutation of the same sort.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by sidideas
    cwr wrote:
    >>Code:
    >>Car car;
    >>Above, you are declaring one car, but you are referencing it as an >>array further down.
    >>Code:
    >>printf(" The name of the car is %s \n", car.make);
    >>Which car are you referring to? Shouldn't this also be car[i].make?
    Actually hassan's code is correct.
    From the instance created with
    Code:
    Car car ; // an instance of the struct is created called 'car' (small letter 'c')
    therefore,
    Code:
     
     printf(" The name of the car is %s \n", car.make);
    Yes, that single example car.make is correct, but only coincidentally. Look at every single other car reference in printf/scanf. They are all car[i].foo, ie. an array. If you look, he obviously meant to have car.make also be car[i].make. It's a loop iterating through with the i variable, so the correct thing to do is change car.make into car[i].make and declare an array of Car. I already explained this in my above post, my statements were correct about his errors.

Popular pages Recent additions subscribe to a feed