fork download
  1. //Ryan Shahriyarpour CS1A Carl Argila CH6 HW Q:19
  2. //
  3. /*****************************************************************************
  4.  *
  5.  * Stock Profit
  6.  *
  7.  *********************
  8.  *
  9.  * This program calculates the profit or loss from the sale of a stock using
  10.  * the function stockProfit. The formula is:
  11.  * Profit = ((NS * SP) - SC) - ((NS * PP) + PC)
  12.  * NS = shares, SP = sale price, SC = sale commission,
  13.  * PP = purchase price, and PC = purchase commission
  14.  *
  15.  * INPUT
  16.  * shares - number of shares
  17.  * purchasePrice - purchase price per share
  18.  * purchaseComm - purchase commission paid
  19.  * salePrice - sale price per share
  20.  * saleComm - sale commission paid
  21.  *
  22.  * OUTPUT
  23.  * The amount of the profit or loss from the sale
  24.  *
  25.  ****************************************************************************/
  26.  
  27. #include <iostream>
  28. #include <iomanip>
  29. using namespace std;
  30.  
  31. // Function prototype
  32. double stockProfit(int shares, double purchasePrice, double purchaseComm,
  33. double salePrice, double saleComm);
  34.  
  35. int main()
  36. {
  37. int shares; // Number of shares (NS)
  38. double purchasePrice; // Purchase price per share (PP)
  39. double purchaseComm; // Purchase commission paid (PC)
  40. double salePrice; // Sale price per share (SP)
  41. double saleComm; // Sale commission paid (SC)
  42.  
  43. cout << "Number of shares: ";
  44. cin >> shares;
  45. cout << "Purchase price per share: $";
  46. cin >> purchasePrice;
  47. cout << "Purchase commission paid: $";
  48. cin >> purchaseComm;
  49. cout << "Sale price per share: $";
  50. cin >> salePrice;
  51. cout << "Sale commission paid: $";
  52. cin >> saleComm;
  53.  
  54. double profit = stockProfit(shares, purchasePrice, purchaseComm,
  55. salePrice, saleComm);
  56.  
  57. cout << fixed << setprecision(2);
  58. if (profit >= 0)
  59. cout << "\nThe sale resulted in a PROFIT of $" << profit << endl;
  60. else
  61. cout << "\nThe sale resulted in a LOSS of $" << -profit << endl;
  62.  
  63. return 0;
  64. }
  65.  
  66. // Returns profit (positive) or loss (negative) from stock sale
  67. double stockProfit(int shares, double purchasePrice, double purchaseComm,
  68. double salePrice, double saleComm)
  69. {
  70. double proceeds = (shares * salePrice) - saleComm; // Money received
  71. double cost = (shares * purchasePrice) + purchaseComm; // Money spent
  72. return proceeds - cost;
  73. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Number of shares: Purchase price per share: $Purchase commission paid: $Sale price per share: $Sale commission paid: $
The sale resulted in a PROFIT of $0.00