Skip to content

Commit

Permalink
added, modified, and updated
Browse files Browse the repository at this point in the history
  • Loading branch information
jesushilarioh committed May 1, 2020
1 parent fe8f7a0 commit 328c09a
Show file tree
Hide file tree
Showing 12 changed files with 940 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
*************************************************************/
#include <iostream>
#include <string>

using namespace std;

// Function Prototypes
double getInfo(string, double);
void getInfo(string, double &);
double profit(double, double, double, double, double);
double inputValidate(double);
void displayCalculation(double);

void displayCalculation(double, int);

int main()
{
Expand All @@ -35,98 +34,72 @@ int main()
profit_or_loss,
number_of_stock_sales,
total = 0;

// Get info
number_of_stock_sales = getInfo("How many stock sales? ", number_of_stock_sales);

getInfo("How many stock sales? ", number_of_stock_sales);

for (int i = 0; i < number_of_stock_sales; i++)
{
cout << "\nInfo for stock sale #" << (i + 1) << endl;

NS = getInfo("Number of shares: ", NS);
PP = getInfo("Purchase price per share: ", PP);
PC = getInfo("Purchase commission paid: ", PC);
SP = getInfo("Sale price per share: ", SP);
SC = getInfo("Sale commission paid: ", SC);

// Calculate
getInfo("Number of shares: ", NS);
getInfo("Purchase price per share: ", PP);
getInfo("Purchase commission paid: ", PC);
getInfo("Sale price per share: ", SP);
getInfo("Sale commission paid: ", SC);

profit_or_loss = profit(NS, PP, PC, SP, SC);
total += profit_or_loss;

// Display
displayCalculation(profit_or_loss);
total += profit_or_loss;

displayCalculation(profit_or_loss, (i + 1));
}

cout << "\nTotal profit or loss = $"
<< total
<< endl;

return 0;
} // END int main()

void getInfo(string prompt, double &user_input)
{
cout << prompt;
user_input = inputValidate(user_input);
}

/****************************************************
* Definition for inputValidate(): *
* inputValidate checks user input for a double. *
* If a double is NOT found, a *
* while loop displays an error, clears and ignores *
* previous input, and prompts the user to try *
* again. *
****************************************************/
double inputValidate(double num1)
{

while(!(cin >> num1))
while(!(cin >> num1) || num1 < 0)
{
cout << "Error. Number must not be "
<< " 0 or greater:";
cin.clear();
cin.ignore(1234, '\n');
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

return num1;
}

/****************************************************
* Definition for getInfo(): *
* getInfo is designed to get info from the user. *
* the first parameter recives an argument as a *
* string, which prompts the user. The second *
* argument receives the user input and is *
* validated upon receiving information. *
****************************************************/
double getInfo(string prompt, double userInput)
{
cout << prompt;
return inputValidate(userInput);
}

/****************************************************
* Definition for displayMenu(): *
* displayMenu displays a menu for user to choose *
* from. *
****************************************************/
void displayCalculation(double profit_or_loss)
{
cout << "The sale of the stock resulted in "
<< (profit_or_loss < 0 ? "LOSS." : "PROFIT.")
<< "At $" << profit_or_loss
<< endl;
}

/****************************************************
* Definition for profit(): *
* profit calculates the profit from a sale of *
* stock. It accepts the number of shares (NS), the *
* purchase price per share (PP), the purchase *
* commission paid (PC), the sale price per share *
* (SP), and the sale commission paid (SC). *
****************************************************/
double profit(double NS,
double PP,
double PC,
double SP,
double SC)
{
cout << "NS = " << NS << endl;
cout << "PP = " << PP << endl;
cout << "PC = " << PC << endl;
cout << "SP = " << SP << endl;
cout << "SC = " << SC << endl;

return ((NS * SP) - SC) - ((NS * PP) + PC);
}

void displayCalculation(double profit_or_loss, int stock_number)
{
cout << "The sale of stock #" << stock_number << " resulted in "
<< (profit_or_loss < 0 ? "LOSS." : "PROFIT.")
<< "At $" << profit_or_loss
<< endl;
}
Loading

0 comments on commit 328c09a

Please sign in to comment.