How cin will read hh, mm and ss while the input format is hh:mm:ss If I do
cin>>hh;
cout<<":";
cin>>mm;
then it will come in the next line which I don't want.
How cin will read hh, mm and ss while the input format is hh:mm:ss If I do
cin>>hh;
cout<<":";
cin>>mm;
then it will come in the next line which I don't want.
I'm not at all sure what the cout << ":";
is supposed to have to do with anything here. It seems quite unrelated to reading anything.
To read hh:mm:ss
you'd typically want to use get_time
, something like this:
tm t;
std::cin >> std::get_time(&t, "%H:%M:%S");
So yes, contrary to popular belief there is something in the standard library specifically for this task (though the result ends up in a struct tm
object, so if you want the results in separate variables, you'll need to copy them out of the tm
yourself).