//Dione Torrevillas CS1A Chapter 6, P. 374, #19
//
/****************************************************************
*
* CALCULATE STOCK PROFIT
*_______________________________________________________________
* This program computes the profit or loss from selling
* shares of stock. It uses a function to calculate the
* profit based on the purchase price, selling price,
* and commissions paid.
*
* Computation is based on the formula:
* Profit = (Shares * Selling Price - Selling Commission)
* - (Shares * Purchase Price + Purchase Commission)
*_______________________________________________________________
* INPUT
* shares : Number of shares of stock
* purchasePrice : Purchase price per share
* purchaseComm : Purchase commission paid
* sellingPrice : Selling price per share
* sellingComm : Selling commission paid
*
* OUTPUT
* profit : Profit or loss from the stock sale
*
****************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototype
double stockProfit(double, double, double, double, double);
int main()
{
double shares; //INPUT - Number of shares
double purchasePrice; //INPUT - Purchase price per share
double purchaseComm; //INPUT - Purchase commission
double sellingPrice; //INPUT - Selling price per share
double sellingComm; //INPUT - Selling commission
double profit; //OUTPUT - Profit or loss
// Get the number of shares
cout << "Enter the number of shares: ";
cin >> shares;
// Get the purchase price per share
cout << "Enter the purchase price per share: $";
cin >> purchasePrice;
// Get the purchase commission
cout << "Enter the purchase commission: $";
cin >> purchaseComm;
// Get the selling price per share
cout << "Enter the selling price per share: $";
cin >> sellingPrice;
// Get the selling commission
cout << "Enter the selling commission: $";
cin >> sellingComm;
// Call the function to calculate profit
profit = stockProfit(shares, purchasePrice,
purchaseComm, sellingPrice,
sellingComm);
// Display the profit or loss
cout << fixed << setprecision(2);
if (profit > 0)
{
cout << "The stock profit is: $"
<< profit << endl;
}
else if (profit < 0)
{
cout << "The stock loss is: $"
<< -profit << endl;
}
else
{
cout << "There is no profit or loss." << endl;
}
return 0;
}
// Function definition
// Calculates and returns the stock profit or loss
double stockProfit(double shares, double purchasePrice,
double purchaseComm, double sellingPrice,
double sellingComm)
{
double profit; // Profit or loss from stock sale
// Calculate the stock profit or loss
profit = ((shares * sellingPrice) - sellingComm)
- ((shares * purchasePrice) + purchaseComm);
return profit;
}