Reading from a file multiple lines.

Hello everyone!

Implement an abstract data type usedCars with private attributes year(integer), make(string), model(string), miles(integer) and price(float), and public operations to set the values and get the values. Write a main program to read the data from the file “car.txt” Select all the cars that are less than $25,000. Print them in ascending order according to price, when there is a tie, the newer car appears first.

The input contains many lines, with one line per car. The first field of a line is the year, followed by the make, model, and price in that order. The fields are separated by at least one blank. The make and model of the car are composed of one word each, with no space allowed, the prices have a $ prefix. For example, the input file could look like
2001 Ford F-150 $799
2013 BMW X3 $6900
2007 Pontiac TA $21000
car.txt:
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
2011 Chevrolet Tahoe $30588 

2000 Plymouth Neon $2400 

2003 Pontiac Vibe $4499 

2001 saturn sky $1650 

1999 Pontiac Bonneville $2300 

2002 Ford F150 $4450 

2006 Mitsubishi Raider $6000 

2002 Ford f-150 $4500 

1998 Infinity QX  $1795 

2009 Saturn Outlook  $14288 

2006 Gmc Envoy $5000 

2005 VW touareg  $9500 

1999 Ford Mustang $1

2008 dodge caravan $5500 

2002 Honda Odyssey $3500 

1999 Oldsmobile Bravada $2895 

2003 Pontiac Montana  $2495 

1994 econoline e250 $1000 

2000 Toyota 4Runner  $6595 

1989 Mustang Notchback $7200 

1999 Ford  f-150  $3400 

2012 Ford Fusion $8400 

2010 ford f150 $25000 

2010 ford supercab $25000 

2003 Chevy Impala $1950 

2000 Dodge  Magnum $3750 

2003 GMC  CREW_CAB $13900 

2004 ford freestar $3600 

1994 GMC Pick-up $1650 

2003 Mazda MPV  $2500 

2001 Ford Explorer $3750 

2000 Oldsmobile Silhouette  $1800 

2001 Ford F150  $3500 

2005 Chevy Monte_Carlo   $10000 

2005 Chevy blazer $4700 

1999 Dodge Caravan  $1900 

2002 Toyota Avalon $3800 

2003 Ford Expidition  $4999 

1998 Ford Explorer $2300 

2002 Ford Explorer $3600 

2005 Chrysler town_n_country $3500 

2006 Acura TL $9300 

2000 Lincoln towncar $2700 

2006 Acura TL3.2  $9300 

2007 dodge charger $8200 

2011 FORD RANGER $23995 

2005 BUICK CENTRY $3600 

1999 CHEVY S10  $4700 

2005 Duramax LLY $15500

2004 Ford Explorer $4300 

2003 Chev BlazerZR2 $4000 

1996 Ford F-150 $1000 

2002 Toyota Echo  $750 

1994 Jeep Wrangler $2900 

2001 JEEP WRANGLER  $6999 


this is what I did so far and I got stuck in how to read multiple lines and what method should be used.

Also, I have a problem with "$Price" because the program prints it differently because it's an approximation. So I would maybe want to read it as a string then convert it to a float.

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
#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()
{
	std::ifstream myfile("car.txt");
	if (myfile.is_open())
	{
		int Year;
		string Make, Model;
		float Price;
		usedCars carsAR[52];
		int i;

		while (myfile.good())
		{
		
			myfile >> Year >> Make >> Model >> Price;
			cout << Year;

		}
	
	}
	else cout << "Unable to open file";
	system("pause");
	return 0;



}
Use this while loop instead:

1
2
3
4
...
char dollar;
while (myfile >> Year >> Make >> Model >> dollar >> Price)
{ ... }
Thank you!

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
int main()
{
	std::ifstream myfile("car.txt");
	if (myfile.is_open())
	{
		int Year;
		string Make, Model;
		float Price;
		char Dollar;
		int i;

		while (myfile >> Year >> Make >> Model >> Dollar >> Price)
		{
		
			cout << Year << " " << Make << " " << Model << " " << Dollar << Price << endl;

		}
	
	}
	else cout << "Unable to open file";
	system("pause");
	return 0;



}


Now I want to know how can I select cars that are less than $25000. What method should I use?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
char dollar;
while (myfile >> Year >> Make >> Model >> dollar >> Price)
{
    ...
    if ( Price < 25000 )
    {
        // do something with this car...
    }
    else
    {
        // ignore this car
    }
}
thank you for the help!

I am trying to write something that will show the newer car first if there is a tie in the price.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...
char dollar;
while (myfile >> Year >> Make >> Model >> dollar >> Price)
{
    ...
    if ( Price < 25000 )
    {
        cout << Year << " " << Make << " " << Model << " " << Dollar << Price << endl;
    }
    else
    {
        return 0;
    }
    if ( ...... )
    {
        // .....
    }
}
Last edited on
Someone already asked this question yesterday the answer is here:
http://www.cplusplus.com/forum/beginner/122731/

So basically you use a container to store the cars then use std::sort from algorithm library to sort them according to the order you want
Thank you!

What confuses me is why would the teacher give this assignment and he wanted us to use set and get methods since it's all reading from a file and printing the info out.
Topic archived. No new replies allowed.