Already the computer hangs on the tabs, but I can not understand how to make a normal tab in the console. Code for example:

cout << "name" << "\t[" << "12:10:10" << "] "; cout << "\t[" << "qwe" << "]"; cout << "\t<" << "name" << "> : " << "message" << endl; cout << "name2" << "\t[" << "12:10:10" << "] "; cout << "\t[" << "rqwqsvdwdfqweqr" << "]"; cout << "\t<" << "name" << "> : " << "message" << endl; 

Expected output:

 name [12:10:10] [qwe] <name> : message name2 [12:10:10] [rqwqsvdwdfqweqr] <name> : message 

The resulting conclusion:

  name [12:10:10] [qwe] <name> : message name2 [12:10:10] [rqwqsvdwdfqweqr] <name> : message 

I have already tried the usual tabulators \t , vertical tabulators \v (which I still don’t understand how they work), used setw() , but nothing setw() . You can, of course, manually read the length of the name in brackets, and depending on the size, you can give either \t or \t\t or \t\t\t , but can you really not make it all easier?

  • Use spaces. Or look towards setw and resetiosflags - Nick
  • @entithat thanks, this question was already reviewed by me. - Vitali
  • How about using setw ? - Harry
  • @Harry использовал setw(), но ничего не выходит. show, please, the proper use of setw in my example. - Vitali

1 answer 1

Set the left alignment of the setiosflags(ios::left) and specify the width of the setw(10) .

 #include "iostream" #include <iomanip> using namespace std; int main() { cout << setiosflags(ios::left); cout << setw(10) << "name"; cout << setw(15) << "[12:10:10]"; cout << setw(20) << "[qwe]"; cout << "<name> : message" << endl; cout << setw(10) << "name2"; cout << setw(15) << "[12:10:10]"; cout << setw(20) << "[rqwqsvdwdfqweqr]"; cout << "<name> : message" << endl; return 0; } 
  • Great, thanks! - Vitali