Solved

Formatting I/O

Posted on 2006-10-22
2
240 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Move constructor only called if marked noexcept? 6 108
How to gracefully close the c++ 11 thread? 3 107
What is sub-make ? 2 71
boost::uuid crashes 17 11
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

840 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