Thread: Formatting string to time

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Formatting string to time

    Hi! I have the following problem: I need to make a digital timer. I must have always 4 digits in the format MM:SS. I know the functions width() and fill(), but I do not know how work with them to acchieve what I need.
    For example 1 minute and 2 seconds must be shown as: 01:02, and not 1:2, or 1: 2.
    Any help would be appreciated.
    Nothing more to tell about me...
    Happy day =)

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read here, attempt to write it yourself, then post your code here when you're stuck.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    First, thanks for the help! I have created a solution, altough it is slow:
    Code:
          stringstream str;
          str.fill('0');
          str.width(2);
          str << (currentTime-initialTime)/60;
          str << ':';
          str.width(2);
          str << (currentTime-initialTime)%60;
          string str2;
          str >> str2;
          mClock->value(str2.c_str());
    Is there any faster way?
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    How about sticking with cout and avoiding use of stringstreams? Sorry, no compiler handy to test before posting.

    #include<iomanip>

    cout.setiosflags(ios::right);
    cout << setw(2) << setfill('0') <<

    or if you (as some others) prefer, using the format string of printf() to accomplish your needs.

    or even

    if((currentTime-initialTime)/60 < 10)
    cout << '0' << (currentTime-initialTime)/60;
    else
    cout << (currentTime-initialTime)/60;
    Last edited by elad; 05-03-2004 at 08:59 AM.

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    I already tryied printf, but it does not fill with '0'. I need string stream because I do not want to write to the screen. Your option of using if is good, I thought about it, but didn't realize that could get simpler.

    Thanks for the help!
    Nothing more to tell about me...
    Happy day =)

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by gustavosserra
    I already tryied printf, but it does not fill with '0'. I need string stream because I do not want to write to the screen.
    Then you didn't use it correctly, nor dud you use the proper function.
    Code:
          stringstream str;
          str.fill('0');
          str.width(2);
          str << (currentTime-initialTime)/60;
          str << ':';
          str.width(2);
          str << (currentTime-initialTime)%60;
          string str2;
          str >> str2;
    would become
    Code:
    sprintf(str, "%02d:%02d", (currentTime-initialTime)/60, (currentTime-initialTime)%60);
    Writes to the char buffer str, zero filled.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Thank you so much!!!
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  4. How to decrease a time string by a given value
    By Ruchikar in forum C Programming
    Replies: 3
    Last Post: 04-11-2002, 12:08 PM