I am asking the user to input a start time and an end time in this format: 15.45 (military time with a decimal instead of a colon) and I need to convert those times to a float to perform calculations on them. I am having trouble wrapping my mind around converting the 60 minutes of an hour to a decimal value. e.g. – User inputs start of 12.45 and end of 14.15, this should be converted to 12.75 and 14.25 respectively. How would I go about this conversion?
Also, something I believe I am more capable of figuring out, but curious anyway: how would I validate input so as not to allow a time greater than 23.59 and no time with the last two digits greater than 59?
As soon as one of the values is
double, then the arithmetic will bedone in
double, withdoubleresults. So you can convert theminutestodouble, using any of the three C++ syntaxes:static_cast<double>(minutes),double(minutes)or(double)minutes,or just divide by
60.0, rather than the integer60. (I like to beexplicit, so I’d write
double(minutes) / 60.0. And some people preferstatic_cast, even in this case.)With regards to the validation, I’d do it before conversion; once you’ve
added the minutes to the hours, it’s too late anyway.