Inputting HH:MM:SS format

C

crystal twix

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!
 
J

James Kanze

On 07/11/09 22:33, Maxim Yegorushkin wrote:

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top