Storing into array

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;
Last edited on
Object-oriented.

You appear to be trying to work with arrays of char. That makes no sense.

Create a car object that can populate itself and show itself.

Create an array of those car objects.

For each car object in the array, call populate.

For each car object in the array, call show.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;

struct carType
{
  string make;
  string model;
  int price;

  void  show()
  {
     cout << "make: "  << make << '\n'
          << "model: " << model << '\n'
          << "price: " << price << '\n';
  }

  void populate()
  {
          cout << "Enter make: " << '\n';
          cin >> make;
          cout  << "Enter model: " << '\n';
          cin >> model;
          cout << "Enter price: " << '\n';
          cin >> price;
  }
};

int main()
{
  carType array_of_cars[3];

  for (int i = 0 ; i < 3 ; ++i)
  {
    array_of_cars[i].populate();
  }

  for (int i = 0 ; i < 3 ; ++i)
  {
    array_of_cars[i].show();
  }
}

Last edited on
I see thank you. But say I have 4 different employees, and they all need parking for their cars. Each employee may have up to 2 cars, for some arbitrary reason.

I would create each employee with

1
2
3
4
5
6
7
8
9
class Employee {
	char* name;
	short age;
	int id;
  public:
	Employee();
	Employee(const char*, short, int);
	//whatever other necessary public member functions
}


And then how would I associate each employee with a car?

1
2
3
4
5
6
7
8
class Car : public Employee {
	char* make;
	char* model;
	int year;
	float monthlyCharge;
  public:
    //whatever necessary public member functions
}
char* name;
Just use a string. That's what they're for.

class Car : public Employee {
This makes no sense at all. This says that a Car is a kind of Employee. It's valid syntax, but it makes no sense.

Here's one option; each Employee object contains a pointer-to-car, and if that employee has a car, the pointer is made to point to the appropriate car object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Car{
  public:
	string make;
	string model;
	int year;
	float monthlyCharge;

    //whatever necessary public member functions
}

class Employee {
 public:
	string name;
	short age;
	int id;
        car* pointer_to_employeesCar = nullptr;
 
	Employee();
	Employee(string, short, int);
	//whatever other necessary public member functions
}

int main()
{
  Employee an_employee("Mike", 25, 123);
  Car a_car;

  an_employee.pointer_to_employeesCar = &a_car;
}


Alternatively, if a car simply belongs, always, to an employee:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Car{
  public:
	string make;
	string model;
	int year;
	float monthlyCharge;

    //whatever necessary public member functions
}

class Employee {
 public:
	string name;
	short age;
	int id;
        car employees_car;
 
	Employee();
	Employee(string, short, int);
	//whatever other necessary public member functions
}

int main()
{
  Employee an_employee("Mike", 25, 123);
  an_employee.employees_car.make = "Some car make"; // or call some function on the car, or some other option

}


If an Employee can have zero, one or two cars:

1
2
3
4
5
6
7
8
9
10
11
class Employee {
 public:
	string name;
	short age;
	int id;
        car[2] employees_cars; // set these blank in the Employee constructor
 
	Employee();
	Employee(string, short, int);
	//whatever other necessary public member functions
}


or in the C++ way, with vector rather than array (stop using arrays; they're bad):
1
2
3
4
5
6
7
8
9
10
11
class Employee {
 public:
	string name;
	short age;
	int id;
        vector<car> employees_cars; // starts at size zero - no cars. Add cars to this vector as needed
 
	Employee();
	Employee(string, short, int);
	//whatever other necessary public member functions
}


Your problems don't seem to be so much about code as about thinking with objects.
Last edited on
Topic archived. No new replies allowed.