And yet another formatting question

Hey! I've written a program that does some calculations yadda yadda yadda.
What I'd like to do is have the output look a little more neat. So I want the numbers aligned. Here is my code, I used the <iomanip> header file to have the decimal numbers rounded off but they are not neatly aligned as I would like. I want the number to be aligned by the decimal point.
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    // Declare Variables
    float payRate;
    
    float taxRate = 0.14;
    string initials;
    int week1;
    int week2;
    int week3;
    int week4;
    int week5;
    
    cout << fixed << showpoint << setprecision(2) << endl;
    
    cout << "Please, enter your initials to get started" << endl;
    cin >> initials;
    cout << initials << endl;
    
    cout << "What is your pay rate? " << endl;
    cin >> payRate;
    
    cout << "Please, enter the amount of hours worked in the first week" << endl;
    cin >> week1;
    
    cout << "Please, enter the amount of hours worked in the second week" << endl;
    cin >> week2;
    
    cout << "Please, enter the amount of hours worked in the third week" << endl;
    cin >> week3;
    
    cout << "Please, enter the amount of hours worked in the fourth week" << endl;
    cin >> week4;
    
    cout << "Last but not least, enter the amount of hours worked in the fifth week" << endl;
    cin >> week5;
    
    
    const float hoursWorked = week1 + week2 + week3 + week4 + week5; // Declared the variable hoursWorked
    
    cout << "Excellent! the total hours worked = \t"    << hoursWorked << endl;
    
   
    float grossEarns = hoursWorked * payRate; // Declared Variable grossEarns
    
    cout << "That means that your Gross Earnings = \t" << grossEarns << endl;
    
    const float taxesPaid = grossEarns * taxRate;
    
    cout << "That taxes you paid = \t\t\t" << taxesPaid << endl;
    
    float netEarns = grossEarns - grossEarns * taxRate;  //Declared variable netEarns
    
    cout << "Your net pay is = \t\t\t" << netEarns << endl;
    
    cout << "Expenses" << endl;
    
    const float clothes = netEarns * .20;  // declared Variable clothes
    cout << "Money spend on clothes = \t\t" << clothes << endl;
    
    const float schoolSupp = netEarns * .05;
    cout << "Money spent on school supplies = \t" << schoolSupp << endl;
    
    float amountRemain = netEarns - schoolSupp - clothes; // declaring amount remain
    cout << "Total amount remaining = \t\t" << amountRemain << endl;
    
    float expenses = netEarns - schoolSupp - clothes; // declared variable expenses
    
    const float savings = expenses * .25; // declared variable savings
    cout << "Money going to savings = \t\t" << savings << endl;
    
    float leftToSpend = netEarns - schoolSupp - clothes - savings; // declared a variable
    
    cout << "The money left to spend for youself = \t" << leftToSpend << endl;
    

    
    system("PAUSE");
    
    
    
    
    return 0;
}
Could you give me an example of how you would like it to be aligned?
like this

_12.00
_13.00
123.00


Instead of aligning starting from the first digit, I want them to be aligned by the decimal. Does that make sense?
Last edited on
setprecision() to set the amount of digits to display, fixed to have that amount be in the decimal place, and right to justify to the right.

Then setw() to give the amount of space you want:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	double x = .25;
	double y = 1.2;
	double z = 100.125;


	cout << setprecision(3) << fixed  << right;
	
	cout << setw(7) << x << endl;
	cout << setw(7) << y << endl;
	cout << setw(7) << z << endl;
	
	return 0;
}


A quick way if you forget:
1
2
3
if (number < 10) cout << "  ";
else if (number < 100) cout << " ";
//... 
Last edited on
Topic archived. No new replies allowed.