Hey! Again, small but devious problem for me: I have a string that gets time as an input, in a format like "9.45" or "15.37". How do I extract the hours and minutes to integer variables, when the hour part may consist of either one or two digits? If it was just one digit from 0-9 I could just pick the chars up and convert them, but now I can't figure out the solution... :-/ Maybe I haven't had enough coffee today!
bleedi 3 Junior Poster in Training
Recommended Answers
Jump to PostHi
You can convert time string values with stringstream to double values. From double values you can cast hours and minutes:
#include <iostream> #include <string> #include <sstream> using namespace std; int main(){ stringstream conv; double t; int h,m; conv<<"9.45"; conv>>t; h=(int)t; m = (int)(100*(t-h)+0.5); cout<<t<<" = …
All 3 Replies
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
tesuji 135 Master Poster
bleedi 3 Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.