Freelancer job part time jobs

  • Home
  • Jobs hiring now
  • FAQ
  • Contact

Formatting Cout Output in C++ programming language using iomanip

January 9, 2011 by Freelance Leave a Comment

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Google+ (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • More
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to email this to a friend (Opens in new window)
  • Click to print (Opens in new window)

In C++, if you want to get output then cout is used. All functions are in iomanip. You can call by the use of std.
Handling with Spacing problems with the use of iomanip
If you want to get an output in a well organized way, then it is necessary to give proper spaces. In such a program, things should be adjusted accurately. So, here is some information related to spacing.
Arranging the turf breadth with the help of setw
For arranging smallest amount of gaps among outputs, std::setw is used. it puts insertion operator between the output. It gives the space of an int. if the input is less than the space, then gap will adjusted for stuffing.

using namespace std;
cout<<setw(11)<<"eleven"<<"five"<<"five";

It will give output in this way:

eleven       fivefive

If there are lengthy constituent then it is probable to indicate the length for output during execution. Even you can alter the stuffing with the use of setfill. It is clear that setfill can be used for operating “stream”.

cout<<setfill('-')<<setw(81)<<"-"<<endl;

With the use of this code, padding char looks like a dash. Now, the breadth of the coming output will be approximately 81. Now the result will be:

--------------------------------------------------------------------------------

As the padding char has become a dash so you have to change it for new execution.
Arranging the text in a line with the help of iomanip
You can indicate the output to right or left with the help of flags.
Using the facts of “iomanip”:
We have an idea of gaps and alignment, so our data will be printed rightly.

using namespace std;

struct person
{
    string firstname;
    string lastname;
};

Your output will be arranged in a pleasant way if we have vector related to the persons.

vector<person> people;

int field_one_width = 0, field_two_width = 0;


for ( vector<person>::iterator iter = people.begin();
      iter != people.end();
      ++iter )
{
    if ( iter->firstname.length() > field_one_width )
    {
        field_one_width = iter->firstname.length();
    }
    if ( iter->lastname.length() > field_two_width )
    {
        field_two_width = iter->lastname.length();
    }
}


for ( vector<person>::iterator iter = people.begin();
      iter != people.end();
      ++iter )
{
    cout<<setw(field_one_width)<<left<<iter->firstname;
    cout<<" ";
    cout<<setw(field_two_width)<<left<<iter->lastname;
}

We can locate new space for lengthy names by the use of setw.
Publishing Figures
If you want to get an authentic output, then it is better to write a value leading to the prefix “ox”.
In order to locate the utmost numbers, it is suggested to use setprecision.

std::cout << setprecision(3) << 2.71828;

It is seen that the setprecision modifies the precision awaiting for the new turn when it will be surpass
to a stream.

 2.71 1.41

production in diverse bases
The function setbase will give back a value in the form of 10, 16 or 8. The key in will be interpret as num but will be published in particular base.

std::cout << setbase(18) << 36;

In spite pf writing the full name, you can write it as dec, hex, and oct. the digits in decimal form will get published as it is.

std::cout <<
std::cout<< setbase(18) << 36;

Now, you are able enough to get your output without writing “printf”.

100% Bonus

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Google+ (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • More
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to email this to a friend (Opens in new window)
  • Click to print (Opens in new window)
Strings in C programming language
Using ofstream and ifstream as main input output C programming classes
Singly linked lists in C++ programming language
Typecasting function in C programming language
How to use Functions in C++ programming language
Getting Random Values in C and C++ programming language with Rand
How to use Switch Case in C++ programming language
Recursion in C++ programming language
Basic lesson for C++ programming language
How to use If statements in c++ programming language
Understanding Initialization Lists in programming language C++
How to use Loops in C++ programming language
Array basics in C programming language

Filed Under: C++

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

CAPTCHA
Refresh

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Earn money as trader

img

Earn from home ads

img

Work from home ads

img

Work from home as instaforex forex trader

InstaForex

Freelance categories

  • Earn money as webmaster
    • Advertising
    • Affiliate program
    • Amazon
    • Bidvertiser
    • Blogging
    • Facebook
    • Google Adsense
    • Hosting
    • Komoona
    • LinkedIn
    • Neteller
    • SEO
    • Skrill Moneybookers
    • Twitter
  • Education
    • How to do
    • Job description glossary
    • Wordpress how to do
  • Freelancer advice tips
    • Forex trading from home
      • Affiliate programs
        • 500affiliates
        • Avapartner
        • Etoropartners affiliate program
        • HotForex Affiliate program HFaffiliates
      • Binary options trading
        • Anyoption
        • OptionBit Review
        • TradeRush
        • Ubinary
        • Yesoption
      • Facebook IPO price – buy facebook stock
      • Forex brokers reviews
        • 4XP Review
        • Alpari Russia Review
        • Alpari UK
        • AvaTrade Review
          • AvaFx Review
        • DeltaStock Review
        • Directfx Review
        • DukaScopy Review
        • Easy Forex Review
        • Etoro Review
        • ETX Capital Review
        • Exness
        • Forex.com Review
        • Forex4you
        • ForexYard Review
        • FXCM Review
        • FXDD Review
        • FxOpen Review
        • Fxpro Review
        • HiWayFX review
        • Hotforex Review
        • HY Markets Review
        • IC Markets Forex Review
        • IFC Market
        • IForex Review
        • IG Markets
        • Instaforex Review
        • Ironfx Review
        • Keystock Review
        • LCG review
        • Liteforex broker
        • Marketiva Review
        • Markets.com Review
        • NordFx Review
        • Pepperstone Review
        • Plus500 review
        • Roboforex
        • SwissQuote Bank Review
        • Thinkforex
        • UFX Markets review
        • Windsor Brokers Review
        • XeMarkets Review
        • xForex
        • Xtrade Review
        • Youtrade FX Review
        • Zulutrade Review
    • Freelance glossary
    • Freelance Programming tips
    • Learn about programming
      • C++
      • Linux
      • PHP
      • Ruby on rails
      • SQL
    • Top freelance websites
    • Web Design tips
    • Work from home business
    • Writing tips
  • Freelancer job news
    • Job advice
    • Thinking
    • Worldwide job offers
  • IT news
  • Jobs

Best Forex Brokers Reviews

  • Avafx Review – how to earn in forex market using Avafx Forex Broker
  • Avatrade Review
  • eToro Review
  • Alpari NZ Review
  • InstaForex Review
  • DukaScopy Review – Swiss bank Forex broker
  • Forex.com Review – how to earn money in forex market as trader
  • Ironfx Review
  • FXCM Review
  • FxOpen Review
  • Hotforex Review
  • Markets.com Review
  • UFX Markets Review
  • Zulutrade Review – earn money using free forex signals where forex experts trade for you
  • Exness forex broker Review
  • Fxpro Review
  • LiteForex forex broker review
  • NordFx Review and bonus
  • Roboforex forex broker Review
  • XM.com Review - Xemarkets forex broker review

Navigation

  • Home
  • Jobs hiring now
  • FAQ
  • Contact

Languages

  • Arabic language
  • Bengali language
  • Communiqué de presse
  • China language
  • Pressemitteilung
  • Japanese language
  • Comunicado de imprensa
  • Russian language
  • Presione soltar
  • Turkish
  • Comunicato stampa
  • Hindi language

Recent articles

  • IronFX Review
  • How to earn money as freelancer ?
  • LCG broker Review
  • Forex vs. stocks – Which is a Better for Trading ?
  • Thinkmarkets Review
Freelancer job - Copyright 2007
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.