So i made a file called car_sales_data.txt. And I have no clue why it won't read the file here is my code. I am on Mac.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
const int MAX_SALESPEOPLE = 6;
const int MAX_CARS = 20;
class Car
{
public:
string make;
string model;
int year;
double costPrice;
double salePrice;
};
class Salesperson
{
public:
string firstName;
string lastName;
vector < Car > carsSold;
Salesperson (string first, string last):firstName (first), lastName (last)
{
}
void addCar (Car car)
{
carsSold.push_back (car);
}
double calculateTotalProfit ()
{
double totalProfit = 0.0;
for (const Car & car:carsSold)
{
totalProfit += car.salePrice - car.costPrice;
}
return totalProfit;
}
};
int
main ()
{
Salesperson *salespeople[MAX_SALESPEOPLE];
string salespersonNames[MAX_SALESPEOPLE] = {
"John Smith",
"Emily Johnson",
"Michael Davis",
"Laura Wilson",
"Daniel Brown",
"Sophia Lee"
};
// Initialize the Salesperson objects using dynamic memory allocation
for (int i = 0; i < MAX_SALESPEOPLE; i++)
{
salespeople[i] =
new Salesperson (salespersonNames[i].
substr (0, salespersonNames[i].find (' ')),
salespersonNames[i].substr (salespersonNames[i].
find (' ') + 1));
}
// Open the master sequential file for reading
ifstream inputFile ("car_sales_data.txt");
if (!inputFile)
{
cerr << "Error opening file." << endl;
return 1;
}
// Read data from the master file and split it into salespeople
string firstName, lastName, make, model;
int year;
double costPrice, salePrice;
while (inputFile >> firstName >> lastName >> make >> model >> year >>
costPrice >> salePrice)
{
Car car;
car.make = make;
car.model = model;
car.year = year;
car.costPrice = costPrice;
car.salePrice = salePrice;
for (int i = 0; i < MAX_SALESPEOPLE; i++)
{
if (salespersonNames[i] == firstName + " " + lastName)
{
salespeople[i]->addCar (car);
break;
}
}
}
// Create a report file for each salesperson
for (int i = 0; i < MAX_SALESPEOPLE; i++)
{
if (!salespeople[i]->carsSold.empty ())
{
string filename = salespeople[i]->lastName + ".txt";
ofstream reportFile (filename);
reportFile << "Salesman Name: " << salespeople[i]->
firstName << " " << salespeople[i]->lastName << endl;
reportFile << "Auto Make and Model\tAuto Year\tSale Price\tProfit"
<< endl;
for (const Car & car:salespeople[i]->carsSold)
{
double profit = car.salePrice - car.costPrice;
reportFile << car.make << " " << car.model << "\t" << car.
year << "\t$" << car.salePrice << "\t$" << profit << endl;
}
double totalProfit = salespeople[i]->calculateTotalProfit ();
reportFile << "Total profit generated by this salesperson: $" <<
totalProfit << endl;
reportFile.close ();
}
}
// Deallocate the dynamically allocated Salesperson objects
for (int i = 0; i < MAX_SALESPEOPLE; i++)
{
delete salespeople[i];
}
return 0;
}