c++ code output problem

*I have a problem with the output of my code. I'm supposed to input this values:

Acme Software, Inc.
1000
45.50
56.90

*With my current code, the output is this:

Company: Acme Software, Inc.
Shares: 1000

Purchase/share: $45.5
Cost of Stock: $45500
Cost of Commission: $1137.5
Total cost: $46637.5

Sale/Share: $56.9
Income from stock: $56900
Cost of Commission: $1422.5
Total income: $55477.5

Gain or loss: $8840

*But I can't get my output to like the one below with the 0's at the end of the numbers. For example, Purchase/share:$45.5, should be Purchase/share: $45.50, and Gain or loss: $8840, should be Gain or loss: $8840.00.

Company: Acme Software, Inc.
Shares: 1000

Purchase/share: $45.50
Cost of stock: $45500.00
Cost of commission: $1137.50
Total cost: $46637.50

Sale/share: $56.90
Income from stock: $56900.00
Cost of commission: $1422.50
Total income: $55477.50

Gain or loss: $8840.00


*Here is my coding:

#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;
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, so if anyone can correct my code to make the output look like it's supposed to, I would be grateful. Thanks!


Last edited on
Hello bal160730,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Use this before your first cout that has a decimal number to print:

std::cout << std::fixed << std::showpoint << std::setprecision(2);

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.