1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
/***************************************************************
Existing classes, objects, and functions used in this program.
Explain what items are used from each of these header files.
***************************************************************/
#include <iostream> // what is used from here? Gives access to cin, cout
#include "stocks.h"
#include <limits>
using namespace std; // eliminates needing the "std::" prefix
/***************************************************************
struct definitions
***************************************************************/
//none
/***************************************************************
global variables (rare)
***************************************************************/
//none
/***************************************************************
New function prototypes. Include a description of the function
input(s) and the function output. Often some data is helpful:
int largest( int, int, int ); // largest(4,5,-1) returns 5
***************************************************************/
//This function takes in the whole dollar value of as stocck and the numerator and
//denominator of it's fractional value and converts it into a double value.
double calculateStockPrice(int dollars, int top, int bottom); // calculateStockPrice(2.0, 1.0, 2.0) returns 2.5
/***************************************************************
main() function code.
The main() function always takes zero parameters/arguments
and returns 0 to indicate that the input was successfully
processed into output.
Later: A return of [some other value] means [something else].
****************************************************************/
int main()
{
int shares = 0;
int wholeNumber = 0;
int numerator = 0;
int denominator = 0;
double stockPrice = 0.0;
double stockValue = 0.0;
cout.setf(ios::fixed); //displays fixed numbers decimals
cout.precision(2); //displays two numbers after the decimal for fixed numbers
stock test;
//welcome message
cout << "Welcome to the stock value calculator\n";
//ask for number of shares
cout << "\nEnter the number of shares in a stock[0 or less to exit]:\n";
//get number of shares
cin >> shares;
//if they have no shares get out
while (shares > 0.0)
{
test.resetShares();
//ask for whole number of the price
cout << "Enter the whole dollar value of the stock:\n";
//get whole number of price
cin >> wholeNumber;
//ask for numerator of price fraction
cout << "Enter the top part of the fractional value(numerator):\n";
//get numerator of price fraction
cin >> numerator;
//ask for denominator of price fraction
cout<< "Enter the bottom part of the fractional value:\n";
//get denominator of price fraction
cin >> denominator;
// cout << test.getNumOfShares();
test.addShares(shares);
test.setPrice(wholeNumber, numerator, denominator);
cout << test.getShareValue() << "\n";
//ask for number of shares from user again
cout << "\nEnter the number of shares in a stock[0 or less to exit]:\n";
//get number of shares
cin >> shares;
}
cout << "\nGood-Bye\n"; //say good bye
cin.sync();
cout << "Press ENTER to continue...";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// system("pause"); // why is this here? What does it do? Pauses the screen so the user can see whats displayed.
return 0; // return 0 to indicate successful execution of main()
}
/***************************************************************
Code for other functions
Use the main() comment above as the model.
***************************************************************/
//This function takes in the whole dollar value of as stocck and the numerator and
//denominator of it's fractional value and converts it into a double value.
double calculateStockPrice(int dollars, int top, int bottom)
{
double calculation = 0.0; // new var for calculations
// calculate stock price in doubles, stock price = whole dollar value + (top/bottom)
calculation = (static_cast<double>(dollars) + ( static_cast<double>(top) / static_cast<double>(bottom) ) );
return calculation; // send calculation back to function call as the answer
}
|