I posted about this assignment last night, and am now almost completed. 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
If you have more questions about the same subject, can we ask you to just keep the original Topic going :+). Duplicate Topics can become a time waster for those who reply, the same things are said by different respondents in different topics. If one does a new post in a topic, it will be bumped to the top of the list.