How to Easily Format My Data Table in C++

Smart way to format tables on stdout in C

printf may be the quickest solution:

#include <cstdio>

int a[] = { 22, 52352, 532 };

for (unsigned int i = 0; i != 3; ++i)
{
std::printf("%6i %6i\n", a[i], a[i]);
}

Prints:

    22     22
52352 52352
532 532

Something similar can be achieved with an arduous and verbose sequence of iostream commands; someone else will surely post such an answer should you prefer the "pure C++" taste of that.


Update: Actually, the iostreams version isn't that much more terrible. (As long as you don't want scientific float formatting or hex output, that is.) Here it goes:

#include <iostreams>
#include <iomanip>

for (unsigned int i = 0; i != 3; ++i)
{
std::cout << std::setw(6) << a[i] << " " << std::setw(6) << a[i] << "\n";
}

C++ and table format printing

You can use the std::setw manipulator for cout.

There's also a std::setfill to specify the filler, but it defaults to spaces.

If you want to center the values, you'll have to do a bit of calculations. I'd suggest right aligning the values because they are numbers (and it's easier).

cout << '|' << setw(10) << value << '|' setw(10) << value2 << '|' << endl;

Don't forget to include <iomanip>.

It wouldn't be much trouble to wrap this into a general table formatter function, but I'll leave that as an exercise for the reader :)

How can I print a table if the format below?

Since another answer has already provided a full solution, I will now provide my own full solution.

The advantage of my solution is that less if checks are necessary, because I have placed the code which jumps several columns outside the main loop. That way, it is not necessary to constantly check inside the main loop whether we must jump several columns. Also, I make more use of the control flow of the program, so that in the main loop, the only conditions that I must check for are

  1. whether the end of the line has been reached, and
  2. whether the end of the array has been reached.

However, the disadvantage of my solution is that my two nested for loops are harder to understand, because they have a non-standard structure.

#include <stdio.h>

void print_table( int start_year, int values[], int num_values )
{
int col;
int written = 0;

//don't print anything if array is empty
if ( num_values <= 0 )
return;

//calculate quotient and remainder of dividing
//start_year by 10
int quotient = start_year / 10;
int remainder = start_year % 10;

//print header
printf(" 0 1 2 3 4 5 6 7 8 9\n");

//prepare first line for data
printf( "%4d", quotient );
for ( col = 0; col < remainder; col++ )
{
printf( " " );
}

//process one row per loop iteration
for ( int row = 1; ; row++ )
{
//process one column per loop iteration
for ( ; col < 10; col++ )
{
printf( " %3d", values[written++] );

//check whether we have reached the end of the array
if ( written == num_values )
{
putchar( '\n' );
return;
}
}

//prepare next line for data
printf( "\n%4d", quotient + row );
col = 0;
}
}

int main( void )
{
int arr[] = {
18, 23, 22, 21, 20,
18, 24, 23, 22, 20, 19, 18, 24, 22, 21,
20, 19, 24, 23, 22
};

print_table( 2015, arr, sizeof arr / sizeof *arr );
}

This program has the following output:

       0   1   2   3   4   5   6   7   8   9
201 18 23 22 21 20
202 18 24 23 22 20 19 18 24 22 21
203 20 19 24 23 22

Format output in a table, C++

Can't you do something very similar to the C# example of:

String.Format("|{0,5}|{1,5}|{2,5}|{3,5}|", arg0, arg1, arg2, arg3);

Like:

printf("|%5s|%5s|%5s|%5s|", arg0, arg1, arg2, arg3);

Here's a reference I used to make this: http://www.cplusplus.com/reference/clibrary/cstdio/printf/

How To: Best way to draw table in console app (C#)

You could do something like the following:

static int tableWidth = 73;

static void Main(string[] args)
{
Console.Clear();
PrintLine();
PrintRow("Column 1", "Column 2", "Column 3", "Column 4");
PrintLine();
PrintRow("", "", "", "");
PrintRow("", "", "", "");
PrintLine();
Console.ReadLine();
}

static void PrintLine()
{
Console.WriteLine(new string('-', tableWidth));
}

static void PrintRow(params string[] columns)
{
int width = (tableWidth - columns.Length) / columns.Length;
string row = "|";

foreach (string column in columns)
{
row += AlignCentre(column, width) + "|";
}

Console.WriteLine(row);
}

static string AlignCentre(string text, int width)
{
text = text.Length > width ? text.Substring(0, width - 3) + "..." : text;

if (string.IsNullOrEmpty(text))
{
return new string(' ', width);
}
else
{
return text.PadRight(width - (width - text.Length) / 2).PadLeft(width);
}
}

How to change column format in DataTable C#

As Jeff mentioned in his answer, You cannot change the DataType after the Datatable is filled with data. What you can do is, take a clone of the Data table, change the column type and load data from the original data table to the cloned table as follows.

DataTable dtCloned = tableone.Clone();
dtCloned.Columns[1].DataType = typeof(string); //In your case you need to change WaitTime and AssistTime
dtCloned.Columns[2].DataType = typeof(string);

foreach (DataRow row in tableone.Rows)
{
dtCloned.ImportRow(row);
}

Then you can use your code as,

dtCloned.Select().ToList().ForEach(row =>
{
double xxx = Convert.ToDouble(row["WaitTime"]);
row.SetField("WaitTime", secondsToTime(xxx));
});


Related Topics



Leave a reply



Submit