Solved

Another simple but confused question...

Posted on 2004-04-16
7
205 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 25 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 25 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 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 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.

688 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