Used Car Dealership, Help

I'm working on an assignment that uses classes to help my "uncle" keep track of the inventory in his dealership. He starts with $10,000 and no cars. In the main menu I have options of being able to buy/sell cars, and then those cars are supposed to be added/or removed from my inventory. However, I am not able to figure out how to get the user input (stating the car name, color, and price) stored into the inventory.
I'm working exclusively within the int main() section.
Any help is greatly appreciated!
Here's the code that I have:

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  //WARNING: It is expressly forbidden to modify any part of this document, including its name
#pragma once
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class Car
{
private:
	string name;
	string color;
	double price;

public:
	//---------------------------------------------------------------------------------------
	/*
	* Constructor/Destructor
	*
	* Handles creation and deletion of Car objects.
	*
	* Parameter: name_in
	*		The name of a new car
	* Parameter: color_in
	*		The color of a new car
	* Parameter: price_in
	*		The price of a new car
	*/
	Car(string name_in, string color_in, double price_in);
	virtual ~Car();
	//---------------------------------------------------------------------------------------
	/*
	* getName
	*
	* Returns the name of the car.
	*
	* Return:
	*		The name of the car
	*/
	string getName();
	/*
	* getColor
	*
	* Returns the color of the car.
	*
	* Return:
	*		The color of the car
	*/
	string getColor();
	/*
	* getPrice
	*
	* Returns the price of the car.
	*
	* Return:
	*		The price of the car
	*/
	double getPrice();
	//---------------------------------------------------------------------------------------
	/*
	* paint
	*
	* Paints the car a new color and increases the price by $1,000.
	*
	* Parameter: new_color
	*		The color of paint to be used on the car
	*/
	void paint(string new_color);
	//---------------------------------------------------------------------------------------
	/*
	* toString
	*
	* Returns a single string containing useful information about the car.
	*
	* Return:
	*		A data string about this car
	*/
	string toString();
	//-------------------------------------------------------------------
};


//WARNING: It is expressly forbidden to modify any part of this document, including its name
#include "car.h"
using namespace std;

//---------------------------------------------------------------------------------------
Car::Car(string name_in, string color_in, double price_in)
{
	name = name_in;
	color = color_in;
	price = price_in;
}
Car::~Car() {}
//---------------------------------------------------------------------------------------
string Car::getName()
{
	return name;
}
string Car::getColor()
{
	return color;
}
double Car::getPrice()
{
	return price;
}
//---------------------------------------------------------------------------------------
void Car::paint(string new_color)
{
	color = new_color;
	price += 1000;
}
//---------------------------------------------------------------------------------------
string Car::toString()
{
	stringstream ss;
	ss << "Name: " << name << endl;
	ss << "Color: " << color << endl;
	ss << "Price: $" << price << endl;
	return ss.str();
}
//---------------------------------------------------------------------------


#include <iostream>
#include "car.h"
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

int main()
{
	int option;
	string name;
	string color;
	double price = 0;
	double balance = 10000;
	Car cars(name, color, price);

	do
	{
		cout << "Please select an option:" << endl;
		cout << "1 - Show current inventory." << endl;
		cout << "2 - Show current balance." << endl;
		cout << "3 - Buy a car." << endl;
		cout << "4 - Sell a car." << endl;
		cout << "5 - Paint a car." << endl;
		cout << "6 - Load file." << endl;
		cout << "7 - Save file." << endl;
		cout << "8 - Quit program." << endl;
		cout << "\n";

		cin >> option;

		if (option == 1)
		{
			cout << cars.toString();
		}
		if (option == 2)
		{
			cout << "$" << balance << endl;
		}
		if (option == 3)
		{
			cout << "Please enter the name, color, and price of the car you would like to buy." << endl;
			cin >> name;
			cin >> color;
			cin >> price;

			if (price <= balance)
			{
				cout << "Car has been purchased." << endl;
				balance = balance - price;
			}
			else
			{
				cout << "You cannot afford that car." << endl;
				option = 0;
			}
		}
		if (option == 4)
		{
			cout << "Please enter the name of the car you want to sell." << endl;
			cin >> name;
			balance = balance + price;
		}
		if (option == 5)
		{

		}
		if (option == 6)
		{
			ifstream fin;
			fin.open("cars1.txt");
			
		}
		if (option == 7)
		{
			ofstream fout;
			fout.open("Lab 8.txt");
			fout << cars.toString() << endl;
			fout << "$" << balance << endl;
		}
		if (option == 8)
		{
			exit(0);
		}
		if (option < 0 || option > 8)
		{
			cout << "Please enter a number from 1-8." << endl;
		}

		cout << "\n";
	} while (option < 8 || option > 8);
}
I suggest creating another constructor...a default one,like this

 
Car() {}


Then at line 142,call this constructor.
When you hav taken the input from memory at lines 170,171 and 172, put this at the next line

 
cars.(name, color, price);


Then i suppose you could hav taken caree of your problem. :D
Is there a way to do it without creating a default constructor? I'm not allowed to edit the class header or the functions from what they are above. Thank you for your reply though!
The problem is tht in line 142,you hav constructed the Car object with parameters which dont hav any value.
But if main.cpp can be changed,then

In line 142,
1
2

Car  *cars;


and after line 172,this

1
2
3

cars=new Car(name,color,price);


dont forget to put delete cars; before return statement,ie, return 0 .

Rejoice!!!
Why the pointer? Your code only allows one Car in the inventory, if you try to add a second car you have a memory leak.

Wouldn't a vector<Car> be a better alternative?

Then 142 would become:

std::vector<Car> inventory;
Instead of just a just a single instance of a Car, or a pointer to a Car.

and line 182 would become:

inventory.push_back(Car(name, color, price);

No need to manually create a single instance of the class on line 172 nor worry about deleting the manually allocated memory.




Thank you so much! That is a ton of help. I did, however, run into one more problem when I added your suggestions. In lines 161 and 205 I was using the old constructor to call the function that showed the inventory. Would you mind explaining how I call that same function with the default constructor?
What exactly is the problem?

I need to be able to have the program store the information I input for name, color, and price of a car so that I can look it up in the inventory via option 1 (using the toString() function). Right now I'm stuck so that I can store it, but can't view it. Or I can't store, but can access the empty inventory.
What changes have you made to your code? Did you go with the pointer or the vector?

I am currently using the pointer. I tried the vector but it was coming up with an error message saying that the vector wasn't supported by namespace std;
Did you include the <vector> header file?

The pointer, as used in by MaBunny, only allocates memory for one car so you'll need to allocate enough memory for several cars if you want an inventory.



line 142: That line only allocates a single car. You need a collection of cars. As jlb suggested, a std::vector would be appropriate. Although a std::map would make it easier to look up cars by name.

line 161: That line outputs only a single car. As stated above, you need a collection of cars.

The following should help 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include "car.h"
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
using namespace std;

class Dealer
{   vector<Car>     cars;
    double          balance;

public:
    Dealer ();
    
    void show_inventory ();
    void show_balance ();
    void buy_car ();
    vector<Car>::iterator FindCarByName (const string & car_name);
    void sell_car ();
    void paint_car ();
    void load_file ();
    void save_file ();
};    

Dealer::Dealer ()
{   balance = 10000;
}
        
void Dealer::show_inventory ()
{   for (unsigned i=0; i<cars.size(); i++)
        cout << cars[i].toString () << endl; 
}

void Dealer::show_balance ()
{   cout << "$" << balance << endl;
}

void Dealer::buy_car ()       
{   string name;
	string color;
	double price;
	
	cout << "Please enter the name, color, and price of the car you would like to buy." << endl;
	cin >> name;
	cin >> color;
	cin >> price;
	if (price > balance)
	{   cout << "You cannot afford that car." << endl;
	    return;
	}
	cout << "Car has been purchased." << endl;
	balance = balance - price;
	cars.push_back (Car(name,color,price));
}

vector<Car>::iterator Dealer::FindCarByName (const string & car_name)
{   vector<Car>::iterator   car_iter;

    //  Find car in factor, return iterator
    return car_iter;
}

void Dealer::sell_car ()
{   string  name;
    vector<Car>::iterator   car_iter;
    
    cout << "Please enter the name of the car you want to sell." << endl;
    cin >> name;
    car_iter = FindCarByName (name);    
	balance = balance + car_iter->getPrice();
	cars.erase (car_iter);
}

void Dealer::paint_car ()
{   string name;
    string new_color;
    vector<Car>::iterator   car_iter;
    
    cout << "Please enter the name of the car you want to paint." << endl;
    cin >> name;
    cout << "Please enter the new color: ";
    cin >> new_color;
    car_iter = FindCarByName (name);
    car_iter->paint (new_color);
    balance += 1000;
}

void Dealer::load_file ()
{   cars.clear();
    //  Read file TBD
}

void Dealer::save_file ()
{   //  TBD
}
          
int main()
{   int option;
	Dealer      dealer;     
	
	while (true)
	{   cout << "Please select an option:" << endl;
		cout << "1 - Show current inventory." << endl;
		cout << "2 - Show current balance." << endl;
		cout << "3 - Buy a car." << endl;
		cout << "4 - Sell a car." << endl;
		cout << "5 - Paint a car." << endl;
		cout << "6 - Load file." << endl;
		cout << "7 - Save file." << endl;
		cout << "8 - Quit program." << endl;
		cout << "\n";
		cin >> option;
		switch (option) 
		{
		case 1: dealer.show_inventory ();		
		        break;
		case 2: dealer.show_balance ();		            
		        break;
        case 3: dealer.buy_car ();
                break;		        
        case 4: dealer.sell_car ();
                break;
        case 5: dealer.paint_car ();
                break;
        case 6: dealer.load_file ();
                break;
        case 7: dealer.save_file ();
                break;                
        case 8: return 0;
		default: cout << "Please enter a number from 1-8." << endl;
		}
		cout << "\n";
	} 
}
Last edited on
Just realized I didn't #include <vector>, which is why that wasn't working before. But that is working and I'm now able to buy, sell, and paint my cars. Thank you all so much for your help!
Last edited on
I am now almost done. I just have one more thing that i need to figure out. On option 6 of my main menu, I'm supposed to be able to upload some .txt files that were given to me as part of the assignment (I'll include them below). I don't just need to output the next, but I actually need to have the text added into my car inventory in the appropriate parts.
I'm not allowed to touch the class header or the functions. I'm working exclusively within main(), and focused on the if(option == 6) portion.

Here is my code so far, including the class header and functions (which are stored in different files in my compiler).

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  //WARNING: It is expressly forbidden to modify any part of this document, including its name
#pragma once
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class Car
{
private:
	string name;
	string color;
	double price;

public:
	//---------------------------------------------------------------------------------------
	/*
	* Constructor/Destructor
	*
	* Handles creation and deletion of Car objects.
	*
	* Parameter: name_in
	*		The name of a new car
	* Parameter: color_in
	*		The color of a new car
	* Parameter: price_in
	*		The price of a new car
	*/
	Car(string name_in, string color_in, double price_in);
	virtual ~Car();
	//---------------------------------------------------------------------------------------
	/*
	* getName
	*
	* Returns the name of the car.
	*
	* Return:
	*		The name of the car
	*/
	string getName();
	/*
	* getColor
	*
	* Returns the color of the car.
	*
	* Return:
	*		The color of the car
	*/
	string getColor();
	/*
	* getPrice
	*
	* Returns the price of the car.
	*
	* Return:
	*		The price of the car
	*/
	double getPrice();
	//---------------------------------------------------------------------------------------
	/*
	* paint
	*
	* Paints the car a new color and increases the price by $1,000.
	*
	* Parameter: new_color
	*		The color of paint to be used on the car
	*/
	void paint(string new_color);
	//---------------------------------------------------------------------------------------
	/*
	* toString
	*
	* Returns a single string containing useful information about the car.
	*
	* Return:
	*		A data string about this car
	*/
	string toString();
	//---------------------------------------------------------------------------------------
};


//WARNING: It is expressly forbidden to modify any part of this document, including its name
#include "car.h"
using namespace std;

//---------------------------------------------------------------------------------------
Car::Car(string name_in, string color_in, double price_in)
{
	name = name_in;
	color = color_in;
	price = price_in;
}
Car::~Car() {}
//---------------------------------------------------------------------------------------
string Car::getName()
{
	return name;
}
string Car::getColor()
{
	return color;
}
double Car::getPrice()
{
	return price;
}
//---------------------------------------------------------------------------------------
void Car::paint(string new_color)
{
	color = new_color;
	price += 1000;
}
//---------------------------------------------------------------------------------------
string Car::toString()
{
	stringstream ss;
	ss << "Name: " << name << endl;
	ss << "Color: " << color << endl;
	ss << "Price: $" << price << endl;
	return ss.str();
}
//---------------------------------------------------------------------------------------


#include <iostream>
#include "car.h"
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
	int option;
	string name;
	string color;
	double price = 0;
	double balance = 10000;
	vector <Car> cars;

	do
	{
		cout << "Please select an option:" << endl;
		cout << "1 - Show current inventory." << endl;
		cout << "2 - Show current balance." << endl;
		cout << "3 - Buy a car." << endl;
		cout << "4 - Sell a car." << endl;
		cout << "5 - Paint a car." << endl;
		cout << "6 - Load file." << endl;
		cout << "7 - Save file." << endl;
		cout << "8 - Quit program." << endl;
		cout << "\n";

		cin >> option;

		if (option == 1)
		{
			for (int i = 0; i < cars.size(); i++)
			{
				cout << cars[i].toString();
			}
		}
		if (option == 2)
		{
			cout << "$" << balance << endl;
		}
		if (option == 3)
		{
			cout << "Please enter the name, color, and price of the car you would like to buy." << endl;
			cin >> name;
			cin >> color;
			cin >> price;
			Car newcar(name, color, price);

			if (price <= balance)
			{
				cars.push_back(newcar);
				cout << "Car has been purchased." << endl;
				balance = balance - price;
			}
			else
			{
				cout << "You cannot afford that car." << endl;
				option = 0;
			}
		}
		if (option == 4)
		{
			cout << "Please enter the name of the car you want to sell." << endl;
			cin >> name;
			bool found = true;

			for (int j = 0; j < cars.size(); j++)
			{
				if (cars[j].getName() == name)
				{
					found = false;
					price = cars[j].getPrice();
					cars.erase(cars.begin() + j);
					cout << "You have sold the " << name << endl;
					balance = balance + price;
				}
				else
				{
					cout << "You don't own that car." << endl;
				}
			}
		}
		if (option == 5)
		{
			cout << "Please enter the name of the car you want to paint." << endl;
			cin >> name;
			bool found = true;

			for (int j = 0; j < cars.size(); j++)
			{
				if (cars[j].getName() == name)
				{
					found = false;
					cout << "What color do you want to paint the " << name << "?" << endl;
					cin >> color;
					cars[j].paint(color);
					cout << "You have painted the " << name << " " << color << "." << endl;
				}
				else
				{
					cout << "You don't own that car." << endl;
				}
			}
		}
		if (option == 6)
		{
			string chooseFile;
		}
		if (option == 7)
		{
			for (int i = 0; i < cars.size(); i++)
			{
				ofstream fout;
				fout.open("Lab 8.txt");
				fout << cars[i].toString() << endl;
				fout << "$" << balance << endl;
			}
		}
		if (option == 8)
		{
			exit(0);
		}
		if (option < 0 || option > 8)
		{
			cout << "Please enter a number from 1-8." << endl;
		}

		cout << "\n";
	} while (option < 8 || option > 8);
}


Here is the content of the text files I'm supposed to be able to upload.
1
2
3
4
529.23
Jalopy Blue 3402.99
Rustbucket Brown 44.99
Lemon Yellow 4226.99

1
2
3
4
5
6
7
8
9
59715.43
Navy White 6972.15
Kidney Red 3971.15
Refried Brown 9999.99
Garbanzo White 975.21
Black Black 7946.85
Edamame Green 555.55
Lima White 5873.15
Pinto Brown 12368.99
Last edited on
Hi,

I was going to say this:

Look carefully at the code provided by AbstractionAnon especially the switch, see how each case calls a member function? See how tidy and more organised that is? One basic interpretation of what a class is: It contains data, and functions which operate on that data.

But then I saw the requirements :+(

It a shame that teachers are forcing students into doing precisely the wrong thing. It is what it is I suppose ....

Edit: I wonder if you are allowed to create your own stand alone functions that each case can call, that would be more tidy at least.

Here is the content of the text files I'm supposed to be able to upload.


You know how to read from an input stream like std::cin right? It's the same with an input stream that is a file.
Last edited on
Topic archived. No new replies allowed.