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:
//WARNING: It is expressly forbidden to modify any part of this document, including its name
#pragma once
#include <iostream>
#include <string>
#include <sstream>
usingnamespace 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"
usingnamespace 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>
usingnamespace 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);
}
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!
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?
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.
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;
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.
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!
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).
//WARNING: It is expressly forbidden to modify any part of this document, including its name
#pragma once
#include <iostream>
#include <string>
#include <sstream>
usingnamespace 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"
usingnamespace 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>
usingnamespace 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
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.