Inputting HH:MM:SS format

Discussion in 'C++' started by crystal twix, Nov 7, 2009.

  1. crystal twix

    crystal twix Guest

    I'm trying to ask the user for a time in the format HH:MM:SS, and I
    was wondering what the best way would be to input those values into a
    struct that looks like:

    struct time {
    int hour;
    int min;
    int sec;
    };

    Thanks!
    crystal twix, Nov 7, 2009
    #1
    1. Advertising

  2. On 07/11/09 22:08, crystal twix wrote:
    > I'm trying to ask the user for a time in the format HH:MM:SS, and I
    > was wondering what the best way would be to input those values into a
    > struct that looks like:
    >
    > struct time {
    > int hour;
    > int min;
    > int sec;
    > };


    For parsing there is strftime() function:
    http://www.cplusplus.com/reference/clibrary/ctime/strftime/

    --
    Max
    Maxim Yegorushkin, Nov 7, 2009
    #2
    1. Advertising

  3. On 07/11/09 22:33, Maxim Yegorushkin wrote:
    > On 07/11/09 22:08, crystal twix wrote:
    >> I'm trying to ask the user for a time in the format HH:MM:SS, and I
    >> was wondering what the best way would be to input those values into a
    >> struct that looks like:
    >>
    >> struct time {
    >> int hour;
    >> int min;
    >> int sec;
    >> };

    >
    > For parsing there is strftime() function:
    > http://www.cplusplus.com/reference/clibrary/ctime/strftime/


    Oops, I meant strptime():
    http://www.opengroup.org/onlinepubs/009695399/functions/strptime.html

    --
    Max
    Maxim Yegorushkin, Nov 7, 2009
    #3
  4. crystal twix

    crystal twix Guest

    On Nov 7, 2:37 pm, Maxim Yegorushkin <>
    wrote:
    > On 07/11/09 22:33, Maxim Yegorushkin wrote:
    >
    > > On 07/11/09 22:08, crystal twix wrote:
    > >> I'm trying to ask the user for a time in the format HH:MM:SS, and I
    > >> was wondering what the best way would be to input those values into a
    > >> struct that looks like:

    >
    > >> struct time {
    > >> int hour;
    > >> int min;
    > >> int sec;
    > >> };

    >
    > > For parsing there is strftime() function:
    > >http://www.cplusplus.com/reference/clibrary/ctime/strftime/

    >
    > Oops, I meant strptime():http://www.opengroup.org/onlinepubs/009695399/functions/strptime.html
    >
    > --
    > Max


    Ok, I read the reference and tried going over the example. Would I do
    something like this?

    struct Time {
    int hours, minutes, seconds;
    };

    const int length = 8;

    int main() {

    char time[length];
    char *time;

    cout << "Enter time:";
    cin >> time;
    Time timeStruct = strptime(time, "%H %M %S", &time);
    }

    Thanks!
    crystal twix, Nov 7, 2009
    #4
  5. crystal twix

    James Kanze Guest

    On Nov 7, 11:37 pm, Maxim Yegorushkin <>
    wrote:
    > On 07/11/09 22:33, Maxim Yegorushkin wrote:


    > > On 07/11/09 22:08, crystal twix wrote:
    > >> I'm trying to ask the user for a time in the format
    > >> HH:MM:SS, and I was wondering what the best way would be to
    > >> input those values into a struct that looks like:


    > >> struct time {
    > >> int hour;
    > >> int min;
    > >> int sec;
    > >> };


    > > For parsing there is strftime() function:
    > >http://www.cplusplus.com/reference/clibrary/ctime/strftime/


    > Oops, I meant
    > strptime():http://www.opengroup.org/onlinepubs/009695399/functions/strptime.html


    Not in C++. That's a Posix extension, so if you're concerned
    with portability, you can't use it.

    There are several solutions to what he wants to do, depending on
    the context and how flexible he wants to be with regards to the
    input (exactly two digits for the hour, or one or two digits,
    etc.). Basically, however, I'd start by defining a
    std::istream& operator>>(std::istream& source, time& dest)
    function, using sgetc on the input to determine if the next
    character was what I wanted. Depending on how flexible I wanted
    to be, I might just read exactly eight characters, validate it
    with a regular expression, then use an istringstream to convert
    the three numbers; or I'd input a number, check for and extract
    the ':', input another number, check for and extract the ':',
    and input the third number.

    --
    James Kanze
    James Kanze, Nov 9, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill

    Inputting NULLS?

    Bill, Dec 11, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    353
    Suresh
    Dec 11, 2003
  2. Aaron

    Error on inputting data

    Aaron, Jan 1, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    2,775
    Peter Blum
    Jan 2, 2006
  3. =?Utf-8?B?amFzb253b25n?=

    Inputting Text to the Runtime Parser

    =?Utf-8?B?amFzb253b25n?=, Jan 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    357
    =?Utf-8?B?amFzb253b25n?=
    Jan 13, 2006
  4. yasin
    Replies:
    1
    Views:
    324
    Gordowey
    Apr 20, 2006
  5. dungping
    Replies:
    27
    Views:
    3,373
    Jukka K. Korpela
    Sep 2, 2005
Loading...

Share This Page