I want to set fmtflag

M

mohamed azaz

hi
I want to set a format flag what is the right code


example


format flag as (right)or(left)


thank you


bye
 
J

Jeff Schwab

mohamed said:
> I want to set a format flag what is the right code
> example
> format flag as (right)or(left)

It is not clear what you need. Could you please post some sample code,
or some pseudo-code showing what you need? A small example, with
comments instead of code for the part you're not sure about, would be a
big help.
 
M

mohamed azaz

cout.setf(ios::left); // left justify output text
cout >> "This text is left-justified";
cout.unsetf(ios::left); // return to default (right justified)

this code doesn't work
 
J

Jeff Schwab

mohamed said:
cout.setf(ios::left); // left justify output text
cout >> "This text is left-justified";

cout << "This text is left-justified";
cout.unsetf(ios::left); // return to default (right justified)

this code doesn't work

The problem has nothing to do with the flags. You have used the
extraction operator (>>) when you needed the insertion operator (<<).
The arrows indicate the direction of information flow: During
extraction, a value is moved from the stream to the variable (stream >>
variable). During insertion, a value is moved from the variable to the
stream (stream << variable).
 
J

James Kanze

cout.setf(ios::left); // left justify output text
cout >> "This text is left-justified";
cout.unsetf(ios::left); // return to default (right justified)
this code doesn't work

That's because justification isn't just a flag, it's a field
with three different possible values. So you have to use the
two argument form of setf:

std::cout.setf( std::ios::left, std::ios::adjustfield ) ;
// ...
std::cout.setf( std::ios::right, std::ios::adjustfield ) ;

Note too that you normally want to restore the field when you're
through, not set it to some arbitrary default value. This is
best done by an RAII class, which saves the original value in
the constructor, and restores it in the destructor.

And of course, you don't usually use these commands except in
user defined manipulators. Which, if correctly designed, can
restore the original values at the end of the full expression.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,516
Latest member
ChrisHibbs

Latest Threads

Top