I have a problem with the output of my code again. This time it doesn't show negative loss. I'm supposed to input this values:
Acme Software, Inc.
100
25
25
With my current code, the output is this:
Company: Acme Software, Inc.
Shares: 100
Purchase/share: $25.00
Cost of stock: $2500.00
Cost of commission: $62.50
Total cost: $2562.50
Sale/share: $25.00
Income from stock: $2500.00
Cost of commission: $62.50
Total income: $2437.50
Gain or loss: $125.00
**The problem is I can't get the Gain or loss: $125.00 to be Gain or loss: $-125.00. It needs to be negative number. This is what the output should be below:
Company: Acme Software, Inc.
Shares: 100
Purchase/share: $25.00
Cost of stock: $2500.00
Cost of commission: $62.50
Total cost: $2562.50
Sale/share: $25.00
Income from stock: $2500.00
Cost of commission: $62.50
Total income: $2437.50
Gain or loss: $-125.00
*Here is my coding:
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<bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
string company_name;
getline(cin , company_name); //input name of company
string tmp;
double number_of_shares;
getline(cin , tmp); //input number of shares
number_of_shares = atof(tmp.c_str());
double purchase_price_per_share;
getline(cin, tmp); //input purchase price
purchase_price_per_share = atof(tmp.c_str());
double saling_price_per_share;
getline(cin,tmp); //input saling price
saling_price_per_share = atof(tmp.c_str());
cout << "Company: " << company_name << endl;
cout << "Shares: " << number_of_shares << endl;
cout<<endl;
std::cout << std::fixed << std::showpoint << std::setprecision(2);
cout << "Purchase/share: $" << purchase_price_per_share << endl;
cout << "Cost of stock: $" << purchase_price_per_share*number_of_shares << endl;
cout << "Cost of commission: $" << purchase_price_per_share*number_of_shares*0.025 << endl;
const double Total_purchase = purchase_price_per_share*number_of_shares + purchase_price_per_share*number_of_shares*0.025;
cout << "Total cost: $" << Total_purchase << endl;
cout<<endl;
cout << "Sale/share: $" << saling_price_per_share <<endl;
cout << "Income from stock: $" << saling_price_per_share*number_of_shares << endl;
cout << "Cost of commission: $" << saling_price_per_share*number_of_shares*0.025 <<endl;
const double Total_income = saling_price_per_share*number_of_shares - saling_price_per_share*number_of_shares*0.025;
cout << "Total income: $" << Total_income << endl;
cout<<endl;
cout << "Gain or loss: $" << abs(Total_purchase - Total_income) << endl;
return 0;
}
|
*I really need help with this for the second time, so if anyone can correct my code to make the output look like it's supposed to, I would be grateful. Thanks!