Friday, March 26, 2010

Stock Transaction Program: It displays the money paid in dollars for the stock, commission paid for the broker, total stock bought, total stock sold, commissions paid to broker and whether you ended up making profit or loss (if you also sold your stock).


// Programming Assignment: 2. Stock Transaction Program
// Programming Environment: Bloodshed
// Date/Time: 1/27/2010  3:01:55 AM
//---------------------------------------------------------------------------
// This program asks the Joe user to input the values for shares purchased
// and sold, price per share bought and sold and, percentage of commission when
// bought and sold the stock to stock broker.
// It processes the information provided by the user using if...else statement
// at two different location in the program to be more user-friendly.
// It displays the money paid in dollars for the stock, commission paid for the
// broker, total stock bought, total stock sold, commissions paid to broker and
// whether you ended up making profit or loss (if you also sold your stock).
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Function Prototypes
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
int main (void)
{
    cout.setf (ios::fixed, ios::floatfield);
    cout.setf (ios::showpoint);
    cout.precision (2);

    // Variable Initialization.
    int share_no;
    double share_price;
    double commission_rate;
    double amount_paid_stock;
    double commission_paid;
  
    int share_no_sold;
    double share_price_sold;
    double commission_rate_sold;
    double amount_sold_stock;
    double commission_paid_sell;
  
    double total_buying;
    double total_selling;
    double profit;
    double loss;
  
    // Prompt for and get shares purchased, price per share and commission to
    // broker.  
    cout << endl << "Enter the number of shares purchased: ";
    cin >> share_no;
  
    cout << endl << "Enter the price per share: ";
    cin >> share_price;
  
    cout << endl << "Enter the percentage of commission paid to broker: ";
    cin >> commission_rate;
  
    // Calculations for amount paid for stock, commission paid to the broker and
    // total buying.
    amount_paid_stock = share_no * share_price;
    commission_paid = amount_paid_stock * commission_rate / 100;
    total_buying = amount_paid_stock + commission_paid;
  
    // Display Results
    cout << endl << "Amount of money paid for the stock: " << "$" << amount_paid_stock;
    cout << endl << "Amount of commission paid to broker when the stock was bought: " << "$" << commission_paid;
    
    // Using if...else to make this program more user-friendly.
    cout << endl << endl << endl << "\nHave you sold the stock? (y/n): ";
    char answer;
    cin >> answer;
    if (answer == 'Y' || answer == 'y'){    
  
       // Prompt for and get shares sold, price per share and commission to broker.
       cout << endl << "Enter the number of shares sold: ";
       cin >> share_no_sold;
  
       cout << endl << "Enter the price per share sold: ";
       cin >> share_price_sold;
  
       cout << endl << "Enter the percentage of commission paid to broker: ";
       cin >> commission_rate_sold;
  
       // Calculations for amount of stock sold, commission paid to sell the
       // stock and total selling.  
       amount_sold_stock = share_no_sold * share_price_sold;
       commission_paid_sell = amount_sold_stock * commission_rate_sold / 100;
       total_selling = amount_sold_stock - commission_paid_sell;
       profit = total_selling - total_buying;
  
       // Display results
       cout << endl << "Amount of money received for the stock sold: " << "$" << amount_sold_stock;
       cout << endl << "Amount of commission paid to broker when the stock was sold: " << "$" << commission_paid_sell;
       // cout << endl << "Profit/loss: " << "$" << profit;

       if (profit >= 0.0){
          cout << endl << "Your profit is: " << "$" << profit;
          } else {
          cout << endl << "Your Loss is: " << "$" << fabs(profit);
          // Using Fabs function to convert negative no into a positive.
          }

    } else {
    cout << endl << "Come back again when you sell your stock. ";
    cout << endl << "Thank You";
    }
        
    cout << endl << endl << endl << "Depress Any Key to Continue...";
    _getch ();
    return 0;
}
//---------------------------------------------------------------------------
// Function Definitions follow.
//---------------------------------------------------------------------------

1 comment:

  1. This is my first programming class and i just wanted to thank you.....this really helped me out alot

    ReplyDelete

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