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:
> 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; > };
> 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; >> };
> > 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; > >> };
crystal twix writes: > On Nov 7, 2:37 pm, Maxim Yegorushkin <maxim.yegorush...@gmail.com> > 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; >> >> };
No, for at least three reasons. strptime returns a char *, and not your custom Time C++ class, which the standard C library knows nothing about, of course.
Furthermore, the third argument that strptime() expects to get is a struct tm *, and not a char **.
Thirdly, you cannot declare two variables named 'time' in the same scope.
On Nov 7, 11:37 pm, Maxim Yegorushkin <maxim.yegorush...@gmail.com> 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.