showpos problem

F

fb

Hi everyone...

I have a small problem with showpos placing a '+' in the wrong spot.

What I have is:
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
....
....
I want to place a '+' in front of all these as so:
+0000 +0000 +0000 +0000 +0000 etc...
....
....

The problem with the code below is that the '+' sign is placed in a
wierd spot. Something like: 00+0

(notes: 'Accumulator' is an int that holds a value from -9999 to 9999)

cout << "accumulator: " << showpos << setw(4) << setfill('0') <<
Accumulator << endl;

Thanks.
 
D

Dietmar Kuehl

fb said:
The problem with the code below is that the '+' sign is placed in a
wierd spot. Something like: 00+0

(notes: 'Accumulator' is an int that holds a value from -9999 to 9999)

cout << "accumulator: " << showpos << setw(4) << setfill('0') <<
Accumulator << endl;

Why do you think it is placed in a weird spot? The fill character
is not part of the value but intended to be something like e.g.
a space. You can, however, still arrange this formatting by using
"internal" for the adjustment which insert the fill character at
some appropriate internal point:

| std::cout << std::showpos << std::setw(4) << std::setfill('0')
| << std::internal << 0 << '\n';
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top