Home C++ Iostream Functions noshowpos C++ noshowpos Manipulator Definition In C++, noshowpos turns off showpos. After it, positive numbers print without a plus sign again, which is the normal way.
You often use it to show one value with a sign and the following values without. It comes from <ios>, which <iostream> includes.
This is a manipulator . You do not call it on its own. You send it into a stream with << or >>, and it changes how that stream behaves from then on (or does one small job, like ending a line).
Syntax Put std::noshowpos into the stream.
std::cout << std::showpos << 5; // +5
std::cout << std::noshowpos << 5; // 5 Call What it does std::cout << std::noshowposPositive numbers have no plus sign. std::cout << std::showpos << d << std::noshowposSigns for one value, then back to normal. std::cout << std::noshowpos << 1.5Prints 1.5.
New streams already start with noshowpos. It only matters after showpos was used.
Library declarations ios_base& noshowpos(ios_base& str); Parameters Parameter Description strThe stream. It is passed in for you when you write cout << noshowpos. You never write this argument yourself.
Return Value Return Value Description ios_base&A reference to the same stream, so you can keep chaining << or >> after noshowpos.
Important
noshowpos never removes a minus sign. It only stops the extra plus sign. Because settings stay on the stream, call noshowpos at the end of a function that used showpos, so the rest of the program is not affected.
C++ Examples 1. Switch it off 2. Change and total 3. Decimal numbers Example 1 — Turn Off the Plus Sign Print the same number twice, once for each mode.
#include <iostream>
int main() {
std::cout << std::showpos << 1 << ' ';
std::cout << std::noshowpos << 1 << '\n';
return 0;
} How It Works showpos is on first, so the first 1 appears as +1.noshowpos follows in the second statement and turns the plus sign off.The second 1 prints as a plain 1. Example 2 — Sign for the Change, Plain Total A report that shows the change with a sign and the total without one.
#include <iostream>
int main() {
int delta = 5;
int total = 105;
std::cout << "change: " << std::showpos << delta
<< std::noshowpos << ", total: " << total << '\n';
return 0;
} How It Works showpos is turned on just before delta, so it prints as +5.noshowpos is put in straight after it.total is then printed in the normal way: 105, with no plus sign.Example 3 — noshowpos with Decimal Numbers The setting works for floating-point numbers in the same way.
#include <iostream>
int main() {
std::cout << std::showpos << 1.5 << ' ';
std::cout << std::noshowpos << 1.5 << ' ';
std::cout << -1.5 << '\n';
return 0;
} How It Works With showpos, 1.5 prints as +1.5. After noshowpos, the same 1.5 prints with no sign. The last number -1.5 is negative. Its minus sign stays: noshowpos never removes it. Frequently Asked Questions What does noshowpos do in C++? It turns off the plus sign for positive numbers that showpos switched on.
Is noshowpos the default? Yes. A stream does not print a plus sign unless you turn showpos on.
Does noshowpos remove minus signs? No. Negative numbers always keep their minus sign.
Can I show a plus sign for only one value? Yes. Put showpos before the value and noshowpos right after it.
What is the same as noshowpos? cout.unsetf(std::ios::showpos) does the same thing.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion