Hello,
Say I need to store into an array data about a car.
So, a typical index of this said array would contain data such as,
1 2 3 4 5 6 7 8 9
|
char arrCars[5];
for (int i = 0; i < 5; i++) {
std::cout << "Make: ";
std::cin >> make;
std::cout << "Model: ";
std::cin >> model;
std::cout << "Price: ";
std::cin >> price;
}
|
I want
arrCars[3].show()
to then for example return something like,
1 2 3 4
|
//arrCars[3]
make: "Acura",
model: "RDX",
price: 10000
|
The problem is that I can make
make
model
and
price
into private member data types of a class, in which
arrCars
can reside.. but what if I dont want to do that? Can I replicate this functionality, like in Javascript? Simply create an object array, that has many attributes per cell? For example a
person
object would have attributes such as name, age and a function
talk()
which outputs to console the attributes.
Because when I cycle, I get undesired output:
1 2 3 4 5 6 7 8 9
|
cars = new char[3];//dynamic allocation
for (int i = 0; i < 3; i++) {
std::cout << "Car#" << i + 1 << " is\n> " << std::endl;
std::cin >> cars[i]; //ACURA
}
std::cout << "Your cars are:" << std::endl;
for (int i = 0; i < 3; i++) {
std::cout << ">>" << cars[i] << "<<\t\t"; // >>A<< >>C<< >>U<<
}
|
Perhaps something like,
1 2 3 4 5 6 7
|
char cars[5][4];
for (int i = 0; i < 3; i++) {
std::cout << "Car#" << i + 1 << " is\n> " << std::endl;
for (int j = 0; j < 4; j++) {
std::cin >> cars[i][j];
}
}
|
Desired output:
cars[0][0] = Acura;
cars[1][0] = BMW;
cars[2][0] = Opel;