So I need to sort all of the getPrice array values in order by price in ascending order. The ones I need to cout are supposed to be under $25,000. I need to cout them in order of price too.
The thing is, to sort the cells, I have to set them to a temporary value, because obviously,
1 2
|
array[1]=array[2]
array[2]=array[1]
|
This does not work. I am using a temporary value instead:
1 2 3
|
temp=array[1]
array[1]=array[2]
array[2]=temp
|
The thing is, when I use cars[i].getPrice()
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
|
temp= cars[i].getPrice(); cars[i].getPrice() = cars[j].getPrice(); cars[j].getPrice() = temp;
[/code[
It is not modifiable. I understand the error, I just don't know what to do to fix it. Do I have to make a new variable for each cell in the array?
What should I do to cout them in order of price? I already have the rest of my code done:
[code]
#include <iostream>
#include <string>
#include <sstream>
#include "car.h"
using namespace std;
const int number=2;
const int maxPrice=25000;
int main()
{
int a;
string b; string c;
float d; float e;
car cars [number];
int i, j;
int temp;
for(i=0; i<number; i++)
{
cout << "Enter the YEAR of the car: ";
cin >> a;
cars[i].setYear(a);
cout << "Enter the MAKE of the car: ";
cin >> b;
cars[i].setMake(b);
cout << "Enter the MODEL of the car: ";
cin >> c;
cars[i].setModel(c);
cout << "Enter the number of MILES on the car: ";
cin >> d;
cars[i].setMiles(d);
cout << "Enter the PRICE of the car: ";
cin >> e;
cars[i].setPrice(e);
cout << " " << endl;
}
cout << "Cars listed under $25,000: " << endl;
for(int i=0; i<number; i++) //For loop to test all car prices.
{
if(cars[i].getPrice() < maxPrice) //Finds all under $25,000.
{
for (i=0; i<(number-1); i++) //Comparing the first cell...
{
for(j=(i+1); j<number; j++) //To the rest of the cells.
{
if (cars[i].getPrice() < cars[j].getPrice) // descending order
{
temp= cars[i].getPrice(); // swap
cars[i].getPrice() = cars[j].getPrice();
cars[j].getPrice() = temp;
}
}
}
cout << "Year: " << cars[i].getYear() << endl;
cout << "Make: " << cars[i].getMake() << endl;
cout << "Model: " << cars[i].getModel() << endl;
cout << "Miles: " << cars[i].getMiles() << endl;
cout << "Price: $" << cars[i].getPrice() << endl;
cout << " " << endl;
cout << "This is a " << cars[i].getYear() << " " << cars[i].getMake() << " " << cars[i].getModel() << " with " << cars[i].getMiles() << " miles on it, for $" << cars[i].getPrice() << "." << endl;
cout << " " << endl;
}
}
system("pause");
};
|
I totally understand why it is not modifiable, it's because I am getting that value from a single source. It's like saying we have 10 apples, but telling people we have 15. It just doesn't work. So how can I modify it? I just need to order the array cells in ascending order and then cout them.
EDIT: Also, what is a simple way to compare all cells to find the same values? If there is a tie in price, I need to cout the newer car first. So I need to find the ones that are the same, and then I can simply use the same algorithm that I used to order the price.
Thanks for the help.