Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do I make my code read a text file?

+2 votes
asked Nov 6, 2023 by Jake Lisk (140 points)

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;

}

2 Answers

0 votes
answered Nov 30, 2023 by SRINATH S (150 points)
file=fopen("file_name",'r');
0 votes
answered Nov 30, 2023 by Peter Minarik (86,240 points)

Is the file right next to your executable? You can add a new file to your project called car_sales_data.txt and fill it with data or just upload your existing file. Use the buttons left to the RUN button.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...