-6

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.

  • http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction – πάντα ῥεῖ May 22 '17 at 02:31
  • Possible duplicate of [Why does std::getline() skip input after a formatted extraction?](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – CaptainTrunky May 22 '17 at 02:37
  • @Captain Yeah something like that. Refrained to hammer, since the question is lacking a [MCVE] in 1st place. – πάντα ῥεῖ May 22 '17 at 02:44
  • You'll have to write that parsing function yourself using more primitive io functions. It doesn't exist in the standard library. – Benjamin Lindley May 22 '17 at 02:44
  • @CaptainTrunky: How on Earth could that be a duplicate? I see no relation whatsoever between that question and this one, other than both being generally concerned with stream input functionality. – Benjamin Lindley May 22 '17 at 02:49

1 Answers1

0

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).

Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035