?
Solved

Another simple but confused question...

Posted on 2004-04-16
7
Medium Priority
?
208 Views
Last Modified: 2010-04-01
In the following progrm, I set the precision to 2 initially. That is effective to the whole block.
Then I setn the fill('*'). That is also effective until end of the block after setting once. But why
if that the cout.width(10) is not effective when I set it for one time at the very top? I, infact
have to have the cout.width(10) in every line before cout(ing.)

Tryrunning after commenting out the 2nd, 3rd and 4th cout.width(10), but having just the first one.
Then try uncommenting all cout.width(10).

#include <iostream>
using namespace std;

main()
{
  cout.precision(2);

  cout.width(10);
  cout << 10.12345 << endl;       

  cout.fill('*');
  cout.width(10);
  cout << 10.12345 << endl;       

  //field width applies to string too.
  cout.width(10);
  cout<<"Hi!" << endl;            

  cout.width(10);
  cout.setf(ios::left);       

  cout << 10.12345;            

  return 0;
}
0
Comment
Question by:prain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
nonubik earned 100 total points
ID: 10842882
operator << sets the width to 0. (that's its implementation in ostream.h)
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10842968
Hi nonubik,
> operator << sets the width to 0. (that's its implementation in ostream.h)

Yes. Try this:
  cout.width(10);
  cout << 10.12345 <<endl;      
  cout << 10.12345 <<endl;      


Cheers,
Stefan
0
 
LVL 6

Assisted Solution

by:Mafalda
Mafalda earned 100 total points
ID: 10843866
You could use this

#include <iomanip>
using std::setw;
using std::setfill;

cout.precision(10);
cout << setfill('*') << setw(10) << 10.12345 << endl << setw(10) << 10.12345 << endl << setw(10) << 10.12345 << endl;
0
 
LVL 16

Expert Comment

by:nonubik
ID: 11134718
I think I gave a straight ansewr....
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

765 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