<iomanip> format with setw(#)

I'm having trouble with getting the formatting to work with setw(). I've spent 2 hours on this.. and I thought it would be a simple task..

I'm just trying to get my current setup to have the correct format then I'll work on the rest of it.

Help would be greatly appreciated. Thank you!

Please note:
I've already checked the cplusplus.com part on <iomanip> and setw() but there weren't many examples. Then I checked this: https://en.cppreference.com/w/cpp/io/manip/setw but it doesn't seem to be working as it shows or as I'd expect..


Goal Output:
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
==============================================================================
fname          lname             test1   test2   test3   test4    avg.   grade
==============================================================================
Johnny         Lang                100     100     100      98   99.50       A
Tom            Jerry                85      90      97      98   92.50       A
Hillary        Clinton              90      91      93      95   92.25       A
Billie         Jeans               100      92      83      85   90.00       A
Bill           Kool                 75      85      95     105   90.00       A
Suzie          Wang                 95      85      75      80   83.75       B
Allie          Smith                85      85      80      82   83.00       B
Henry          Miller               75      85      82      90   83.00       B
Alice          Wood                 44      75      88      99   76.50       C
Sally          Railey               65      70      75      78   72.00       C
George         Michael               0     100     100       0   50.00       F
******************************************************************************
Class Average:                   73.09   86.18   85.27   79.18
Class Max:                         100     100     100      99
Class Min:                           0      70      65       0
==============================================================================
Total As:                   36%
Total Bs:                   27%
Total Cs:                   18%
Total Ds:                    9%
Total Fs:                    9%
==============================================================================


Current Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
=============================================================================
fname          lname             test1   test2   test3   test4      avg.   grade
=============================================================================
Johnny                 Lang
Suzie                 Wang
Allie                Smith
George              Michael
Henry               Miller
Billie                Jeans
Hillary              Clinton
Sally               Railey
Tom                Jerry
Bill                 Kool
Alice                 Wood


Code Snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
		if (!fout.fail()) { // if the output file has been opened successfully:
			// Writes to output file
			// Line 1:
			fout << setfill('=') << setw(78) << '\n';
			// Line 2:
			fout << "fname" << setfill(' ') << setw(15) << "lname"
				<< setw(18) << "test1" << setw(8) << "test2"
				<< setw(8) << "test3" << setw(8) << "test4"
				<< setw(10) << "avg." << setw(8) << "grade"
				<< '\n';
			// Line 3:
			fout << setfill('=') << setw(78) << '\n';
			// Lines with Data:

			for (int i = 0; i < arrSize - 1; i++) {
				fout << setfill(' ');
				// Each Line with Data: (THE CODE BELOW IS THE PROBLEM CODE)
				fout << right << arr[i].fName <<
					setw(21) << arr[i].lName << '\n';

			}
			// Closing output file
			fout.close();
		}
Last edited on
1
2
3
4
fout << left  << setw(21) << a[i].fName         << ' '
              << setw(21) << a[i].lName         << ' '
     << right << setw( 8) << a[i].someNumber    << ' '
              << setw( 8) << a[i].anotherNumber << '\n';

Note that it's important to output an actual space between the values. Otherwise if a value fills its field it will run-on with the next one.

For the floating point value, you're also going to want to do a

 
cout << fixed << setprecision(2);

It only needs to be done once.
Last edited on
@tpb

Ahh okay. Thank you!
Topic archived. No new replies allowed.