-
setw() - left justify
im new at programming and i know that setw(x) will justify everything to the right. how can i make everything left justified? i know there is a way b/c it mentions it in the book. but they dont give any examples and i have to write a program that formats the output left justified. thanks for your help.
-
One way...
Code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string s("Hello");
cout << left << setw(10) << s << "there." << endl;
return 0;
}
-
sweet! i didnt realize that it was that easy! i wonder y a begginers book would not show this way, but show something completely outrageaous. lemme get the book and write their method .................................................. .................................................. .................................................. ...............................................
"you can left justify text output using the manipulator
setiosflags(ios::left)"
i didnt understand that. but the cout << left << setw(x) << command it so much easier!! just out of curiousity tho, how does the example the book mentions work? thanks for your help btw!
-
The example your text illustrates is the language from which the 'shortcut' version that hk_mp5kpdw provided is derived.
Unless I'm mistaken, this is compiler-dependent, however. Borland supports it, though I'm not certain that MSVC++ does, for example. (I reformatted my hard drive recently and have yet to reinstall MSVC++. Just might do that in case other questions of this sort pop up. :D )
Your source is not at all incorrect, so don't immediately start questioning its content. In fact, you may want to investigate further into the method your book demonstrates so that you know the foundations of the language rather than relying on shortcut versions that may not be portable. ;)
Btw, char data is left-justified by default while numeric data is right-justified. (Someone jump in and correct me if I've mixed the two up!) Something else to consider when you format your output, anyway.
-Skipper