Aligning output on the right in c++ -
#include<iostream> #include<iomanip> using namespace std; int main() { cout << right << "hello" << setw(10) << "world\n"; cout << right << "goodbye" << setw(10) << "world\n"; }
why result in output like:
hello world goodbye world
rather than:
hello world goodbye world
what doing wrong here?
edit:
#include<iostream> #include<iomanip> using namespace std; int main() { cout << "hello" << " world"; cout << right << setw(10) << "today\n"; cout << "goodbye" << " world"; cout << right << setw(10) << "today\n"; }
if try this, why "today" part misaligned?
change order of operators solve problem:
#include <iostream> #include <iomanip> int main() { std::cout << std::left << std::setw(10) << "hello" << "world\n"; std::cout << std::left << std::setw(10) << "goodbye" << "world\n"; return 0; }
- you have place operators before value format.
- avoid using
using namespace std
.
the std::setw()
operator sets field next value. , std::left
or std::right
operators set position of value in field.
this example
std::cout << std::left << std::setw(10) << "word1" << std::right << std::setw(20) << "word2" << std::endl;
will create kind of formatting:
aaaaaaaaaabbbbbbbbbbbbbbbbbbbb word1 word2
you see there first "field" 10 characters in first text placed, , second "field" 20 characters in second word placed right aligned. if text in first field longer field, happens:
aaaaaaaaaa....bbbbbbbbbbbbbbbbbbbb word1istoolong word2
the second field shifted number of characters. stream never keeps track of current position, build output using "fields" of given size.
to justify text given page with, use code this:
#include <iostream> #include <sstream> #include <list> const int pagewidth = 78; typedef std::list<std::string> wordlist; wordlist splittextintowords( const std::string &text ) { wordlist words; std::istringstream in(text); std::string word; while (in) { in >> word; words.push_back(word); } return words; } void justifyline( std::string line ) { size_t pos = line.find_first_of(' '); if (pos != std::string::npos) { while (line.size() < pagewidth) { pos = line.find_first_not_of(' ', pos); line.insert(pos, " "); pos = line.find_first_of(' ', pos+1); if (pos == std::string::npos) { pos = line.find_first_of(' '); } } } std::cout << line << std::endl; } void justifytext( const std::string &text ) { wordlist words = splittextintowords(text); std::string line; (wordlist::const_iterator = words.begin(); != words.end(); ++it) { if (line.size() + it->size() + 1 > pagewidth) { // next word doesn't fit line. justifyline(line); line.clear(); line = *it; } else { if (!line.empty()) { line.append(" "); } line.append(*it); } } std::cout << line << std::endl; } int main() { justifytext("this small code sample format paragraph " "is passed justify text function fill " "selected page , insert breaks necessary. " "it working justify formatting in text " "processors."); return 0; }
this example justifies each line fill given page @ begin. works splitting text words, filling lines these words, , justifies each line match width.
Comments
Post a Comment