Friday, March 26, 2010

Functions to perform the profit or loss from the sale of stock.


// Programming Assignment: Stock Profit
// Programming Environment: Bloodshed
// Date/Time: 2/17/2010  5:48:40 PM
//---------------------------------------------------------------------------
// This program uses functions to perform the profit or loss from the sale
// of stock. It used Pass by value and return statement to get the profit or loss.
// The input is taken from the user and validated using while loop. Function is
// called to check the profit or loss and used if..else statement to check profit
// or loss. Then function defination is written to support the function call.
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
   //Function Prototype
   double Calculate_Profit(int, double, double, double, double);
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
int main (void)
{
    cout.setf (ios::fixed, ios::floatfield);
    cout.setf (ios::showpoint);
    cout.precision (2);
  
    int Num_Shares;
    double Share_Price_Buy;
    double Purchase_Commission;
  
    double Share_Price_Sell;
    double Sale_Commission;
  
    // Getting input from the user.
    cout << endl << "Please Enter the Number of Shares: ";
    cin >> Num_Shares;
    // Validating the user input to enter positive numbers only.
    while(Num_Shares <= 0) {
        cout << "Warning: Invalid Share Number." << endl
             << "Please Re-enter the Number of Shares: ";
        cin >> Num_Shares;
    }
  
    cout << endl << "Please Enter the Price of Share when Bought: $";
    cin >> Share_Price_Buy;
    // Validating the user input to enter positive numbers only.
    while(Share_Price_Buy <= 0) {
        cout << "Warning: Invalid Share Price." << endl
             << "Please Re-enter the Price Share when Bought: $";
        cin >> Share_Price_Buy;
    }
  
    cout << endl << "Please Enter the Commission Amount when Purchased Share: $";
    cin >> Purchase_Commission;  
    // Validating the user input to enter positive numbers only.
    while(Purchase_Commission <= 0) {
        cout << "Warning: Invalid Commission Amount." << endl
             << "Please Re-enter the Commission Amount when Purchased Share: $";
        cin >> Purchase_Commission;
    }
  
    cout << endl << "Please Enter the Price of Share when Sold: $";
    cin >> Share_Price_Sell;  
    // Validating the user input to enter positive numbers only.
    while(Share_Price_Sell <= 0) {
        cout << "Warning: Invalid Share Price." << endl
              << "Please Re-enter the Price Share when Sold: $";
        cin >> Share_Price_Sell;
    }
  
    cout << endl << "Please Enter the Commission Amount when Sold the Share: $";
    cin >> Sale_Commission;  
    // Validating the user input to enter positive numbers only.
    while(Sale_Commission <= 0) {
        cout << "Warning: Invalid Commission Amount." << endl
             << "Please Re-enter the Commission Amount when Sold the Share: $";
        cin >> Sale_Commission;
    }
  
    double profit; // Local Valiable defined to store the profit / loss.
  
    // Function Invoke / Call
    // Just using function call by passing parameters by value. Not using reference!
    profit = Calculate_Profit(Num_Shares, Share_Price_Buy, Purchase_Commission,
             Share_Price_Sell, Sale_Commission);
  
    // Using if..else statement to determine if we encounter with profit or loss.
    if(profit >= 0) {
        cout << endl << "\nYour Profit is: " << "$" << profit;
        } else {
          cout << endl << "\nYour Loss is: " << "$" << abs(profit);
        }                  
    
      
    cout << endl << endl << endl << "Depress Any Key to Continue...";
    _getch ();
    return 0;
}
//---------------------------------------------------------------------------
    // Function Defination
    // Function Defination that uses return statement. This functions returns
    // a double value to the function call.
    double Calculate_Profit (int num_shares, double share_price_buy,
                            double purchase_commission, double share_price_sell,
                            double sale_commission) {
           // Just the formula to calculate the profit / loss.                      
    double Profit = (num_shares * share_price_sell - sale_commission) -
                    (num_shares * share_price_buy + purchase_commission);
    return Profit; // Pass By Value
    }
//---------------------------------------------------------------------------

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.