format - Query regarding formatting output in C++ -
this question has answer here:
- c++ alignment when printing cout << 6 answers
i want output per follows in c++:
name aabsd size in kb 170 width , height 512 512 cout<<"\n \t name "<<std::setw(15)<<filename; cout<<" \n \t size in kb "<<std::setw(10)<<size; cout<< " \n \t width , height "<<std::setw(3)<<width<<" "<<height;
values on right side should aligned in same coloumn. tried setw() not give me output aligned because left side text different.
another answer relies on purely c++ specific constructs.
#include <iostream> #include <iomanip> int main() { char const* filename = "abcd"; int size = 10; double width = 20; double height = 30; // std::left says align output left when writing next field // set::setw(20) says use 20 characters next field. std::cout << std::left << std::setw(20) << "name" << filename << std::endl; std::cout << std::left << std::setw(20) << "size in kb" << size << std::endl; std::cout << std::left << std::setw(20) << "width , height" << width << " " << height << std::endl; return 0; }
Comments
Post a Comment