Advertisement
hfxdesign

Stock Analyzer

Sep 23rd, 2014
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. /*
  2. Name: Nickelis Jarvis
  3. Date: 9.23.14
  4. Program Name: Stockbroker
  5. Description: Analyze and calculate gain or loss of stock over time.
  6. */
  7.  
  8. #include <iostream>
  9. #include <iomanip>
  10. #include <string>
  11. #include <cstdlib>
  12. #include <ctime>
  13.  
  14. //globally define the value of percentage, this can also be done with a const double, but doing so limits it's scope.
  15. #define PERCENT 100.0
  16.  
  17. using namespace std;
  18.  
  19. int main() {
  20.     //declare and initialize variables, i prefer to order them by sizeof(*type).
  21.     bool coinFlip;
  22.     int numberShares;
  23.     double purchasePrice, sharesCost, newCost, percentGain;
  24.     const double commissionRate = 0.021;
  25.     string stockName, stockSymbol;
  26.  
  27.     //Get name of company you're purchasing shares in.
  28.     cout << "Enter name of company: ";
  29.     getline(cin, stockName);
  30.  
  31.     //Get stock symbol, this is usually 3-5 characters, all capital letters.
  32.     cout << "Enter stock symbol: ";
  33.     getline(cin, stockSymbol);
  34.  
  35.     //Get number of shares you purchased.
  36.     cout << "Enter number of shares purchased: ";
  37.     cin >> numberShares;
  38.  
  39.     //Get the price per each share.
  40.     cout << "Enter the price per share: ";
  41.     cin >> purchasePrice;
  42.  
  43.     //Calculate the total cost of shares purchased.
  44.     sharesCost = purchasePrice * numberShares;
  45.    
  46.     //Output cost of shares, commission rate, amount of comission charged, and total cost of purchase.
  47.     cout << fixed << setprecision(2) << "Cost of shares: $" << sharesCost << endl;
  48.     cout << setprecision(1) << "Comission rate: " << (commissionRate * PERCENT) << "\%" << endl;
  49.     cout << setprecision(2) << "Commission charged: $" << (commissionRate * sharesCost) << endl;
  50.     cout << "Total cost of purchase: $" << (commissionRate + sharesCost) << "\n" << endl;
  51.  
  52.     //Seed the random number generator by time since epoch.
  53.     srand(time(NULL));
  54.  
  55.     //Initialize percentGain with a random number from 1-100, divided by percent global constant.
  56.     percentGain = (rand() % 100) / PERCENT;
  57.  
  58.     //Initialize the boolean value coinFlip randomly (odd number = TRUE, even number = FALSE; 50/50 chance).
  59.     coinFlip = (rand() % 2);
  60.  
  61.     //I re-worked the if; else statement to a ternary statement.
  62.     coinFlip ? newCost = sharesCost + (sharesCost * percentGain) : newCost = sharesCost - (sharesCost * percentGain);
  63.  
  64.  
  65.     //Output new value, share price, total share value, amount of profit or loss, and return rate.
  66.     cout << "The new value of the stock" << endl;
  67.     cout << "New share price: $" << newCost / numberShares << endl;
  68.     cout << "New value of shares: $" << newCost << endl;
  69.     cout << "Profit or loss: $" << newCost - sharesCost << endl;
  70.     cout << "Rate of return: " << (newCost - sharesCost) / sharesCost << "\%" << endl;
  71.  
  72.     //Pause to view output.
  73.     system("pause");
  74.  
  75.     return(0);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement