the "Type" is in same column, because i know the string size. but the "Position Line: " isn't in same column, because, in these case, i don't know the type size(Tokens[i].Type).
so how can show the "Position Line: " in same line?
// Example program
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using std::cout;
using std::right;
using std::left;
using std::setw;
using std::fixed;
struct Token {
std::string Token;
std::string Type;
};
int main()
{
std::vector<Token> Tokens = {
{ "var", "KeyWord" },
{ "const", "KeyWord" },
{ "varname", "ID" },
// ...
};
cout <<"\n" << "show tokens\n";
//cout << std::setfill('*');
for(unsignedint i=0;i<Tokens.size(); i++)
{
std::string token_column = "Token: " + Tokens[i].Token;
std::string token_type_column = "Type: " + Tokens[i].Type;
cout << setw(20) << left << token_column << setw(20) << token_type_column << "Position Line: " << i <<"\n";
}
}
show tokens
Token: var Type: KeyWord Position Line: 0
Token: const Type: KeyWord Position Line: 1
Token: varname Type: ID Position Line: 2
But would of course like to see your solution, especially if it doesn't require the contents of a single column to be manually concatenated beforehand.
There is no way to avoid building the content of a column, though, as setw() works on a single item. Where it is important a simple function/lambda or local ostringstream or string concatenation will do.
I disagree. You get the same limitations with printf() as you do with std::cout in terms of space formatting. (What printf() functions have going for them is making life much more succinct. And with modern C++ template magic, you can still get type genericity, which is nice.)
std::setw() applies to the next thing actually written to stream.
It tells how many character columns minimum to use, padding with the fill character if necessary.
std::left and std::right tell which side to pad (indirectly: std::left means pad on the right, because the output text sticks to the left.)
When designing any kind of organized output, such as columnar output, it is always useful to get a text editor and start counting things. Even better: use some graph paper so you can draw extra stuff around the columns to help keep track.
Duthomhas: C\C++ don't have GotoXY(), ANSI, for that... and is very useful.
(unless we use the API Console functions)
but, in these case, is what i really need.
thank you so much for all to all
Turbo C++ was designed to work on an entirely different system. It was natural for programming languages to come bundled with a TUI library, since GUI as we know it today didn’t exist.
I suppose I ought to put up my Simple Cross-Platform Console I/O library at some point...
It is shockingly easy to do all that stuff and more.
(Though, making clipboard access work on X takes a some 150 LOC + threads.)