How do i display cout << 1; in two digits '01'
This is a discussion on trying to display two digits within the C++ Programming forums, part of the General Programming Boards category; How do i display cout << 1; in two digits '01'...
How do i display cout << 1; in two digits '01'
One option is to use std::setw and std::setfill from <iomanip>, e.g.,
Code:cout << setw(2) << setfill('0') << 1;
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
QuantumPeteCode:#include <iomanip> std::cout << std::setw(2) << std::setfill('0') << 1;
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
for some reason it puts the zero after the digit 1 becomes 10 ..
Code:cout << left << setw(2) << setfill('0') << iter->getIssueDate().getDay() << " / " << left << setw(2) << iter->getIssueDate().getMonth() << " / " << left << setw(6) << iter->getIssueDate().getYear(); cout << iter->getIssueDate().getHour() << ":" << iter->getIssueDate().getMinute();
Perhaps you set the justification to left in some earlier portion of the code, so just set it to right, e.g.,for some reason it puts the zero after the digit 1 becomes 10 ..
Code:cout << setw(2) << setfill('0') << right << 1;
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
I thought as so, but i need it left..
Also, why is this 0 inserted to all the following couts following it, how do i enforce it to only one variable/value ?Code:// Date [01/01/2008] cout << left << setw(2) << setfill('0') << iter->getIssueDate().getDay(); cout << " / " << left << setw(2) << iter->getIssueDate().getMonth(); cout << " / " << left << setw(6) << iter->getIssueDate().getYear(); // Clock [12:45] cout << iter->getIssueDate().getHour() << ":" << iter->getIssueDate().getMinute();
For the printing of this date, set the justification to right, then change it back to left afterwards.I thought as so, but i need it left..
You can setfill(' ') after you are done. More generally when you are overloading operator<<, save the format flags before you begin changing them, then restore them after you are done.Also, why is this 0 inserted to all the following couts following it, how do i enforce it to only one variable/value ?
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
You could also just print the year followed by two spaces (instead of the year with width 6), then the time and finally set the alignment and fill character back just once.
But in any case, formatted output is usually simpler with format strings, e.g with boost::format
Code:std::cout << boost::format("%02d/%02d/%d %02d:%02d") % 1 % 8 % 1946 % 14 % 8 << '\n';
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
That's because you set the justification back to left before you actually print the number.No! this doesn't work .. Still puts zero's after
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
Another option, which may make your entire date-printing easier to handle, would be to have a "tostring" function (and a "fromstring"?) in your Date class, that produces a std::string.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.