In: Computer Science
Write a program that does the following in C++
1 ) Write the following store data to a file (should be in main)
DC Tourism
Expenses 100.20
Revenue 200.50
Maryland Tourism
Expenses 150.33
Revenue 210.33
Virginia Tourism
Expenses 140.00
Revenue 230.00
2 ) Print the following heading: (should be in heading function)
Store name | Profit
[Note: use setw to make sure all your columns line up properly]
3 ) Read the store data for one store (should be in main)
4 ) Calculate the profit for the store (should be in getProfit function)
5 ) Print the store information in the following format (should be in printMessage function)
<Store name> | <Profit>
[Note: use setw to make sure all your columns line up properly]
6 ) Repeat steps 3-5 in an end of file loop [should be in main]
Your answer should look like this:
Store name Profit
DC Tourism 100.30
Maryland Tourism 60.00
Virginia Tourism 90.00
Functions you need
Function name | Parameters | Return Type
heading | N/A | void
getProfit | expenses,revenue | float
PrintMessage | storeName1, profit | void
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include<vector>
#include<sstream>
#include<string>
using namespace std;
class Record
{
private:
string name;
double expenses, revenue;
public:
Record()
{
this->name = "";
this->expenses =
this->revenue = 0.0;
}
Record(string name, double expenses, double
revenue)
{
this->name = name;
this->expenses = expenses;
this->revenue = revenue;
}
string getName() { return name; }
double getExpenses() { return expenses; }
double getRevenue() { return revenue; }
double getProfit() { return(revenue - expenses);
}
};
// FUNCTION PROTOTYPES
void heading();
void PrintMessage(string storeName, double profit);
int main()
{
string filename = "data.txt";
string line;
vector<Record> records;
ifstream inFile(filename.c_str());
if (!inFile.is_open())
{
cout << "Error in opening
file " << filename << "! Exiting..\n";
exit(EXIT_FAILURE);
}
// read input file and populate array of records
while (getline(inFile, line))
{
string name = line;
// expenses
getline(inFile, line);
stringstream ss1(line);
vector<string> tokens1;
string token1;
double expense = 0.0;
while (getline(ss1, token1, '
'))
{
tokens1.push_back(token1);
}
string title = tokens1[0];
expense = stod(tokens1[1]);
// revenue
getline(inFile, line);
stringstream ss2(line);
vector<string> tokens2;
string token2;
double revenue = 0.0;
while (getline(ss2, token2, '
'))
{
tokens2.push_back(token2);
}
title = tokens2[0];
revenue = stod(tokens2[1]);
records.push_back(Record(name,
expense, revenue));
}
inFile.close();
// display the heading
heading();
// print the rows
for (Record r : records)
PrintMessage(r.getName(),
r.getProfit());
cout << endl;
return 0;
}
void heading()
{
cout << setw(20) << left <<
"STORENAME" << setw(1) << left << "PROFIT"
<< endl;
}
void PrintMessage(string storeName, double profit)
{
cout << setw(20) << left <<
storeName << setw(1) << left << "$" <<
setprecision(2) << fixed << profit << endl;
}
******************************************************** SCREENSHOT ********************************************************
INPUT FILE (data.txt) - This file needs to be created before running the code and this file should be created within the same project directory.
CODE SCREENSHOTS :
CONSOLE OUTPUT :