need help with a input function

I need help, I have the whole program written I just need a function where it asks the user to pick 3 of the 5 cars and compares them, like what I have. It compares ALL 5 of the cars, I want it so someone picks 3 of them then it will run the compare of best mileage etc etc
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
#include <iostream>
#include <string>


using namespace std;
const int SIZE = 5;

class car
{
private:
	string name;
	int mpg;
	double price;
	int horsepower;

public:
	car(string name, int mpg, double price, int horsepower);
	void viewCars();
	void bestMileage();
	void lowestPrice();
	void greatestHorspower();

};

int main()
{

	
	car carArray[SIZE] = {
						   car("2012 Toyota Scion tC", 26, 10575, 180),
						   car("2019 Toyota TUNDRA",   18, 33320, 310), 
						   car("2017 Chrysler 200",    25, 19000, 295),
						   car("2017 Chevy Silverado", 15, 26946, 285),
						   car("2007 BMC Envoy",       17, 4500,  291)
	};
	

	for (size_t i = 0; i < SIZE; i++) {
		carArray[i].viewCars();
		cout << endl;
	}

	
			carArray[5].bestMileage();
			cout << endl;
			carArray[5].lowestPrice();
			cout << endl;
			carArray[5].greatestHorspower();
		
	
	
	system("pause");
	return 0;
}

car::car(string name, int mpg, double price, int horsepower) {
	this->name = name;
	this->mpg = mpg;
	this->price = price;
	this->horsepower = horsepower;
}

void car::viewCars()
{
	cout << "Car name: " << name << endl;
	cout << "Car MPG: " << mpg << endl;
	cout << "Car price: " << price << endl;
	cout << "Car horsepower: " << horsepower << endl;
}
void car::bestMileage()
{
	car carArray[SIZE] = {
							   car("2012 Toyota Scion tC", 26, 10575, 180),
							   car("2019 Toyota TUNDRA",   18, 33320, 310),
							   car("2017 Chrysler 200",    25, 19000, 295),
							   car("2017 Chevy Silverado", 15, 26946, 285),
							   car("2007 BMC Envoy",       17, 4500,  291)
	};
	size_t idx = 0;
	for (size_t i = 1; i < SIZE; ++i) {
		if (carArray[i].mpg > carArray[idx].mpg) {
			idx = i;
		}
	}
	cout << "\nThe Vehicle with highest mileage:\n";
	carArray[idx].viewCars();
	cout << '\n';
}
void car::lowestPrice()
{
	car carArray[SIZE] = {
							   car("2012 Toyota Scion tC", 26, 10575, 180),
							   car("2019 Toyota TUNDRA",   18, 33320, 310),
							   car("2017 Chrysler 200",    25, 19000, 295),
							   car("2017 Chevy Silverado", 15, 26946, 285),
							   car("2007 BMC Envoy",       17, 4500,  291)
	};
	size_t idx = 0;
	for (size_t i = 1; i < SIZE; ++i) {
		if (carArray[i].price < carArray[idx].price) {
			idx = i;
		}
	}
	cout << "\nThe Vehicle with the lowest price is: \n";
	carArray[idx].viewCars();
	cout << '\n';
}
void car::greatestHorspower()
{
	car carArray[SIZE] = {
							   car("2012 Toyota Scion tC", 26, 10575, 180),
							   car("2019 Toyota TUNDRA",   18, 33320, 310),
							   car("2017 Chrysler 200",    25, 19000, 295),
							   car("2017 Chevy Silverado", 15, 26946, 285),
							   car("2007 BMC Envoy",       17, 4500,  291)
	};
	size_t idx = 0;
	for (size_t i = 1; i < SIZE; ++i) {
		if (carArray[i].horsepower > carArray[idx].horsepower) {
			idx = i;
		}
	}
	cout << "\nThe Vehicle with the highest Horse Power is : \n";
	carArray[idx].viewCars();
	cout << '\n';
}
Your class design is....ummm....a bit strange.

Your class shouldn't care how many objects of itself are being created.

Your comparison functions shouldn't be part of your class.

They can be friends so they can access your private data members without the need for public Getters/Setters.

You are creating a lot of temporary arrays in your comparison functions. You should pass the main() created array into your functions.

Passing the array into your comparison functions lets you also pass a created sub-array of user selected cars.

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
#include <iostream>
#include <string>

const unsigned SIZE = 5;

class car
{
private:
   std::string name;
   int mpg;
   double price;
   int horsepower;

public:
   car(std::string name, int mpg, double price, int horsepower);
   void viewCar();

public:
   friend void viewCars(car[], size_t);
   friend void bestMileage(car[], size_t);
   friend void lowestPrice(car[], size_t);
   friend void greatestHorspower(car[], size_t);

};

int main()
{
   car carArray[SIZE] = { car("2012 Toyota Scion tC", 26, 10575, 180),
                          car("2019 Toyota TUNDRA",   18, 33320, 310),
                          car("2017 Chrysler 200",    25, 19000, 295),
                          car("2017 Chevy Silverado", 15, 26946, 285),
                          car("2007 BMC Envoy",       17, 4500,  291) };

   std::cout << "Car comparisons with full list:\n";

   bestMileage(carArray, SIZE);
   lowestPrice(carArray, SIZE);
   greatestHorspower(carArray, SIZE);

   // let's simulate a user chosen list
   const unsigned USER_LIST = 3;

   car userList[USER_LIST] = { car("2012 Toyota Scion tC", 26, 10575, 180),
                               car("2017 Chrysler 200",    25, 19000, 295),
                               car("2007 BMC Envoy",       17, 4500,  291) };

   std::cout << "Car comparisons with user list:\n";

   bestMileage(userList, USER_LIST);
   lowestPrice(userList, USER_LIST);
   greatestHorspower(userList, USER_LIST);
}

car::car(std::string name, int mpg, double price, int horsepower)
{
   this->name       = name;
   this->mpg        = mpg;
   this->price      = price;
   this->horsepower = horsepower;
}

void car::viewCar()
{
   std::cout << "Car name: " << name << '\n';
   std::cout << "Car MPG: " << mpg << '\n';
   std::cout << "Car price: " << price << '\n';
   std::cout << "Car horsepower: " << horsepower << '\n';
}

void viewCars(car carArray[], size_t arrSize)
{
   for (size_t i = 0; i < arrSize; i++)
   {
      carArray[i].viewCar();
      std::cout << '\n';
   }
}

void bestMileage(car carArray[], size_t arrSize)
{
   size_t idx = 0;

   for (size_t i = 1; i < arrSize; ++i)
   {
      if (carArray[i].mpg > carArray[idx].mpg)
      {
         idx = i;
      }
   }

   std::cout << "\nThe Vehicle with highest mileage:\n";
   carArray[idx].viewCar();
   std::cout << '\n';
}

void lowestPrice(car carArray[], size_t arrSize)
{

   size_t idx = 0;

   for (size_t i = 1; i < arrSize; ++i)
   {
      if (carArray[i].price < carArray[idx].price)
      {
         idx = i;
      }
   }

   std::cout << "\nThe Vehicle with the lowest price is: \n";
   carArray[idx].viewCar();
   std::cout << '\n';
}

void greatestHorspower(car carArray[], size_t arrSize)
{
   size_t idx = 0;

   for (size_t i = 1; i < arrSize; ++i)
   {
      if (carArray[i].horsepower > carArray[idx].horsepower)
      {
         idx = i;
      }
   }

   std::cout << "\nThe Vehicle with the highest Horse Power is: \n";
   carArray[idx].viewCar();
   std::cout << '\n';
}
Car comparisons with full list:

The Vehicle with highest mileage:
Car name: 2012 Toyota Scion tC
Car MPG: 26
Car price: 10575
Car horsepower: 180


The Vehicle with the lowest price is:
Car name: 2007 BMC Envoy
Car MPG: 17
Car price: 4500
Car horsepower: 291


The Vehicle with the highest Horse Power is:
Car name: 2019 Toyota TUNDRA
Car MPG: 18
Car price: 33320
Car horsepower: 310

Car comparisons with user list:

The Vehicle with highest mileage:
Car name: 2012 Toyota Scion tC
Car MPG: 26
Car price: 10575
Car horsepower: 180


The Vehicle with the lowest price is:
Car name: 2007 BMC Envoy
Car MPG: 17
Car price: 4500
Car horsepower: 291


The Vehicle with the highest Horse Power is:
Car name: 2017 Chrysler 200
Car MPG: 25
Car price: 19000
Car horsepower: 295


From a design standpoint of what you are trying to achieve, being able to compare a sub-array selected by the user, using fixed sized arrays makes that more difficult than it should be. std::vector has several advantages over fixed sized arrays:

1. a vector can be resized at run-time, adding and deleting elements as needed.

2. no need to keep track of the size of a vector separately. That makes passing a vector into a function and accessing the elements easier. The vector internally keeps track of the number of elements.

Using vectors makes it easier to expand your full or user selected list of cars, no need to hard-code a fixed size.

A rewrite of the code using vectors, including a function to have the user choose a sub-array, will take a bit of time at my end. It is late, and I need some time to mash the code and test it. As well as doing meat world activities.

Tomorrow I should have something whipped up for you if you can wait.
Okay thanks
I tried to mash together the user selection function using fixed sized arrays, and my brain started to melt-down. I know it can be done, but the hoops to jump through are enormous IMO. *OUCH!*

Here's a rewrite of my code using vectors instead of fixed sized arrays so you can get a feel for how they work and what gets changed. The output is the same as before:

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
#include <iostream>
#include <string>
#include <vector>

class car
{
private:
   std::string name;
   int mpg;
   double price;
   int horsepower;

public:
   car(std::string name, int mpg, double price, int horsepower);
   void viewCar();

public:
   friend void viewCars(std::vector<car>& carArray);
   friend void bestMileage(std::vector<car>& carArray);
   friend void lowestPrice(std::vector<car>& carArray);
   friend void greatestHorspower(std::vector<car>& carArray);

};

int main()
{
   std::vector<car> carArray;
   
   carArray = { car("2012 Toyota Scion tC", 26, 10575, 180),
                car("2019 Toyota TUNDRA",   18, 33320, 310),
                car("2017 Chrysler 200",    25, 19000, 295),
                car("2017 Chevy Silverado", 15, 26946, 285),
                car("2007 BMC Envoy",       17, 4500,  291) };

   std::cout << "Car comparisons with full list:\n";

   bestMileage(carArray);
   lowestPrice(carArray);
   greatestHorspower(carArray);

   // let's simulate a user chosen list
   std::vector<car> userList { car("2012 Toyota Scion tC", 26, 10575, 180),
                               car("2017 Chrysler 200",    25, 19000, 295),
                               car("2007 BMC Envoy",       17, 4500,  291) };

   std::cout << "Car comparisons with user list:\n";

   bestMileage(userList);
   lowestPrice(userList);
   greatestHorspower(userList);
}

car::car(std::string name, int mpg, double price, int horsepower)
{
   this->name       = name;
   this->mpg        = mpg;
   this->price      = price;
   this->horsepower = horsepower;
}

void car::viewCar()
{
   std::cout << "Car name: " << name << '\n';
   std::cout << "Car MPG: " << mpg << '\n';
   std::cout << "Car price: " << price << '\n';
   std::cout << "Car horsepower: " << horsepower << '\n';
}

void viewCars(std::vector<car>& carArray)
{
   for (size_t i = 0; i < carArray.size(); i++)
   {
      carArray[i].viewCar();
      std::cout << '\n';
   }
}

void bestMileage(std::vector<car>& carArray)
{
   size_t idx = 0;

   for (size_t i = 1; i < carArray.size(); ++i)
   {
      if (carArray[i].mpg > carArray[idx].mpg)
      {
         idx = i;
      }
   }

   std::cout << "\nThe Vehicle with highest mileage:\n";
   carArray[idx].viewCar();
   std::cout << '\n';
}

void lowestPrice(std::vector<car>& carArray)
{

   size_t idx = 0;

   for (size_t i = 1; i < carArray.size(); ++i)
   {
      if (carArray[i].price < carArray[idx].price)
      {
         idx = i;
      }
   }

   std::cout << "\nThe Vehicle with the lowest price is: \n";
   carArray[idx].viewCar();
   std::cout << '\n';
}

void greatestHorspower(std::vector<car>& carArray)
{
   size_t idx = 0;

   for (size_t i = 1; i < carArray.size(); ++i)
   {
      if (carArray[i].horsepower > carArray[idx].horsepower)
      {
         idx = i;
      }
   }

   std::cout << "\nThe Vehicle with the highest Horse Power is: \n";
   carArray[idx].viewCar();
   std::cout << '\n';
}


Writing the user selection function will be easy now using vectors. Tomorrow. :)
After no coffee and a few brain farts:
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
#include <iostream>
#include <string>
#include <vector>

class car
{
private:
   std::string name;
   int         mpg;
   double      price;
   int         horsepower;

public:
   car(std::string name, int mpg, double price, int horsepower);

public:
   void viewCar() const;

public:
   friend void viewCars(const std::vector<car>&);
   friend void bestMileage(const std::vector<car>&);
   friend void lowestPrice(const std::vector<car>&);
   friend void greatestHorspower(const std::vector<car>&);

   // the long awaited user selection function
   friend std::vector<car> getUserList(const std::vector<car>&, unsigned);
};

int main()
{
   std::vector<car> carArray;
   
   carArray = { car("2012 Toyota Scion tC", 26, 10575, 180),
                car("2019 Toyota TUNDRA",   18, 33320, 310),
                car("2017 Chrysler 200",    25, 19000, 295),
                car("2017 Chevy Silverado", 15, 26946, 285),
                car("2007 BMC Envoy",       17, 4500,  291) };


   // let's simulate a user chosen list
   std::vector<car> userList;

   userList = getUserList(carArray, 3);

   std::cout << "The user list cars:\n";

    // lets show the user list
   viewCars(userList);

   std::cout << "Car comparisons with user list:\n";

   bestMileage(userList);
   lowestPrice(userList);
   greatestHorspower(userList);
}

car::car(std::string name, int mpg, double price, int horsepower)
{
   this->name       = name;
   this->mpg        = mpg;
   this->price      = price;
   this->horsepower = horsepower;
}

void car::viewCar() const
{
   std::cout << "Car name: " << name << '\n';
   std::cout << "Car MPG: " << mpg << '\n';
   std::cout << "Car price: " << price << '\n';
   std::cout << "Car horsepower: " << horsepower << '\n';
}

void viewCars(const std::vector<car>& carArray)
{
   for (const auto& itr : carArray)
   {
      itr.viewCar();
      std::cout << '\n';
   }
}

void bestMileage(const std::vector<car>& carArray)
{
   size_t idx = 0;

   for (size_t i = 1; i < carArray.size(); ++i)
   {
      if (carArray[i].mpg > carArray[idx].mpg)
      {
         idx = i;
      }
   }

   std::cout << "\nThe Vehicle with highest mileage:\n";
   carArray[idx].viewCar();
   std::cout << '\n';
}

void lowestPrice(const std::vector<car>& carArray)
{
   size_t idx = 0;

   for (size_t i = 1; i < carArray.size(); ++i)
   {
      if (carArray[i].price < carArray[idx].price)
      {
         idx = i;
      }
   }

   std::cout << "\nThe Vehicle with the lowest price is: \n";
   carArray[idx].viewCar();
   std::cout << '\n';
}

void greatestHorspower(const std::vector<car>& carArray)
{
   size_t idx = 0;

   for (size_t i = 1; i < carArray.size(); ++i)
   {
      if (carArray[i].horsepower > carArray[idx].horsepower)
      {
         idx = i;
      }
   }

   std::cout << "\nThe Vehicle with the highest Horse Power is: \n";
   carArray[idx].viewCar();
   std::cout << '\n';
}

std::vector<car> getUserList(const std::vector<car>& carArray, unsigned numToChoose)
{
   std::vector<car> userList;

   for (unsigned i = 0; i < numToChoose; i++)
   {
      std::cout << "Make a choice:\n";
      
      for (unsigned j = 0; j < carArray.size(); j++)
      {
         std::cout << j << ": " << carArray[j].name << '\n';
      }
      std::cout << ": ";

      unsigned choice = 0;
      std::cin >> choice;
      std::cout << '\n';
      userList.push_back(carArray[choice]);
   }
   
   return userList;
}
Make a choice:
0: 2012 Toyota Scion tC
1: 2019 Toyota TUNDRA
2: 2017 Chrysler 200
3: 2017 Chevy Silverado
4: 2007 BMC Envoy
: 2

Make a choice:
0: 2012 Toyota Scion tC
1: 2019 Toyota TUNDRA
2: 2017 Chrysler 200
3: 2017 Chevy Silverado
4: 2007 BMC Envoy
: 4

Make a choice:
0: 2012 Toyota Scion tC
1: 2019 Toyota TUNDRA
2: 2017 Chrysler 200
3: 2017 Chevy Silverado
4: 2007 BMC Envoy
: 0

The user list cars:
Car name: 2017 Chrysler 200
Car MPG: 25
Car price: 19000
Car horsepower: 295

Car name: 2007 BMC Envoy
Car MPG: 17
Car price: 4500
Car horsepower: 291

Car name: 2012 Toyota Scion tC
Car MPG: 26
Car price: 10575
Car horsepower: 180

Car comparisons with user list:

The Vehicle with highest mileage:
Car name: 2012 Toyota Scion tC
Car MPG: 26
Car price: 10575
Car horsepower: 180


The Vehicle with the lowest price is:
Car name: 2007 BMC Envoy
Car MPG: 17
Car price: 4500
Car horsepower: 291


The Vehicle with the highest Horse Power is:
Car name: 2017 Chrysler 200
Car MPG: 25
Car price: 19000
Car horsepower: 295


One thing to note for future reference: getUserInput() performs ZERO validation the user makes a valid selection or even a valid numeric input. Enter a number outside the range of carArray or a non-numeric input and the function blows up.
Heh, I really do need coffee and lots of it. The comment at line 40 I forgot to either remove or change.

Another thing to note about getUserInput()....there is no check if the user chooses a previously selected item. While not technically an error it is flawed logic.
Topic archived. No new replies allowed.