Home >Backend Development >C++ >How can I easily format data tables in C using `setw()`, `setfill()`, and `left` (or `right`)?

How can I easily format data tables in C using `setw()`, `setfill()`, and `left` (or `right`)?

Barbara Streisand
Barbara StreisandOriginal
2024-11-17 21:14:02663browse

How can I easily format data tables in C   using `setw()`, `setfill()`, and `left` (or `right`)?

Easily Formatting Data Tables in C

When working with data tables, it's often necessary to format them in a way that makes them easy to read and interpret. C provides several functions in the library to assist in this task.

The functions setw(), setfill(), and left (or right) work together to control the width, fill character, and alignment of data elements. By using these functions judiciously, you can create formatted tables with minimal effort.

For example, consider the following unformatted table:

Bob Doe     10.96      7.61     14.39      2.11     47.30     14.21     44.58      5.00     60.23
Helen City     10.44      7.78     16.27      1.99     48.92     13.93     53.79      5.00     70.97
Joe Green     10.90      7.33     14.49      2.05     47.91     14.15     44.45      4.70     73.98

To format the table as desired:

const char separator = ' ';
const int nameWidth = 6;
const int numWidth = 8;

cout << left << setw(nameWidth) << setfill(separator) << "Bob";
cout << left << setw(nameWidth) << setfill(separator) << "Doe";
cout << left << setw(numWidth) << setfill(separator) << 10.96;
cout << left << setw(numWidth) << setfill(separator) << 7.61;
cout << left << setw(numWidth) << setfill(separator) << 14.39;
cout << left << setw(numWidth) << setfill(separator) << 2.11;
cout << left << setw(numWidth) << setfill(separator) << 47.30;
cout << left << setw(numWidth) << setfill(separator) << 14.21;
cout << left << setw(numWidth) << setfill(separator) << 44.58;
cout << left << setw(numWidth) << setfill(separator) << 5.00;
cout << left << setw(numWidth) << setfill(separator) << 60.23;
cout << endl;

This code will produce the formatted table:

Bob           Doe        BLR  10.96   7.61  14.39   2.11  47.30  14.21  44.58   5.00  60.23  4:27.47
Helen         City       CUB  10.90   7.33  14.49   2.05  47.91  14.15  44.45   4.70  73.98  4:29.17
Joe           Green      USA  10.44   7.78  16.27   1.99  48.92  13.93  53.79   5.00  70.97  5:06.59

To further simplify the code, you can use a template function to handle the formatting:

template<typename T> void printElement(T t, const int&amp; width)
{
    cout << left << setw(width) << setfill(separator) << t;
}

Using this template function, you can format elements as follows:

printElement("Bob", nameWidth);
printElement("Doe", nameWidth);
printElement(10.96, numWidth);
printElement(17.61, numWidth);
printElement(14.39, numWidth);
printElement(2.11, numWidth);
printElement(47.30, numWidth);
printElement(14.21, numWidth);
printElement(44.58, numWidth);
printElement(5.00, numWidth);
printElement(60.23, numWidth);
cout << endl;

By combining the power of setw(), setfill(), left (or right), and template functions, you can easily format data tables in C without the need for complex calculations.

The above is the detailed content of How can I easily format data tables in C using `setw()`, `setfill()`, and `left` (or `right`)?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn