How can I output data to the console in a table in C++? There's a question for this in C#, but I need it in C++.
This, except in C++: How To: Best way to draw table in console app (C#)
We use the scanf() function for getting the formatted inputs or standard inputs so that the printf() function can provide the program with numerous options of conversion. We use the printf() function for generating the formatted outputs or standard outputs in accordance with a format specification.
%s refers to a string %d refers to an integer %c refers to a character. Therefore: %s%d%s%c\n prints the string "The first character in sting ", %d prints i, %s prints " is ", and %c prints str[0].
%d is a signed integer, while %u is an unsigned integer. Pointers (when treated as numbers) are usually non-negative. If you actually want to display a pointer, use the %p format specifier.
printf("%%d") , or just fputs("%d", stdout) .
I couldn't find something I liked, so I made one. Find it at https://github.com/haarcuba/text-table
Here's an exmaple of its output:
+------+------+----+
| |Sex | Age|
+------+------+----+
|Moses |male |4556|
+------+------+----+
|Jesus |male |2016|
+------+------+----+
|Debora|female|3001|
+------+------+----+
|Bob |male | 25|
+------+------+----+
Here's a small sample of what iomanip has:
#include <iostream>
#include <iomanip>
int main(int argc, char** argv) {
std::cout << std::setw(20) << std::right << "Hi there!" << std::endl;
std::cout << std::setw(20) << std::right << "shorter" << std::endl;
return 0;
}
There are other things you can do as well, like setting the precision of floating-point numbers, changing the character used as padding when using setw, outputting numbers in something other than base 10, and so forth.
http://cplusplus.com/reference/iostream/manipulators/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With