classes, with functions

So, I need to know how to display the function, i made a function createCars, now I'm trying to make a function viewCars, and I'm not quite sure how to call a function inside another function or even if you can. Some help would be nice!
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
  #include <iostream>
#include <string>
#include "Source.h"

using namespace std;


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

public:
	void createCars(string name, int mpg, double price, int horsepower);
	void viewCars()
	// feel free to add more atributes
};

int main()
{
	
	void createCars();





	system("pause");
	return 0;
}

void car::createCars(string name, int mpg, double price, int horsepower)
{
	car cars[5];

	cars[0].name = "2012 Toyota Scion tC";
	cars[0].mpg = 26;
	cars[0].price = 10,575;
	cars[0].horsepower = 180;

	cars[1].name = "2019 Toyota TUNDRA";
	cars[1].mpg = 18;
	cars[1].price = 33,320;
	cars[1].horsepower = 310;

	cars[2].name = "2017 Chrysler 200";
	cars[2].mpg = 25;
	cars[2].price = 19,000;
	cars[2].horsepower = 295;

	cars[3].name = "2017 Chevrolet Silverado ";
	cars[3].mpg = 15;
	cars[3].price = 36, 946;
	cars[3].horsepower = 285;

	cars[4].name = "2007 GMC Envoy ";
	cars[4].mpg = 17;
	cars[4].price = 4,500;
	cars[4].horsepower = 291;

	
}
void viewCars()
{
	//i tried void car::createCars();
}
You need to be careful. The comma operator does not do what you think it may do, so we do not represent numbers with more digits by using commas.

You can do this

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

using namespace std;

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

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

int main()
{
  constexpr int SIZE = 5;
  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;
  }
  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;
}



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

Car name: 2019 Toyota TUNDRA
Car MPG: 18
Car price: 33320
Car horsepower: 310

Car name: 2017 Chrysler 200
Car MPG: 25
Car price: 19000
Car horsepower: 295

Car name: 2017 Chevy Silverado
Car MPG: 15
Car price: 26946
Car horsepower: 285

Car name: 2007 BMC Envoy
Car MPG: 17
Car price: 4500
Car horsepower: 291
Okay I understand that, but within that array how do I choose the cars to compare since it isnt like car[0], car[1], like if I wanted to compare find best gas mileage, find lowest price find greatest horsepower, etc of lets say 2 cars out of the list
First you need access to the mpg member. I took the easy way out and just made it public.

Then you must compare the MPG of each car:

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

using namespace std;

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

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

int main()
{
  constexpr int SIZE = 5;
  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;
  }

  // Find the index of the car with the highest mileage
  size_t idx = 0;		// assume that carArray[0] has highest mileage
  for (size_t i=1; i<SIZE; ++i) { // note loop starts at 1, not 0
      if (carArray[i].mpg > carArray[idx].mpg) {
	  idx = i;
      }
  }
  cout << "\nCar with highest mileage:\n";
  carArray[idx].viewCars();
  cout << '\n';
  
  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;
}

trying to keep it private, I tried to incorporate the viewCars method, but its showing the car with best mileage like 5 times.

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
#include <iostream>
#include <string>
#include "Source.h"

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();
	
};

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;
	}

	
	for (size_t i = 0; i < SIZE; i++) {
		carArray[i].bestMileage();
		cout << endl;
	}
	
	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 << "\nCar with highest mileage:\n";
	carArray[idx].viewCars();
	cout << '\n';

}
figured it out
Okay, So i have the program working fully, I just need the someone to compare 3 of the 5 cars that has the best mpg, horsepower, and price.. Not out of all 5.
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
#include <iostream>
#include <string>
#include "Source.h"

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 << "\nCar 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 Car 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';
}
not sure what you really want here.
lets compare HP, though... since you used arrays, we are sort of painted into a corner, but let me show you some almost there pseudocode...

car* cp[3]; //build a list of what you want to compare, in this example, these 3 cars...
cp[0]= &carArray[3];
cp[1] = &carArray[1];
cp[2] = &carArray[4];

greatestHorspower_sublist(car** c, int num) //and a function to compare them
{
for(int x = 0; x < num; x++)
if(blah blah)
biggest = *c[x].horsepower;
}



Last edited on
I want to give the user the chance to pick 3 vehicles out of the 5, then display which one has the best Horse power, Mileage, and Price
ok. Then what I showed you is the general approach you can use that can shoehorn into what you have.

the user picks the cars (I hard coded the assignment).
then you need new versions of your comparison functions (or a re-write of the old ones that have the parameters you need).

I recommend the re-write. You re-create the 5 cars in each function, and that is making your code very difficult to get right (its a big design flaw). Take those out of the functions and back into the class, if you want to do the rewrite fix approach...
Last edited on
damn. hate to hear it having to rewrite lol
sorry :( We all hate that, but its one way we learn things. Everyone here has at one point or another had some design that went totally wrong and had to be redone.

It isnt a total loss. Just do one thing at a time... move the data back into the class, rewrite one of the comparisons to work when you pass in a list of what to compare (start by just passing in the whole list of cars but keep in mind that soon it will need to be a partial list, and because of that, the parameters need to be something like what I showed, or some similar alternative -- if the pointer soup turns you off, you can make an array of bool and set the ones you want to process to true, the rest to false, and pass that around...). Make a fix, test it... repeat, its only 4 or 5 major changes.

if you want an even better approach, make 2 classes. the car and then something to manage a container (array, in this case) of cars. this isnt obvious why its better, until you have done it the other way... trying to have a class manage a container of itself is doable but awkward. Having a simple class that just holds data and lets you set/get/etc simple stuff and using it as a building block gets rid of a lot of the awkwardness. You will see this soon -- it takes a little while to really understand how to break things up.
Last edited on
The pain of having to rewrite code over and over again as it gets expanded/modified helps reinforce the habit of designing the basic layout of the software before a single line of code is written.
Topic archived. No new replies allowed.