print + if number is positive

How do you make a positive number, that, if you print it out, also has a + infront of it, for example:
1
2
int a = 4;
cout << a;

prints "+4"
Well... you might do it with if else:

1
2
3
4
5
if ( a >= 0 ) {
cout << "+" << a << endl;
}else {
cout  << a << endl;
}
Last edited on
Isnt there another way?
Include <iomanip> and do
1
2
int a = 4;
cout << showpos << a;
Last edited on
Topic archived. No new replies allowed.