Solved

Formatting I/O

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SetCurrentDirectory path limit 7 121
C Language combined operators 28 109
C++ finding a sting in a char* string from a text file 3 108
Beginner to Unreal Engine 4 5 79
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

776 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