Archive

Posts Tagged ‘cout’

How to force show/hide + sign for numbers?

February 13, 2009 2 comments

We know that ‘-‘ i.e. minus is always displayed for numbers but how about ‘+’ sign. Is there a way to show this sign using output streams in c++? The answer is yes! Use “showpos” function and use “noshowpos” to disable this functionality…

int main()
{
   cout << showpos   << 10 << " " << -100 << endl;    cout << noshowpos << 10 << " " << -100  << endl;    return 0; } // Output +10 -100 10 -100 [/sourcecode]

Advertisements

Replacement for CString::Format in standard C++

std::stringstream can be used as a replacement for CString::Format, if you are using CString just for the sake of Format, caveat being that it could be slow, I’ve heard a user mentioning this but not sure, I haven’t tested it out, but should be definitely better that using CString::Format and then assigning to std::string.

std::stringstream strm;
strm << "Age: " << nAge << ", DOB: " << szDate << ", Salary: " << nSalary; std::cout << strm.str();[/sourcecode]

Use cout to print out bool values as true and false?

Have you tried printing out bool using cout.

Well who hasn’t, and most of you might have got results like 0/1. For eg:

cout < < "1 > 2: " < <  ( 1 > 2 );

Output
1 > 2: 0
Well how to print out true or false directly instead of 0 and 1. Use boolalpha manipulator…

cout < < "1 > 2: " < <  boolalpha <<  bool( 1 > 2 );

Output
1 > 2: false

To unset boolalpha effect on cout use noboolalpha likewise…

cout << noboolalpha[/sourcecode]

%d bloggers like this: