Won't calculate correctly

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.

Above is what is being asked. It runs without syntax errors, but does not calculate correctly. Any help greatly appreciated!

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
35
36
37
38
39
40
41
42
  P#include <iostream>

using namespace std;

int main() {
    // Write your main here
   const double FEE = 0.015;
    double purchasePrice;
    double salePrice;
    double totalCharges;
    double AmtInvested;
    double AmtReceived;
    double profit;
    int numOfShares;
    double saleFee;
    double purchaseFee;
 
    cout << "What was the purchase price?" << endl;
    cin >> purchasePrice;
    cout << "What was the selling price of each share?" << endl;
    cin >> salePrice;
    cout << "How many shares were sold?" << endl;
    cin >> numOfShares;
        
        
   
    profit = (AmtReceived - AmtInvested)   ;
    AmtReceived = (numOfShares * salePrice) - saleFee;
    AmtInvested = (numOfShares * purchasePrice) +  purchaseFee ;
    saleFee = (numOfShares * salePrice) * FEE;
    purchaseFee = (numOfShares * purchasePrice) * FEE;
    totalCharges = saleFee + purchaseFee;
 
    cout << "Amount invested: " << AmtInvested << endl;
    cout << "Service charges: " << totalCharges << endl;
    cout << "Amount gained / lost: " << profit << endl;
    cout << " Amount received: " << AmtReceived  << endl;

    
    return 0;
}


Last edited on
You are computing things in the wrong order.
For example, profit (line 27) should be computed only after AmtReceived and AmtInvested have been computed.

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>

using namespace std;

int main() {

    const double FEE = 0.015;

    double purchasePrice;
    cout << "What was the purchase price? " ;
    cin >> purchasePrice;

    double salePrice;
    cout << "What was the selling price of each share? " ;
    cin >> salePrice;

    int numOfShares;
    cout << "How many shares were sold? ";
    cin >> numOfShares;

    const double saleFee = (numOfShares * salePrice) * FEE;
    const double AmtReceived = (numOfShares * salePrice) - saleFee;

    const double purchaseFee = (numOfShares * purchasePrice) * FEE;
    const double AmtInvested = (numOfShares * purchasePrice) +  purchaseFee ;

    const double profit = (AmtReceived - AmtInvested)   ;
    const double totalCharges = saleFee + purchaseFee;

    cout << "Amount invested: " << AmtInvested << '\n'
         << "Service charges: " << totalCharges << '\n'
         << "Amount received: " << AmtReceived  << '\n'
         << "Amount gained / lost: " << profit << '\n';
}
Last edited on
Also, wouldn't it be more correct not to include the charge in the amount invested, since it's not actually part of the investment. eg.

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
35
36
37
38
39
#include <iostream>

using namespace std;

int main() {
    // Write your main here
   const double FEE = 0.015;
    double purchasePrice;
    double salePrice;
    double totalCharges;
    double AmtInvested;
    double AmtReceived;
    double profit;
    int numOfShares;
    double saleFee;
    double purchaseFee;
 
    cout << "What was the purchase price?\t";
    cin >> purchasePrice;
    cout << "What was the selling price?\t";
    cin >> salePrice;
    cout << "How many shares were sold?\t";
    cin >> numOfShares;

    saleFee = (numOfShares * salePrice) * FEE;
    purchaseFee = (numOfShares * purchasePrice) * FEE;
    totalCharges = saleFee + purchaseFee;
    AmtReceived = (numOfShares * salePrice);
    AmtInvested = (numOfShares * purchasePrice);
    profit = (AmtReceived - AmtInvested)-totalCharges;
    
    cout << endl;
    cout << "Amount invested:\t" << AmtInvested << endl;
    cout << "Service charges:\t" << totalCharges << endl;
    cout << "Amount Gain/Loss:\t" << profit << endl;
    cout << "Amount received:\t" << AmtReceived -totalCharges << endl;
    
    return 0;
}

What was the purchase price?	100
What was the selling price?	100
How many shares were sold?	10

Amount invested:	1000
Service charges:	30
Amount Gain/Loss:	-30
Amount received:	970
Last edited on
GOT IT!! Thanks guys!
Topic archived. No new replies allowed.