Question



Instructions Write a program called stocks.cpp. The program should prompt the user for the number of shares purchased, purcha
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<iostream>
using namespace std;

int getNoOfShares()
{
        int noOfShares;
        while (true)
        {
                cout << "Enter number of shares purchased :: ";
                cin >> noOfShares;
                if (noOfShares > 0)
                {
                        break;
                }
                cout << "Invalid value !!!" << endl;
        }
        return noOfShares;
}

float getSalePrice()
{
        float salePrice;
        while (true)
        {
                cout << "Enter sale price of shares :: ";
                cin >> salePrice;
                if (salePrice > 0)
                {
                        break;
                }
                cout << "Invalid value !!!" << endl;
        }
        return salePrice;
}


float getSaleCommision()
{
        float saleCommision;
        while (true)
        {
                cout << "Enter sale commision on shares :: ";
                cin >> saleCommision;
                if (saleCommision > 0)
                {
                        break;
                }
                cout << "Invalid value !!!" << endl;
        }
        return saleCommision;
}


float getPurchasePrice()
{
        float purchasePrice;
        while (true)
        {
                cout << "Enter purchase price of shares :: ";
                cin >> purchasePrice;
                if (purchasePrice > 0)
                {
                        break;
                }
                cout << "Invalid value !!!" << endl;
        }
        return purchasePrice;
}

float getPurchaseCommision()
{
        float purchaseCommision;
        while (true)
        {
                cout << "Enter purchase commision on shares :: ";
                cin >> purchaseCommision;
                if (purchaseCommision > 0)
                {
                        break;
                }
                cout << "Invalid value !!!" << endl;
        }
        return purchaseCommision;
}

float calculateProfit(int NS, float SP, float SC, float PP, float PC)
{
        float profit;
        profit = ((NS*SP) - SC) - ((NS*PP) + PC);
        return profit;
}

int main()
{
        // declared variable for every parameter required to calculate profit
        int NS;
        float SP, SC, PP, PC;
        float profit = 0;

        /* called functions to get various values required to calculate profit */
        NS = getNoOfShares();
        SC = getSaleCommision();
        SP = getSalePrice();
        PP = getPurchasePrice();
        PC = getPurchaseCommision();

        /* called function to calculate profit, passed parameters to function */
        profit = calculateProfit(NS, SP, SC, PP, PC);
        cout << "total profit is :: "<<profit; /* print profit on console*/
        return 0; 
}

Enter number of shares purchased :: 154 Enter sale commision on shares :: 12 Enter sale price of shares :: 120 Enter purchase

Enter number of shares purchased :: -2 Invalid value !!! Enter number of shares purchased :: 154 Enter sale commision on shar

Please upvote if u like answer otherwise comment if you have any doubt.

Add a comment
Know the answer?
Add Answer to:
Instructions Write a program called stocks.cpp. The program should prompt the user for the number of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python program to prompt the user for an integer number. Assign this integer to...

    Write a Python program to prompt the user for an integer number. Assign this integer to a variable X. Then assign a variable Y to the X**3 (X to the power 3). Finally, assign the variable Z to the square root of Y and print the values of X, Y, and Z as shown in the example below. Use main() function to define and call the program. Example: Enter an integer number: 3 X = 3 if Y=X**3 then Y...

  • Write a C program that allows the user to enter sales information for a number of...

    Write a C program that allows the user to enter sales information for a number of sales persons and compute the commissions due for each, and summary results in report form. The program should first prompt the user to enter how many sales persons to include in the analysis. The user will then be prompted for the name, number of sales commissions, and rate of commission to be paid to each sales person. A summary itemized report will follow, see...

  • C++ TrainStation Program Create a function called searchForSchedules that will: - prompt a user to enter...

    C++ TrainStation Program Create a function called searchForSchedules that will: - prompt a user to enter a scheduleId - search for arrival and departure times of trains based on a trains ID number Create a function called editSchedules that will: - allow the user to edit the fields for a given schedule that is in the linked list. - the program must prompt the user to enter the scheduleId as the key to find the schedule to edit. Print a...

  • Write a complete C++ program to converts ounces into pounds and ounces. Prompt the user for...

    Write a complete C++ program to converts ounces into pounds and ounces. Prompt the user for the weight in ounces in the main program, pass the ounces to the function called convert(). In the function, convert the ounces to pounds and ounces then display it in the function. (hint: 1 pound = 16 ounces) Sample output: Enter ounces:34 2 pound and 2 ounces Using c++

  • Write an application that queries the user for their name and the following information about a...

    Write an application that queries the user for their name and the following information about a stock transaction and then displays the net gain (or loss) on the transaction. The feedback to the user should follow this format. Thanks for your recent transaction (NAME). The net gain on your transaction is $XX.XX. 1. Number of shares bought and sold (this is one number not two) 2. Purchase price per share 3. Purchase commission as a double (for example, 2% commission...

  • C++ Program Write a do-while loop that continues to prompt a user to enter a number...

    C++ Program Write a do-while loop that continues to prompt a user to enter a number less than 100, until the entered number is actually less than 100. End each prompt with a newline. Ex: For the user input 123, 395, 25, the expected output is: Enter a number (<100): Enter a number (<100): Enter a number (<100): Your number < 100 is: 25 #include <iostream> using namespace std; int main() { int userInput; /* Your solution goes here */...

  • Write a program that (in the main program) prompts the user for the (integer) model number...

    Write a program that (in the main program) prompts the user for the (integer) model number of their car. The main function should then call the function check defective which should • take in the model number of the car • check if the model number is 119, 179, 221, 780, or anything between 189 and 195, inclusive, which indicates the car is defective • print a message indicating whether or not the car is defective Note: this is should...

  • In C++: A. Write 3 functions that are called in the program. 1. Function readInput prompt...

    In C++: A. Write 3 functions that are called in the program. 1. Function readInput prompt user to enter an integer to store to the parameter. 2. Function isPerfectSquare take an integer parameter and checks whether it's a perfect square, that it's square root is an integer. 3. Function min3 return the mainimum value of the parameter values, it shouldn’t use any if statement to compare the parameters but call min2 to do the comparison. Extra Credit: Function printPrimeFactorization print...

  • C++ Write a void function called getAge that has no formal parameter. The function should prompt the user for their...

    C++ Write a void function called getAge that has no formal parameter. The function should prompt the user for their age. An example of the call to the function could be as follows getAge();

  • Write an application that queries the user for the following information about a stock transaction and...

    Write an application that queries the user for the following information about a stock transaction and then displays the net gain or loss on the transaction. Number of shares bought and sold (this is one number not two) Purchase price per share Purchase commission as a double (for example, 2% commission would be 0.02) Sale price per share Sale commission as a double (for example, 2% commission would be 0.02) You must allow the user to enter the values. However...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT