question about formatting cout

My current assignment is to prompt the user for a name and gross pay, then apply a series of taxes and payments, then save the results (in formatted form) to an output file. I was able to complete all the objectives, except for the formatting.

The problem is formatting the output according to the teacher's wishes. The column that lists the tax names should be left alligned, with a colon after the name, then cout.fill('.'), the column listing the amount of money deducted should be right aligned with the '$' 's all vertically aligned, and whitespace filler between the $ and the number.

For the life of me, I can't seem to get all the lines to line up.

A second issue that I've noticed is with the value_entered for name; it could be a short name like "Bob" or disturbingly long, like "Otto von Sturfenslager-Bleishtifter." Is there a way to use str.length() to size up the name entered, and use that value as an argument in cout.width() in order to ensure that the margins of each column will be aligned?

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <fstream>
#include <string>
#include <limits>

using namespace std;



int main()
{
	char ch;
	double gross;
	string name;

    cout << fixed;
    cout.precision(2);

	do
	{
	cout << "\nPlease enter the name of the employee: ";
	getline(cin, name);

	cout << "\nPlease enter the gross pay for the employee: ";
	cin >> gross;

	
	cout << "You entered: " << name << endl;
	cout << "and: $" << gross << endl;
	cout << "Is this correct? (y/n)";
	(cin >> ch).get();
	} while (ch == 'n' || ch == 'N');


	cout << "\nThank you.\nThe following taxes will be applied\n.";
	cout << "and the itemized list will be saved to the output\n";
	cout << "file, \"taxes.txt\"" << endl;
        
        //These matrices hold the names of each tax\deduction
        //and the percentage, respectively
	const char * tax_list[6] = { "Federal Income Tax", "State Tax", "Social Security Tax",
		"Medicare/Medicaid", "Pension Plan", "Health Insurance" };
	double tax_percent[6] = { 15, 3.5, 5.75, 2.75, 5, 75 };
        
        //Loop to display the first five tax names, because....
	for (int i = 0; i < 5; i++)
	{
		cout << left;
		cout.fill(' ');
		cout.width(20);
		cout << tax_list[i] << ": ";
		cout << right;
		cout.fill('.');
		cout.width(10);
		cout << tax_percent[i] << "%\n";
	}

	//..."Health Insurance" is a set payment of $75.00
        cout <<left;
	cout.fill(' ');
	cout.width(20);
	cout << tax_list[5] << ": ";
	cout << right;
	cout.fill('.');
	cout.width(9);
	cout << "$" << tax_percent[5] << "\n";

        //net tracks the reductions of gross as each tax is paid
	double net = gross;
        //taxes_paid hold the dollar amount of each deduction
	double taxes_paid[6];

	for ( int i = 0; i < 5; i++)
	{
		taxes_paid[i] = (tax_percent[i]/ 100) * gross;
		net -= taxes_paid[i];
	}

	taxes_paid[5] = 75;
	net -= taxes_paid[5];

	ofstream fout;
	fout.open("taxes.txt");

        //prints the name the user entered
	fout << left;
	fout.fill(' ');
	fout.width(20);
	fout << "Name: ";
	fout.fill('.');
	fout << right;
	fout.width(24);
	fout << name << endl;

        //prints the gross income the user entered
	fout << left;
	fout.fill(' ');
        fout.width(20);
	fout << "Gross Income: ";
	fout.fill('.');
	fout << right;
	fout.width(7);
	fout << fixed;
	fout.precision(2);
	fout << "$" << gross << endl;

        //prints the dollar amount deducted for each tax, including
        //"health insurance"
	for ( int i = 0; i < 6; i++)
	{
		fout << left;
		fout << tax_list[i] << ": ";
		fout.width(20);
                fout.fill('.');
		fout << right;
		fout << "$";
		fout.fill(' ');
		fout.width(7);
		fout.precision(2);
		fout << taxes_paid[i] << "\n";
	}

        //prints net income
	fout << left;
	fout.fill('.');
	fout.width(20);
	fout << "Net Income: ";
	fout << right;
	fout << "$";
	fout.fill(' ');
	fout.width(7);
	fout.precision(2);
	fout << net << endl;

	fout.close();

	cout << "\nData saved to file successfully." << endl;

	cout << "\n\nAssignment: Example 3.6";
	cout << "\nProgrammed by: Otto von Sturfenslager-Bleishtifter" << endl;

	cout << "\n\nPress ENTER to close this window";
	cin.ignore(numeric_limits<streamsize>::max(), '\n');

	return 0;
}





Last edited on
I'm sure there are serval ways to do this but take a look at the use of sstream (ostringstream).
I also put the long name stuff in. See what you think. You were 98% there so I help the last 2%.
Just needed to format the $ and number together to get the spacing right.
The width function only changes the width of the very next output field and then immediately reverts to the default. Listing below illustrates its use. That is why the alignment was off.
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

#include <iostream>
#include <fstream>
#include <string>
#include <limits>
#include <sstream>
#include <iomanip>

using namespace std;



int main()
{
	char ch;
	double gross;
	string name;

    cout << fixed;
    cout.precision(2);

	do
	{
	cout << "\nPlease enter the name of the employee: ";
	getline(cin, name);

	cout << "\nPlease enter the gross pay for the employee: ";
	cin >> gross;

	
	cout << "You entered: " << name << endl;
	cout << "and: $" << gross << endl;
	cout << "Is this correct? (y/n)";
	(cin >> ch).get();
	} while (ch == 'n' || ch == 'N');


	cout << "\nThank you.\nThe following taxes will be applied\n.";
	cout << "and the itemized list will be saved to the output\n";
	cout << "file, \"taxes.txt\"" << endl;
        
        //These matrices hold the names of each tax\deduction
        //and the percentage, respectively
	const char * tax_list[6] = { "Federal Income Tax", "State Tax", "Social Security Tax",
		"Medicare/Medicaid", "Pension Plan", "Health Insurance" };
	double tax_percent[6] = { 15, 3.5, 5.75, 2.75, 5, 75 };
        
        //Loop to display the first five tax names, because....
	for (int i = 0; i < 5; i++)
	{
		cout << left;
		cout.fill(' ');
		cout.width(20);
		cout << tax_list[i] << ": ";
		cout << right;
		cout.fill('.');
		cout.width(10);
		cout << tax_percent[i] << "%\n";
	}

	//..."Health Insurance" is a set payment of $75.00
        cout <<left;
	cout.fill(' ');
	cout.width(20);
	cout << tax_list[5] << ": ";
	cout << right;
	cout.fill('.');
	cout.width(11);
        ostringstream outStrm;
        outStrm << "$" << setprecision(2) << fixed << tax_percent[5];
	cout << outStrm.str()<< "\n";

        //net tracks the reductions of gross as each tax is paid
	double net = gross;
        //taxes_paid hold the dollar amount of each deduction
	double taxes_paid[6];

	for ( int i = 0; i < 5; i++)
	{
		taxes_paid[i] = (tax_percent[i]/ 100) * gross;
		net -= taxes_paid[i];
	}

	taxes_paid[5] = 75;
	net -= taxes_paid[5];

	ofstream fout;
	fout.open("taxes.txt");

        int widthOfName(24);
        if ( (name.length()+2) > 24 ){
           widthOfName = (name.length()+2);
        }
        
        //prints the name the user entered
	fout << left;
	fout.fill(' ');
	fout.width(20);
	fout << "Name: ";
	fout.fill('.');
	fout << right;
	fout.width(widthOfName);
	fout << name << endl;

        //prints the gross income the user entered
	fout << left;
	fout.fill(' ');
        fout.width(20);
	fout << "Gross Income: ";
	fout.fill('.');
	fout << right;
	fout.width(widthOfName);
	fout << fixed;
	fout.precision(2);
        outStrm.str("");
        outStrm << "$" << setprecision(2) << fixed << gross;
	fout << outStrm.str() << endl;

        //prints the dollar amount deducted for each tax, including
        //"health insurance"
	for ( int i = 0; i < 6; i++)
	{
                outStrm.str("");
                outStrm << tax_list[i] << ":";
		fout.fill(' ');
		fout << left;
		fout.width(20);
                fout << outStrm.str();
                fout.fill('.');
		fout << right;
		fout.width(widthOfName);
                outStrm.str("");
                outStrm << "$" << setprecision(2) << fixed << taxes_paid[i];
		fout << outStrm.str() << "\n";
	}

        //prints net income
	fout << left;
	fout.fill(' ');
	fout.width(20);
	fout << "Net Income: ";
	fout << right;
	fout.fill('.');
	fout.width(widthOfName);
        outStrm.str("");
        outStrm << "$" << setprecision(2) << fixed << net;
	fout << outStrm.str() << endl;

	fout.close();

	cout << "\nData saved to file successfully." << endl;

	cout << "\n\nAssignment: Example 3.6";
	cout << "\nProgrammed by: Otto von Sturfenslager-Bleishtifter" << endl;

	cout << "\n\nPress ENTER to close this window";
	cin.ignore(numeric_limits<streamsize>::max(), '\n');

	return 0;
}

Output:

./a.out

Please enter the name of the employee: Otto von Sturfenslager-Bleishtifter

Please enter the gross pay for the employee: 100000
You entered: Otto von Sturfenslager-Bleishtifter
and: $100000.00
Is this correct? (y/n)y

Thank you.
The following taxes will be applied
.and the itemized list will be saved to the output
file, "taxes.txt"
Federal Income Tax  : .....15.00%
State Tax           : ......3.50%
Social Security Tax : ......5.75%
Medicare/Medicaid   : ......2.75%
Pension Plan        : ......5.00%
Health Insurance    : .....$75.00

Data saved to file successfully.


Assignment: Example 3.6
Programmed by: Otto von Sturfenslager-Bleishtifter


Press ENTER to close this window

--------------------------------------------------------------

cat taxes.txt 
Name:               ..Otto von Sturfenslager-Bleishtifter
Gross Income:       ...........................$100000.00
Federal Income Tax: ............................$15000.00
State Tax:          .............................$3500.00
Social Security Tax:.............................$5750.00
Medicare/Medicaid:  .............................$2750.00
Pension Plan:       .............................$5000.00
Health Insurance:   ...............................$75.00
Net Income:         ............................$67925.00
Last edited on
Topic archived. No new replies allowed.