setw() help

Feb 3, 2017 at 11:41pm
Write your question here.

When entering a bill amount in of over $100.00 all of the decimals line up correctly. If entering a bill amount of say $75.26 the first 10% tip does not line up by decimal and I can't figure out the proper way to setw() to get it to.

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

using namespace std;

int main ()
{
	
	float bill;
	
	cout << "What is the total bill: $" ; // this portion defines what is displayed on the screen and what needs to be inputted
	cin >> bill;


    
	
	cout << fixed << setprecision(2);
	cout << "The recommend tip amounts are: " << endl 
		 << "10% = " << "$" << bill * .10 << endl
		 << "15% = " << "$" << bill * .15 << endl                      
		 << "20% = " << "$" << bill * .20 << endl
		 << "25% = " << "$" << bill * .25 << endl << endl
		                                  
		  
		 << "The total bill with the recommended tips are:" << endl 
		 << "10% = " << "$" << bill +  bill * .10 << endl
		 << "15% = " << "$" << bill +  bill * .15 << endl                
		 << "20% = " << "$" << bill +  bill * .20 << endl
		 << "25% = " << "$" << bill +  bill * .25 << endl;
	
	return 0;
}
Feb 4, 2017 at 12:22am
This should allow your numbers to line up as long as the total bill is less than 999.99

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

using namespace std;

int main()
{

	float bill;

	cout << "What is the total bill: $"; // this portion defines what is displayed on the screen and what needs to be inputted
	cin >> bill;




	cout << fixed << setprecision(2);
	cout << "The recommend tip amounts are: " << endl;
	cout << "10% = " << "$" << setw(6) << bill * .10 << endl;
	cout << "15% = " << "$" << setw(6) << bill * .15 << endl;
	cout << "20% = " << "$" << setw(6) << bill * .20 << endl;
	cout << "25% = " << "$" << setw(6) << bill * .25 << endl;

	cout << "\n";

	cout << "The total bill with the recommended tips are:" << endl;
	cout << "10% = " << "$" << setw(6) << bill + bill * .10 << endl;
	cout << "15% = " << "$" << setw(6) << bill + bill * .15 << endl;
	cout << "20% = " << "$" << setw(6) << bill + bill * .20 << endl;
	cout << "25% = " << "$" << setw(6) << bill + bill * .25 << endl;

	return 0;
}
Feb 4, 2017 at 12:44am
That does work, however, it leaves the first line of 10% looking like this.

What is the total bill: $85.25
The recommend tip amounts are:
10% = $ 8.53
15% = $ 12.79
20% = $ 17.05
25% = $ 21.31

The total bill with the recommended tips are:
10% = $ 93.78
15% = $ 98.04
20% = $102.30
25% = $106.56

Is there a way to eliminate the space between $ 8.53.
Last edited on Feb 4, 2017 at 12:44am
Feb 4, 2017 at 4:41am
Hi,

Investigate the std::left and std::right iostream manipulators.
Topic archived. No new replies allowed.