Using Sort on a read file.txt function

Hello everyone!

I am trying to use Sort function in my code. So I sort those lines from a file.txt in ascending order according to their price.. And if there is a tie, newer cars come first.

txt file example:
1
2
3
        2001 Ford F-150  $799
	2013 BMW X3  $6900
	2007 Pontiac TA  $21000


And this is what I wrote 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  #include <string>
#include <iostream>
#include <istream>
#include <fstream>

using namespace std;

class usedCars
{
private:
	int Year, Miles;
	string Make, Model;
	float Price;

public:
	void setYear(int yr)
	{
		this-> Year = yr;
	}
	void setMiles(int mil)
	{
		this-> Miles = mil;
	}
	void setMake(string Mk)
	{
		this-> Make = Mk;
	}
	void setModel(string Md)
	{
		this-> Model = Md;
	}
	void setPrice(float Pr)
	{
		this-> Price = Pr;
	}
	int getYear()
	{
		return this-> Year;
	}
	int getMiles()
	{
		return this-> Miles;
	}
	string getMake()
	{
		return this-> Make;
	}
	string getModel()
	{
		return this-> Model;
	}
	float getPrice()
	{
		return this-> Price;
	}
};

int main()
{

	int Year;
	string Make, Model;
	float Price;
	char Dollar;
	int i;
	const int number = 2;
	usedCars Cars[number];
	

	std::ifstream myfile("car.txt");
	if (myfile.is_open())
	{

		while (myfile >> Year >> Make >> Model >> Dollar >> Price)
		{
		
			if(Price < 25000)
			{
				cout << Year << " " << Make << " " << Model << " " << Dollar << Price << endl;
			}
		}
	
	}
	else cout << "Unable to open file";




	system("pause");
	return 0;



}
Last edited on
You're probably best off putting your cars in a std::vector and using the std::sort function, feeding in your own predicate comparison function. Since you're using a class, you can just overload the () operator.

Here's an example. It's a bit complete (I'm bored and on my lunch break) other than a bit of validation here and there, so feel free to ask if there's anything you're unsure about. I added another car to the list, so you can see it shows the newer car first when the prices are the same.

cars.txt
2001 Ford F-150  $799
2013 BMW X3  $6900
2007 Pontiac TA  $21000
2002 Chevrolet Corvette $799


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

struct Car
{
    int year;
    float price;
    std::string make, model;

    bool operator()( const Car &a, const Car &b )
    {
        if( a.price != b.price )
            return a.price < b.price;
        else
            return a.year > b.year;
    }

    void print() const
    {
        std::cout << year << " " << make << " "<< model << ": $" << price << std::endl;
    }
};

/* Helper function. Strips '$' and converts string to float */
float priceToFloat( std::string price )
{
    float ret;
    std::istringstream is( price.substr( 1 ) );
    is >> ret;
    return ret;
}

int main( int argc, const char *argv[] )
{
    std::vector<Car> my_cars;
    Car temp_car;
    std::string line, price_str;
    std::ifstream in( "cars.txt" );

    /* Read car data */
    while( !in.eof() )
    {
        std::getline( in, line );
        std::stringstream ss( line );

        ss >> temp_car.year >> temp_car.make >> temp_car.model >> price_str;
        temp_car.price = priceToFloat( price_str );

        my_cars.push_back( temp_car );
    }

    /* Sort, note third param. Passing object that has our compare function */
    std::sort( my_cars.begin(), my_cars.end(), my_cars[0] );

    /* Print */
    for( const auto &i : my_cars )
        i.print();

    return 0;
}


Output
2002 Chevrolet Corvette: $799
2001 Ford F-150: $799
2013 BMW X3: $6900
2007 Pontiac TA: $21000
Last edited on
Thank you very much for the help! I am really grateful!
Topic archived. No new replies allowed.