Please help me with a partial working code

Question:
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.

The code works correctly for some inputs, however does not display the correct output in money.

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
#include <iostream>

using namespace std;

int main()
{
    double shares;
    double sharePrice;
    double sellShare;
    double totalSell;
    double servCharge;
    double absoTotal;

    cout << "Hello Cindy, How much shares would you like to sell today? " << endl;
    cin >> shares;
    cout << "What is the purchase price of each share? " << endl;
    cin >> sharePrice;
    cout << "What is the selling price of each share? " << endl;
    cin >> sellShare;
    totalSell = shares * sharePrice - sellShare;
    cout << "The amount you invested is: $" << shares * sharePrice << endl;
    cout << "The service charge is: $"<< shares*sharePrice*.015 << endl;
    servCharge = shares * sharePrice*.015;
    cout << "The amount you gained is: $"<< totalSell - servCharge << endl;
    absoTotal = totalSell - servCharge;
    cout << "You gained a total of: $" << absoTotal << endl;
   
    return 0;
}
What exactly is wrong here. Can you give an example of input and expected output? And thanks for using code tags, it's a breath of fresh air.
Last edited on
An example input for line 15 would be any integer.

Line 16 - any value such as 1.30 , 1.00 ect.

Line 18 - any value such as 1.30 , 1.00 ect.

The output that I am having trouble is on line 22, Line 24, and line 26.

The program does work with money conversion, I am having trouble with the equation on line 22, 24.
I know which output line you're having trouble with. I dont know why you are having trouble with them. You do the calculations which you've written yourself. I need to know what output you are expecting given a certain input. Otherwise I cant really help you, since it is just executing the equation that you wrote.
Last edited on
Example:

Hello Cindy, How much shares would you like to sell today?
1
What is the purchase price of each share?
2
What is the selling price of each share>
3
The amount you invested is: $2
The service charge is: $0.03
The amount you gained is: $-1.03
You gained: $-1.03


// Instead of outputting $-1.03, it should output $0.97, and so on for any other outputs to be correct. How do I code the program in such a way to include correct money conversion? Can you / someone offer tips?
closed account (E0p9LyTq)
What exactly is the output you are expecting to receive, and what is the output you are getting? Give us an example, please.
How do I code the program in such a way to include correct money conversion?


I have no idea what you mean. Correct money conversion? You have a formula, you wrote the formula in code, and the formula is being executed perfectly and outputted. What else do you want? Either you have the wrong formula or there is something that we're missing. It's a huge waste of time when we have to beat around the bush so it would be great if you could just give us all the information we need, and tell us what you actually want. Why should it be $0.97? Using your formula and inputed values it should not be that, but rather $-1.03.
Last edited on
The output should be $0.97 because "Cindy" bought the share for 2 ($2.00) she plans to sell the share for 3 ($3.00) therefore 3 - 2 = 1; therefore Cindy earned 1 ($1.00), but there is a service charge fee associated with the service ($0.03) , this value is subtracted from what she earned.

So, $1.00 - $0.03 = $0.97.

My program is computing the equation wrong, can anyone help me with this or offer any tips?
Last edited on
closed account (E0p9LyTq)
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>

int main()
{
   double shares;
   double sharePrice;
   double sellShare;
   double totalSell;
   double totalShare;
   double servCharge;
   double absoTotal;

   std::cout << "Hello Cindy, How much shares would you like to sell today? ";
   std::cin >> shares;

   std::cout << "What is the purchase price of each share? ";
   std::cin >> sharePrice;

   std::cout << "What is the selling price of each share? ";
   std::cin >> sellShare;
   
   totalSell = shares * sellShare;
   totalShare = shares * sharePrice;

   std::cout << "\nThe amount you invested is:\t$" << totalShare << std::endl;
   std::cout << "The total sell is:\t\t$" << totalSell << "\n";
   std::cout << "The service charge is:\t\t$"<< totalShare  *.015 << std::endl;

   servCharge = totalShare * .015;
   std::cout << "The amount you gained is:\t$"<< totalSell - totalShare - servCharge << std::endl;

   absoTotal = totalSell - servCharge;
   std::cout << "You gained a total of:\t\t$" << absoTotal << std::endl;
}


Hello Cindy, How much shares would you like to sell today? 1
What is the purchase price of each share? 2
What is the selling price of each share? 3

The amount you invested is:     $2
The total sell is:              $3
The service charge is:          $0.03
The amount you gained is:       $0.97
You gained a total of:          $2.97
Last edited on
Thank You so much! I realized that I was to complex in my equation and algorithm.
Topic archived. No new replies allowed.