How does the setw stream manipulator (space count) work? When there is one \t, for example, I want to print a with four spaces, so I use \t and I compare \t with setw.
The code that I wrote:
# include <iostream>
# include <iomanip>
int main()
{
std::cout<<"\t"<<"a\n";
std::cout<<std::setw(9)<<"a\n";
return 0;
}
Output:
a // This is 1 '\t'
a // This is setw()
So what I thought was:
setw(18) = \t\t
The code worked. But when I deleted the \n, it did not become one straight line.
# include <iostream>
# include <iomanip>
int main()
{
std::cout<<"\t\t"<<"a\n";
std::cout<<std::setw(18)<<"a";
return 0;
}
It gives me this output:
a
a
What's wrong?
That's because you need to add a
\nat thesetw(18). And this applies to anysetw.Sample code:
Output:
And another solution is:
And the output will be the same.
The reason behind why should we put the
\nor a space is because: