Question: C++ programming. I have the code that takes the user input for number of stocks, purchase price, purchase commission, sale price, sale commission and performs

C++ programming.

I have the code that takes the user input for number of stocks, purchase price, purchase commission, sale price, sale commission and performs calculations to show profit/loss output. The problem I have is that I can't modify the code to take all this input from the text file, instead of asking user for input. I've tried many methods without any success. So the program should read data from text file, perform calculations and show the following output format.

Stock Shares Purchase Price Sale Price Profit/Loss

-------- --------- ------------ -------------- ---------------

AAPL 23 202.3 324.54 2793.62

GE 15 23.0 12.82 -170.6

MSFT 30 180.0 184.15 104.6

GOOG 10 923.40 1516.37 5909.8

(AAPL, GE, MSFT, GOOG) are trading symbols. The numbers following the trading symbol are:

number of shares as an integer, purchase price, purchase commission, sales price, and sales commission

The content of the file stocks.txt

AAPL 23 202.3 7.95 324.54 9.95 GE 15 23.0 7.95 12.82 9.95 MSFT 30 180.0 8.95 184.15 10.95 GOOG 10 923.40 8.95 1516.37 10.95 

The formula for profit is:

Profit = ((NS x SP) - SC) - ((NS x PP) +PC)

And here's my current program that needs modifying

#include

#include

using namespace std;

void getInfo(string, double&);

double profit(double, double, double, double, double);

double inputValidate(double);

void displayCalculation(double, int);

int main()

{

double NS, // Number of shares

PP, // Purchase price per share

PC, // Purchase commission paid

SP, // Sale price per share

SC, // Sale commission paid

profit_or_loss,

number_of_stock_sales,

total = 0;

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

for (int i = 0; i < number_of_stock_sales; i++)

{

cout << " Info for stock sale #" << (i + 1) << endl;

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;

displayCalculation(profit_or_loss, (i + 1));

}

cout << " Total profit or loss = $"

<< total

<< endl;

return 0;

} // END int main()

void getInfo(string prompt, double& user_input)

{

cout << prompt;

user_input = inputValidate(user_input);

}

double inputValidate(double num1)

{

while (!(cin >> num1) || num1 < 0)

{

cout << "Error. Number must not be "

<< " 0 or greater:";

cin.clear();

cin.ignore(numeric_limits::max(), ' ');

}

return num1;

}

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;

}

These are the requirements I'm trying to meet:

Create' and use a function that opens a passed fstream object and string filename for input. The function returns boolean true upon success (file exists and is opened successfully), false upon failure.

Prompt and read a filename from the user. If there is an error opening the file (using the previously described function), display a message and exit the program with an error code of -1.

If the file is successfully opened for input, read and process all the stock data from this file until the end of file (the exact number of lines in a file is unknown). Remember to close the file after all data has been read and processed from file.

Use only simple data types, not arrays, in your solution. You should, however, use a C++ string to hold the trading symbol and input user filename.

Use only output manipulators, not tabs or spaces, to format your console output in a table format as given below (text left aligned, numbers right-aligned, currency with 2 digits of precision after the decimal). (I showed an example how the output format should look like at the top)

A summary line listing the total number of shares and the total profit (or loss) for all stocks should be included after all the individual stock information has been displayed:

Total Profit (Loss) for 28 shares = $23.90

I've been working on this for few days now and can't get it to work, it's a challenging program. Please help!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!