Solved

Formatting I/O

Posted on 2006-10-22
2
235 Views
Last Modified: 2010-04-01
Hello all,

I have a simple question regarding how to perform some formatting in c++. I know you can use setw(xx) or width(), but when i have the following case below, things get out of hand:

Example:

Text              10
MoreText               10.

Basically i want to able to line things up, so in the above example i would like the number 10 to be lined in the same column as the one before, but this does not seem to be happening. I've currently got code like this:

cout << "Text" << setw(10) << 10<< endl;
cout << "MoreText" << setw(10) << 10<< endl;

Is their a way round this? I know the reason why this is happending, but dont know of a way to control formatting of this nature.

Thanks
0
Comment
Question by:lcoolsingh
2 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 17783445
Hi lcoolsingh,
> cout << "Text" << setw(10) << 10<< endl;
> cout << "MoreText" << setw(10) << 10<< endl;

    cout << setw(20)<< "Text" << setw(10) << 10<< endl;
    cout << setw(20)<< "MoreText" << setw(10) << 10<< endl;

Cheers!
sunnycoder
0
 

Author Comment

by:lcoolsingh
ID: 17783640
But that means that the first ends up like this:

         Text            10
  MoreText            10

I think what needs to be done is to use setiosflags(ios::left).

thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now