Thread: trying to display two digits

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    trying to display two digits

    How do i display cout << 1; in two digits '01'

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One option is to use std::setw and std::setfill from <iomanip>, e.g.,
    Code:
    cout << setw(2) << setfill('0') << 1;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Code:
     #include <iomanip>
    std::cout << std::setw(2) << std::setfill('0') << 1;
    QuantumPete
    "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

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by QuantumPete View Post
    Code:
     #include <iomanip>
    std::cout << std::setw(2) << std::setfill('0') << 1;
    QuantumPete
    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();

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    for some reason it puts the zero after the digit 1 becomes 10 ..
    Perhaps you set the justification to left in some earlier portion of the code, so just set it to right, e.g.,
    Code:
    cout << setw(2) << setfill('0') << right << 1;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by laserlight View Post
    Perhaps you set the justification to left in some earlier portion of the code, so just set it to right, e.g.,
    Code:
    cout << setw(2) << setfill('0') << right << 1;
    I thought as so, but i need it left..

    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();
    Also, why is this 0 inserted to all the following couts following it, how do i enforce it to only one variable/value ?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I thought as so, but i need it left..
    For the printing of this date, set the justification to right, then change it back to left afterwards.

    Also, why is this 0 inserted to all the following couts following it, how do i enforce it to only one variable/value ?
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by laserlight View Post
    For the printing of this date, set the justification to right, then change it back to left afterwards.


    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.
    Just checking
    Code:
    cout << "[" << right << setw(2) << setfill('0') <<  left <<  iter->getIssueDate().getDay() ;
    cout << setfill(' ');
    No! this doesn't work .. Still puts zero's after
    Last edited by csonx_p; 09-01-2008 at 12:05 PM.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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("&#37;02d/%02d/%d  %02d:%02d") % 1 % 8 % 1946 % 14 % 8 << '\n';
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by anon View Post
    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';
    Cool, looks more a c style , printf() .. thanx

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No! this doesn't work .. Still puts zero's after
    That's because you set the justification back to left before you actually print the number.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  4. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM