Need output formatting help!

Hey guys,
I need formatting help with my output.
Here's a snippet of my code.

1
2
3
4
5
6
cout << left << StockName << ' ';
cout << setw(8) << right << NUMofShares << ' ';
cout << fixed << setprecision(2);
cout << setw(9) << right << PurchasePrice << ' ';
cout << setw(9) << right << SalePrice << ' ';
cout << setw(14) << right << ProfitOrLoss << endl;


I want my output to look like this below.

1
2
3
4
5
6
Stock  Shares   P Price   S Price  Profit (Loss)
_____  ______   _______   _______  _____________
AAPL       23    102.30     96.25        -157.05
GE         15     23.00     28.87          70.15
MSFT       30     58.50     52.06        -213.10
GOOG       10    540.20    695.03        1528.40


But in reality it looks like this... (Below) :(

1
2
3
4
5
6
Stock  Shares   P Price   S Price  Profit (Loss)
_____  ______   _______   _______  _____________
AAPL       23    102.30     96.25        -157.05
GE       15     23.00     28.87          70.15
MSFT       30     58.50     52.06        -213.10
GOOG       10    540.20    695.03        1528.40


How can I fix this? The shorter Stock name is creating this issue for me.
Ex: GE.
How can I make the stock name stay in place as left aligned and have no effect on the setw for the numerical values?

Thanks,

MisterTams
Last edited on
1
2
3
4
5
6
cout << setw(4) << left << StockName << ' '; // <==
cout << setw(8) << right << NUMofShares << ' ';
cout << fixed << setprecision(2);
cout << setw(9) << right << PurchasePrice << ' ';
cout << setw(9) << right << SalePrice << ' ';
cout << setw(14) << right << ProfitOrLoss << endl;
Thanks Mantorr22!
Works great!
Topic archived. No new replies allowed.