Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Another simple but confused question...

Posted on 2004-04-16
7
Medium Priority
?
212 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 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 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…
Suggested Courses

597 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