1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
bool ReadFile (ifstream&, string);
float ProfitFunction (ifstream&, string, string, int&, float&, float&, float&, float&, float&, int&);
int main ()
{
ifstream inFile;
string filename = "Shares.txt";
bool IsItOpen = true;
string StockName;
int NUMofShares, TotalShares = 0;
float PurchasePrice, SalePrice, PurchaseCommission, SaleCommission, ProfitOrLoss, TotalProfitOrLoss = 0;
cout << "Please open the file associated with the Multiple Stock Sales.\n" << endl;
inFile.open(filename);
IsItOpen = ReadFile(inFile, filename);
if (IsItOpen == false)
{
cout << "Closing down the program due to file error..." << endl;
return -1;
}
inFile.close();
TotalProfitOrLoss = ProfitFunction (inFile, filename, StockName, NUMofShares, PurchasePrice, SalePrice,
PurchaseCommission, SaleCommission, ProfitOrLoss, TotalShares);
cout << "\nTotal Profit (Loss) for " << TotalShares << " shares = $ " << TotalProfitOrLoss << endl;
system("pause");
return 0;
}
bool ReadFile (ifstream& myFile, string NameOfFile)
{
cout << "Type the name of the file." << endl;
cout << "Example Format: StockPrices.txt" << endl;
cout << "\nPlease Enter Here: ";
cin >> NameOfFile; //prompting user to enter the filename
if (myFile.is_open() && NameOfFile == "Shares.txt")//validating the filename that was entered
{
cout << NameOfFile << " has opened successfully.\n" << endl;
return true;
}
else //if an error occurred opening the file, the program will close
return false;
}
float ProfitFunction (ifstream& inFile, string filename, string StockName,
int& NUMofShares, float& PurchasePrice, float& SalePrice,
float& PurchaseCommission, float& SaleCommission, float& ProfitOrLoss, int& TotalShares)
{
float TotalProfit = 0;
cout << "Stock " << setw(5) << " Shares " << setw(10) << " P Price " << setw(10);
cout << " S Price " << setw(10) << " Profit (Loss)" << endl;
cout << "_____ " << setw(5) << " ______ " << setw(10) << " _______ " << setw(10);
cout << " _______ " << setw(10) << " _____________" << endl;
inFile.open(filename);
while (!inFile.eof())
{
inFile >> StockName;
inFile >> NUMofShares;
inFile >> PurchasePrice;
inFile >> SalePrice;
inFile >> PurchaseCommission;
inFile >> SaleCommission;
if(!inFile) break; // Checks if file operation is a success
//Formula for calculating Profit/Loss
ProfitOrLoss = ((NUMofShares * SalePrice) - SaleCommission) - ((NUMofShares * PurchasePrice) + PurchaseCommission);
cout << left << StockName << ' ';
cout << setw(9) << right << NUMofShares << ' ';
cout << fixed << setprecision(2);
cout << setw(9) << right << PurchasePrice << ' ';
cout << setw(9) << right << SalePrice << ' ';
cout << setw(14) << right << ProfitOrLoss << endl;
TotalShares += NUMofShares;
TotalProfit += ProfitOrLoss;
}
inFile.close();
return TotalProfit;
}
|