Solved

Another simple but confused question...

Posted on 2004-04-16
7
199 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
  • 2
7 Comments
 
LVL 16

Accepted Solution

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

Expert Comment

by:stefan73
Comment Utility
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 25 total points
Comment Utility
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
Comment Utility
I think I gave a straight ansewr....
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
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.

762 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

11 Experts available now in Live!

Get 1:1 Help Now