How do I show a positive/negative sign on everything but 0 when outputting to a stream?
c++, formatting, stream
Solution
Just use an if statement to check if the value is 0 or not. If it is, print zero, otherwise print as you were with showpos.
I don't believe there is a shortcut for this, but the above is pretty easy.
Sample code
if(n == 0) {
cout << '0';
} else {
cout << showpos << n;
}
Problem
I would like to output my numbers in one of three following formats: ``` -1 0 +1 ``` but the stream flag `showpos` only allows ``` -1 +0 +1 ``` Are there any easy shortcuts around this?