white space/formatting

So when i input this:
Acme Software, Inc.
1000
45.50
56.90
i'm supposed to get:
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

But instead i get this:


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

this is my code:
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
/**
* string variable named companyName is declared.
* It stores name of the company.
* Using getline() function we scan companyName.
*/
string companyName;
getline(cin,companyName);
/**
* Now an integer variable named numOfShares is declared and scanned.
* It stores total shares to buy/sale.
*/
int numOfShares;
cin>>numOfShares;

/**
* Now we declare two double variables named
* purchaseShareCost and saleShareCost to store purchase price
* of each share and sale price of each share.
* Below we scan both values.(First we scan purchase cost)
*/
double purchaseShareCost,saleShareCost;
cin>>purchaseShareCost;
cin>>saleShareCost;

/**
* Total cost of purchase of numOfShares is calculated
* and stored in variable named totalPurchaseCost.
*/
double totalPurchaseCost=purchaseShareCost*numOfShares;

/**
* Commission of 2.5% is applied to both for purchase and selling.
* 2.5% can be simplified by dividing 2.5 by 100
* So 2.5/100 becomes 0.025
*/
const double commission= 0.025;
double commissionCost=commission*totalPurchaseCost;

/**
* Now we calculate total amount paid and store in variable named
* amountPaid
*/
double amountPaid=totalPurchaseCost+ commissionCost;

/**
* We have done with purchase of shares. Now it's time
* to sell the shares and earn profit or suffer loss.
* We shares number and cost of selling each share.
* So let's calculate totalSellCost.
*/
double totalSellCost=saleShareCost*numOfShares;
double commissionOnSell=commission*totalSellCost;
/**
* Note that while calculating amount we receive we need to subtract
* commission cost as we pay commission.
*/
double amountGain = totalSellCost - commissionOnSell;

/**
* Now we print all the data as required in same format.
*/

cout<<"\n\nCompany:"<<companyName<<endl;
cout<<"Shares:"<<numOfShares<<endl;
printf("\nPurchase/share:$%.2f", purchaseShareCost);
printf("\nCost of stock:$%.2f", totalPurchaseCost);
printf("\nCost of commission:$%.2f", commissionCost);
printf("\nTotal cost:$%.2f", amountPaid);

printf("\n\nSale/share:$%.2f", saleShareCost);
printf("\nIncome from stock:$%.2f", totalSellCost);
printf("\nCost of commission:$%.2f", commissionOnSell);
printf("\nTotal income:$%.2f", amountGain);

/**
* Now it's time to know if we got loss or profit.
*/
double proLoss= amountGain-amountPaid;
printf( "\n\nGain or loss:$%.2f", proLoss);


return 0;
}
Just add a space in the output format:

printf( "\n\nGain or loss: $%.2f", proLoss );

That said, why are you using printf() in C++? Use std::cout.

std::cout << "\nGain or loss: $" << proLoss << "\n";

Also prefer to terminate output with a newline. It is okay to add additional newlines before output, as you are doing, but the final newline is kind of important with how I/O works.

(You can initialize the precision to two decimal places in one spot, right at the beginning, with

1
2
3
4
int main()
{
  std::cout << std::fixed << std::setprecision( 2 );
  ...

Hope this helps.
Topic archived. No new replies allowed.