Car comparison program

I need to make this program have a counter controlled while loop, it needs to compare a certain number of cars that the user enters and output the information shown. I know how to make the loop run for a certain number of inputs I specify, but not that the user does. this is the code I have so far-

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

#include <iostream>
#include <string>
using namespace std;

int main()
{
	const	int	LONG_DISTANCE_MILES = 500;

	string	carManufacture,
		carModel;

	int		currentYear,
		carYear,
		carAge,
		carTankCapacity,
		carsCompared, //input 
		numOfCarsCompared, //counter
		totalNumCarsCompared, //accumulator 
		carMPG;

	double	gasPrice,
		carCruiseRange,
		carCostLongDrive;
	cout << "How many cars would you like to compare? " << "Number of cars: ";
	cin >> carsCompared;

	while (carsCompared != 0);
	{

		cout << "Vehicle Comparison Report\n\n";
		cout << "Please enter the vehicle\'s manufacturer: ";
		cin >> carManufacture;
		cout << "\nPlease enter the vehicle model: ";
		cin >> carModel;
		cout << "\nEnter the current year: ";
		cin >> currentYear;
		cout << "\nEnter the vehicle\'s year of manufacture: ";
		cin >> carYear;
		cout << "\nEnter the vehicle\'s listed MPG: ";
		cin >> carMPG;
		cout << "\nEnter the vehicle's gas tank capacity: ";
		cin >> carTankCapacity;
		cout << "\nWhat is the current gasoline price in your area? $";
		cin >> gasPrice;



		carAge = currentYear - carYear;
		carCruiseRange = carTankCapacity * carMPG;
		carCostLongDrive = static_cast<double>((LONG_DISTANCE_MILES) / static_cast<double>(carMPG)) * gasPrice;


		system("cls");	// clears the screen and moves the cursor to the upper
						// left hand corner
		cout << "Vehicle Comparison Report\n\n";
		cout << "\tVehicle under consideration: " << carManufacture << " " << carModel;
		cout << "\n\n\tAge of vehicle: " << carAge;
		cout << "\n\t Vehicle gas tank capacity: " << carTankCapacity;
		cout << "\n\tVehicle cruising range: " << carCruiseRange;
		cout << "\n\tCurrent gasoline price: $" << gasPrice;
		cout << "\n\tCost to drive 500 miles based on MPG and gas price: " << carCostLongDrive << "\n\n";

		carsCompared++;


	}
	return 0;
}
 
closed account (1vf9z8AR)
if i understand you completely you want the loop to run as many times as the value of cars compared.

1
2
3
4
5
6
int counter=0;
cin>>carscompared;
while(counter<carscompared)
{
counter++;
}
Last edited on
Do you want the user to enter more than one car and later compare those car with each other?
If so, you need to save the respective data of each car at each iteration and later, after the loop finishes, compare them.
Perhaps you should describe better what you want to do.
Topic archived. No new replies allowed.