| | |
Justification
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
You need to know how long the string is, and how wide the available field space is. Then take the difference of that and if it's greater than 0, prepend that number of spaces onto the string:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <string> std::string right_justify ( const std::string& s, std::string::size_type field_width ) { std::string::difference_type whitespace = field_width - s.size(); if ( whitespace > 0 ) return std::string ( whitespace, ' ' ) + s; return s; } int main() { const int N = 10; std::cout<< std::right << std::setfill ( ' ' ) << std::setw ( N ) << "12345" <<'\n'; std::cout<< right_justify ( "12345", N ) <<'\n'; }
I'm here to prove you wrong.
![]() |
Similar Threads
- Help in Creating a Forum (Web Hosting Deals)
- Text Justification Algorithm (Computer Science)
- Right Justified / decimal point alignment (C)
- IE 6.0 The page cannot be displayed error (Viruses, Spyware and other Nasties)
- On your site - How many ads are too many? (Advertising Sales Strategies)
- Overclocking 2600+.. help? (Motherboards, CPUs and RAM)
Other Threads in the C++ Forum
- Previous Thread: a department-store customer progam
- Next Thread: C++ Server: Multi-thread VS Single-thread
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






