hi guys

i have a problem that asks me to work with a given structure and to set an array of 35 and initialize the elements. then i take a loop statement that will step through the entire array and display the contents of each element. since the question only asked me to do statements i did not do an entire program so please check if the following is correct :)

ps-i was already given the list of cars and models, etc.

//given struct
struct Car
{
	char carMake[20];
	char carModel[20];
	int yearModel;
	double cost;
};

//define array of car struct with 35 array and initialize elements

car vehicle1[35] = {Ford, Taurus, 1997};
car vehicle2[35] = {Honda, Accord, 1992};
car vehicle3[35] = {Lamborghini, Countach, 1997};

//write a loop that will step thru the array displaying contents of each element

for (int index = 0; index < 35; index++)
{
	cout << vehicle[index].carMake << endl;
	cout << vehicle[index].carModel << endl;
	cout << vehicle[index].yearModel << endl;
}
//given struct
struct [B]Car[/B]
{
	...
};

//define array of car struct with 35 array and initialize elements

[B]car[/B] vehicle1[35] ...

@@ is it because of this case difference?

Sorry i couldn't understand ur problem. Could u please explain me what is the exact problem ur facing.

if u read the comments in the code, that is my questions and i wrote the code underneath-i just am not sure if it was done rite.

here:
1) using the given strucure, a)write an array of the car struct with 35 and b)intialize the first 3 elements of the list:

given struct

struct Car
{
	char carMake[20];
	char carModel[20];
	int yearModel;
	double cost;
};

given list:

carmake model year price
ford taurus 1997 10,000
honda accord 1992 5,000
lamborghini countach 1997 250,000

a) struct with 35 array

car vehicle1[35];

b) create an initialize list for 1st three elements:

car vehicle1[35] = {Ford, Taurus, 1997};
car vehicle2[35] = {Honda, Accord, 1992};
car vehicle3[35] = {Lamborghini, Countach, 1997};
//should the elements be in 3 sets as shown, or in just one, ie car vehicle[35]..

then the question asks me to write a loop that will go thru all of my elements and display it (using the statements from part b

this is what i have:

for (int index = 0; index < 35; index++)
{
	cout << vehicle[index].carMake << endl;
	cout << vehicle[index].carModel << endl;
	cout << vehicle[index].yearModel << endl;
}

hope it makes sense now. :)

So i think u are trying to make an array out of a user defined structure. To define array of structure u need help of new . simple assigning might create problem . let me show u the steps.

struct Car
{
	char carMake[20];
	char carModel[20];
	int yearModel;
	double cost;
};
int main()
{
    Car *vehicle1 = new Car[35]  //size of array is 35

    //assigning part
   //create a temporary object
  car temp= {"Ford", "Taurus", 1997};
 //then assign to array vehicle
  vehicle1[0] = temp;
    
   // u can make a loop to assign more than 1 elements in this way
.
.
.
 // rest of the code

}

Let me know if it helps

can i also do it using just one object?

ex:

car temp = {Ford, Taurus, 1997),
                 {Honda, Accord, 1992},
                 {Lamborghini, Countach, 1997};

//do i need quotations for the char names?
//how would i assign the loop? was my method correct? (see 1st post)

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This is initialization because all values in the array are set to zero or hello at time of declaration:

int array[3] = {0};
char array[3][6] = {"hello"};

This initializes to different values at time of declaration:

int array1[3] = {1, 2, 3};
char array[3][6] = {"joe", "deer", "car"};

When declaring arrays of user defined type the default constructor is used. Therefore if the default constructor initializes elements to a default value, then the default values will be in the elements of the array. If the default constructor doesn't initialize default values then accessing member values of array elements will return junk.

Once an object has been declared any subsequent changes to its value is called assignment. However, the first time you assign a value to an object, it is frequently called initializing since it is the initial value.

int i; //the value of i is junk
i = 99; //assign value to i. Since it is the first value assigned to i, the is also frequently called initializing i.

When accessing elements of an array you need to use an index. Say you have have declared an array as below:

int arrray2[3];
char names[3][6];

Now to assign values (initialize, if you will) the elements of the array you need to use indexes.

array2[0] = 3;
array2[1] = 55;
array2[2] = -33;

strcpy(names[0], "Tom"};
strcpy(names[1], "Dick"};
strrcpy(names[2], "Joe"};

Note, since this isn't true initialization at time of declaration, the strcpy() function must be used to assign values to (C style) strings; the assignment operator will not work. The same goes for member values.

struct person
{
    char name[80];
    int age;
};
person family[2];
strcpy(family[0].name, "Sally"};
family[0].age, 21;
strcpy(family[1].name, "Hal");
family.age = 22;
This article has been dead for over six months. Start a new discussion instead.