Easy C++ coding

These are the instructions.

This is a simple program to calculate stock prices based on user input. For instance, if Kathryn bought 750 shares of stock at a price of $35.00 per share. She must pay her stockbroker a 2 percent commission for the transaction. Below are the details.


1. Prompt the user for number of shares, price per share, and commission rate. 2. Store these in local variables.
3. All values must be stored in well-formed, commented, local variables and use
standard mathematical operators to perform the calculations.
4. Have your program calculate and display the following:

The amount paid for the stock alone (without the commission).
The amount of the commission.
The total amount paid (for the stock plus the commission).


Can someone please write the code that would produce these results in c++ and in the simplest way possible. Yes this is a homework question but it is the end of the semester and I have so much other HW for this class. Please help me out Thank you so much.
Homework is for you to solve yourself, so you learn from the experience.

Having us write all the code for you won't help you pass the class.
Hello herhsey1234,

Back when I was in school one of the classed I took the instructor said that in the early days of programming it was 10% planning and 90% coding. This later evolved into 90% planning and 10% coding.

So the first question would be what have you planned?

I also like to rework the unstructions into something that looks like small steps:

This is a simple program to calculate stock prices based on user input.For instance, if Kathryn bought 750 shares
of stock at a price of $35.00 per share. She must pay her stockbroker a 2 percent commission for the transaction.
Below are the details.


1. Prompt the user for:
 • number of shares
 • price per share
 • and commission rate.

2. Store these in local variables.

3. All values must be stored in well - formed, commented, local variables and use
standard mathematical operators to perform the calculations.

4. Have your program calculate and display the following:
 • The amount paid for the stock alone(without the commission).
 • The amount of the commission.
 • The total amount paid(for the stock plus the commission).


If you have not planned your program then working in small steps is usually a good approach.

Start with step (1). Write a program to collect the user input. This also gives you an idea of what variables you will need.

Step (2) tends to go along with step (1). Not really an individual step to work on.

Step (3) also goes along with step (1) and most important is All values must be stored in well - formed, commented, local variables. Easier to write the comments as you define the variables then it is to go back later and add the comments.

Write the prompts and collect the input. Compile often and test the code to make sure it works the way that you want. Here is an idea for the first entry:
1
2
std::cout << "\n How many shares of stock would you like: ";
std::cin >> shares;
The opening "\n " is optional. This makes what is displayed on the screen easier to read. This is not required. Most who are new to programming think the the first line should end with "std::endl" although this is OK by leaving it off the "cin" will be on the same line as the prompt, which IMO looks better, also notice the space that follows the colon.

The wording of the above prompt can be changes to anything you would like. It is only a suggestion.

Finish out getting the input and post the code that you have worked up. Any problems that you have can be worked out or pointed out.

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int no_shares = 0;
    double commission_rate = 0.0;
    
    cout << "   Please enter number of shares: ";
    cin >> no_shares;
    
    cout << "Please enter commission rate (%): ";
    cin >> commission_rate;
    
    double amount_without_commission = no_shares * price_per_share;


    cout << "The amount paid for the stock alone (without the commission): $" 
<< amount_without_commission << '\n';
    return 0;
}
Last edited on
Hello herhsey1234,

Lets see if I get reported again.

A good start, but does not compile.

The first step asks for three pieces of information from the user, but you are only asking for two.

Line 17 is more than I was think for right now, but it is usable except for the error it caused. You should compile the program and see if you can fix the error first. It is a simple error and fix, but good practice for dealing with error messages.

Refer back to #1 to see what information you need to collect.

Hope that helps,

Andy
Registered users can post here. Sign in or register to post.