//Ryan Shahriyarpour CS1A Carl Argila CH6 HW Q:19
//
/*****************************************************************************
*
* Stock Profit
*
*********************
*
* This program calculates the profit or loss from the sale of a stock using
* the function stockProfit. The formula is:
* Profit = ((NS * SP) - SC) - ((NS * PP) + PC)
* NS = shares, SP = sale price, SC = sale commission,
* PP = purchase price, and PC = purchase commission
*
* INPUT
* shares - number of shares
* purchasePrice - purchase price per share
* purchaseComm - purchase commission paid
* salePrice - sale price per share
* saleComm - sale commission paid
*
* OUTPUT
* The amount of the profit or loss from the sale
*
****************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototype
double stockProfit(int shares, double purchasePrice, double purchaseComm,
double salePrice, double saleComm);
int main()
{
int shares; // Number of shares (NS)
double purchasePrice; // Purchase price per share (PP)
double purchaseComm; // Purchase commission paid (PC)
double salePrice; // Sale price per share (SP)
double saleComm; // Sale commission paid (SC)
cout << "Number of shares: ";
cin >> shares;
cout << "Purchase price per share: $";
cin >> purchasePrice;
cout << "Purchase commission paid: $";
cin >> purchaseComm;
cout << "Sale price per share: $";
cin >> salePrice;
cout << "Sale commission paid: $";
cin >> saleComm;
double profit = stockProfit(shares, purchasePrice, purchaseComm,
salePrice, saleComm);
cout << fixed << setprecision(2);
if (profit >= 0)
cout << "\nThe sale resulted in a PROFIT of $" << profit << endl;
else
cout << "\nThe sale resulted in a LOSS of $" << -profit << endl;
return 0;
}
// Returns profit (positive) or loss (negative) from stock sale
double stockProfit(int shares, double purchasePrice, double purchaseComm,
double salePrice, double saleComm)
{
double proceeds = (shares * salePrice) - saleComm; // Money received
double cost = (shares * purchasePrice) + purchaseComm; // Money spent
return proceeds - cost;
}