Formatting output to resemble a table in C++ -
i using sprintf format data. same output when formatted using printf works cannot use printf using output data send email.
for(...) { sprintf(sluns, "%-50s%-50s%-50s%-3d%-14s", str1, str2, str3, int1, str4); string srow(sluns); stable = stable + "\n" + srow; }
the output of stable looks below. width not constant columns. because converting row elements c string ?
name1 str1 str2 10 str3 name1 str1 str2 10 str3 name111 str1 str2 10 str3
i see have c++
tag on question. i'll recommend use c++
.
you can use left , setw iomanip
achive want.
example:
#include <string> #include <iostream> #include <strstream> #include <iomanip> using namespace std; string str1 = "str1"; string str2 = "str2"; string str3 = "str3"; string str4 = "str4"; int = 10; int main() { strstream str; str << left << setw(10) << str1 << setw(10) << str2 << setw(10) << str3 << setw(10) << << setw(10) << str4 << endl; cout << str.rdbuf() << endl; }
Comments
Post a Comment