Searching for an array based on a variable and displaying it based from objects.

Hello, I have been trying to search the array for a specific car (array) based on the choice between: Make, Model, Year or number of Cylinders and display the items retrieved by the search on the screen from here:
My data in the text file presented here was passed already into arrays and printed as:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    Text File:
    Volkswagen
    Jetta
    2016
    4
    
    Jeep
    Wrangler
    2008
    6
    
    Toyota
    Corolla
    1999
    4
    
    Chevy
    Malibu
    2018
    6
    
    Chevy
    Malibu
    2014
    6
    
    BMW
    320i
    2014
    6
    
    Jeep 
    GrandCharokeeLaredo
    2006
    6
    
    Ford
    Fusion
    2012
    4
    
    Chevy
    Equinox
    2015
    4
    
    Volkswagen
    Jetta
    1998
    4
------------------------------
// Actual code:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <time.h>
#include <math.h>
using namespace std;

class Cars
{
private:
	// Private variables of cars
	string make;
	string model;
	int year;
	int cylinders;
public:

	// Function within the objects
	void setMake(string m) { make = m; }
	string getMake() { return make; }

	void setModel(string mo) { model = mo; }
	string getModel() { return model; }

	void setYear(int y) { year = y; }
	int getYear() { return year; }

	void setcylinders(int c) { cylinders = c; }
	int getCylinders() { return cylinders; }

	
};

int main()
{
	//Opening the file
	ifstream inputFile;
	inputFile.open("cars.txt");

	// Variables for car characteristics
	string make;
	string model;
	int year;
	int cylinders;

	string inMa;
	string inMo;
	int inYe;
	int inCy;

	// Object array
	Cars car[10];

	cout << " Display of available automobiles: " << endl;
	cout << endl;


	// Reading from file and passing into array
	for (int x = 0; x < 10; x++)
		if (inputFile >> make >> model >> year >> cylinders)
		{

			car[x].setMake(make);
			car[x].setModel(model);
			car[x].setYear(year);
			car[x].setcylinders(cylinders);


			cout << right << setw(7) << " Car #" << x << " " << endl;
			cout << "Maker: " << make << endl;
			cout << "Model: " << model << endl;
			cout << "Year made: " << year << endl;
			cout << "Number of cylinders: " << cylinders << endl;
			cout << endl;

		}
	inputFile.close();

	int selection;
	cout << " Selection of automobiles:" << endl;
	cout << " Select an option to describe your car/cars. " << endl;
	cout << " ----------------------------" << endl;
	cout << " 1. Maker of Car: " << endl;
	cout << " 2. Model of Car: " << endl;
	cout << " 3. Year of the Car made: " << endl;
	cout << " 4. Number of cylinders: " << endl;
	cin >> selection;
	if (selection == 1)
    	{
    		cout << " Who is the maker of the car? " << endl;
    		// Need help displaying array that contain a specific variable 
            // Ex: All arrays that contain "Jeep"
        }
        else if (selection ==2)
        {
           cout << " What is the model of the car? " << endl;
           // Need help displaying array that contain a specific variable
        }
        else if (selection ==3)
        {
           cout << " What was the year the car was made? " << endl;
           // Need help displaying array that contain a specific variable
        }
        else if (selection ==4)
        {
           cout << " How many cylinders does the car have?" << endl;
           // Need help displaying array that contain a specific variable
        }
        else
        {
           cout << "Enter a valid response" << endl;
        }
    	system("pause");
    	return 0;
    
}


My problem is that I do not know how to transfer from a user input (ex: "Volkswagen") from a menu and display the arrays with that characteristic. How can I match arrays with the user input and output it the user based on the object arrays? I am not worried if the user misinputs the variable during "cin".
Last edited on
What you seem to be asking to do is find a specific element in an array, but the criterion for selection varies.

A simple solution would be to just loop through the array and check that element you wish to examine:

1
2
3
4
5
6
7
8
std::string s;
getline( std::cin, maker );
for (int n = 0; n < ARRAY_SIZE; n++)
  if (car[n].getMake() == maker)
  {
    // display the object here, then
    break;
  }


Also, you should add a function to display a Car.

Hope this helps.
Topic archived. No new replies allowed.