Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: Nickelis Jarvis
- Date: 9.23.14
- Program Name: Stockbroker
- Description: Analyze and calculate gain or loss of stock over time.
- */
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <cstdlib>
- #include <ctime>
- //globally define the value of percentage, this can also be done with a const double, but doing so limits it's scope.
- #define PERCENT 100.0
- using namespace std;
- int main() {
- //declare and initialize variables, i prefer to order them by sizeof(*type).
- bool coinFlip;
- int numberShares;
- double purchasePrice, sharesCost, newCost, percentGain;
- const double commissionRate = 0.021;
- string stockName, stockSymbol;
- //Get name of company you're purchasing shares in.
- cout << "Enter name of company: ";
- getline(cin, stockName);
- //Get stock symbol, this is usually 3-5 characters, all capital letters.
- cout << "Enter stock symbol: ";
- getline(cin, stockSymbol);
- //Get number of shares you purchased.
- cout << "Enter number of shares purchased: ";
- cin >> numberShares;
- //Get the price per each share.
- cout << "Enter the price per share: ";
- cin >> purchasePrice;
- //Calculate the total cost of shares purchased.
- sharesCost = purchasePrice * numberShares;
- //Output cost of shares, commission rate, amount of comission charged, and total cost of purchase.
- cout << fixed << setprecision(2) << "Cost of shares: $" << sharesCost << endl;
- cout << setprecision(1) << "Comission rate: " << (commissionRate * PERCENT) << "\%" << endl;
- cout << setprecision(2) << "Commission charged: $" << (commissionRate * sharesCost) << endl;
- cout << "Total cost of purchase: $" << (commissionRate + sharesCost) << "\n" << endl;
- //Seed the random number generator by time since epoch.
- srand(time(NULL));
- //Initialize percentGain with a random number from 1-100, divided by percent global constant.
- percentGain = (rand() % 100) / PERCENT;
- //Initialize the boolean value coinFlip randomly (odd number = TRUE, even number = FALSE; 50/50 chance).
- coinFlip = (rand() % 2);
- //I re-worked the if; else statement to a ternary statement.
- coinFlip ? newCost = sharesCost + (sharesCost * percentGain) : newCost = sharesCost - (sharesCost * percentGain);
- //Output new value, share price, total share value, amount of profit or loss, and return rate.
- cout << "The new value of the stock" << endl;
- cout << "New share price: $" << newCost / numberShares << endl;
- cout << "New value of shares: $" << newCost << endl;
- cout << "Profit or loss: $" << newCost - sharesCost << endl;
- cout << "Rate of return: " << (newCost - sharesCost) / sharesCost << "\%" << endl;
- //Pause to view output.
- system("pause");
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement