Solved

Another simple but confused question...

Posted on 2004-04-16
7
204 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Which IDE to use to begin C++ training? 5 70
Unresolved External Symbols 3 130
c++ reading data from file into two dimensional array 3 132
gdb doesn't stop on breakpoint 2 114
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

733 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