Inaccurate Results

Hi! Im working on a homework assignment: Cindy uses the services of a brokerage firm to buy and sell stocks. The firm charges 1.5% service charges on the total amount for each transaction, buy or sell. When Cindy sells stocks, she would like to know if she gained or lost on a particular investment. Write a program that allows Cindy to input the number of shares sold, the purchase price of each share, and the selling price of each share. The program outputs the amount invested, the total service charges, amount gained or lost, and the amount received after selling the stock.

I've written my code however my test data seems to be inaccurate. Are my equations correct? They seem to be.

Shares sold: 1
Purchase price: 2
Sale Price: 3
Total amount gained/lost: 2.96 (which seems false)
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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    const double SERVICE_CHARGE = 0.015;
    int sharesSold;
    double purchasePrice;
    double salePrice;
    double totalCharges;
    double amountInvested;
    double netAmount;
    
    cout << "How many shares did you sell? ";
    cin >> sharesSold;
    cout << "What was the purchase price? $";
    cin >> purchasePrice;
    cout << "What was the selling price of the share? $";
    cin >> salePrice;
    
    amountInvested = sharesSold * purchasePrice;
    totalCharges = (sharesSold * salePrice) * SERVICE_CHARGE;
    netAmount = (sharesSold * salePrice) - totalCharges;
    
    cout << fixed << showpoint << setprecision(2);
    
    cout << "Amount invested: $" << amountInvested << endl;
    cout << "Service charges: $" << totalCharges << endl;
    cout << "Amount gained / lost: $" << netAmount << endl;
    
    return 0;
}
Last edited on
netAmount = (sharesSold * salePrice) - totalCharges;

You never factor in the amount the user invested, so right now they basically got all their items for free, and just sold it.

netAmount = (sharesSold * salePrice) - (totalCharges + amountInvested); // subtract amount Invested aswell
Ok, I see what the problem is. Thank you!
Topic archived. No new replies allowed.