Thread: Urgent!! Running short on time. Need help with formatting time.

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    2

    Question Urgent!! Running short on time. Need help with formatting time.

    Hey All:

    I am new to the C++ board. I need a simple answer to what I am sure is a simple problem.

    Problem:

    -I have spent all evening writing a program to convert 24-hour notation to 12-hour notation. There are two problems I have run into and simply can't find a clear answer in the text.

    1. How can I format the minutes so that when I ask someone to enter say 5 minutes it comes out with a leading zero ala 05.
    This way my cout statement produces a time of say 12:05 instead of 12:5. This is really the only trouble I am having and would complete my program...PLEASE HELP!

    2. I would like to also insert am, pm, noon, midnight for certain times. How can I do this with out several if-else statements...or is that the only way??

    Thansk,

    Jarod.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    There are several solutions. One solution is ostringstream. Another solution is CString's Format().

    ostringstream ss;
    ss << 0;
    ss << nMinute;

    Kuphryn

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    To expand on what Kuphryn said, you can use the setw() stream modifier to ensure what you print out is at least two characters wide. In conjunction with setfill(), you can set what character will pad your output.

    Example:
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main(int argc, char** argv)
    {
        // ...
        cout "Minutes: " << setfill('0') << setw(2) << nMinutes << endl;
        // ...
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    2
    Thanks, I appreciate it. Now I just have to implement it into my program and I should be good to go. Thanks.


    !!Just one problem, I don't know what to do with either code!!

    Well, if anyone gets a chance to respond in a few minutes,
    can you tell me how? Thanks.

    Update:
    Eibro, I figured out how to implement your code example, it works wonderfully...you saved me. My project now works 100%. Thanks for the help guys.
    Last edited by DreadedOne; 06-13-2003 at 07:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mult-dim arrays and pointers to them
    By skybolt_1 in forum C Programming
    Replies: 11
    Last Post: 05-02-2003, 11:47 AM
  2. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  3. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  4. running time
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2002, 09:35 AM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM