Without seeing the function calls and the class and the exact assumptions regarding what you can assume as far as data, plus whatever else may be going on with the stream, I'm not 100% sure what the problem is. Note that "time" is highlighted. It's a C++ keyword. I doubt that's the problem, but possibly consider renaming the variable just in case.
I assume this is the criteria, in order:
- Any amount of white space.
- 0-5 (index 0)
- 0-9 (index 1)
- : (index 2)
- 0-5 (index 3)
- 0-9 (index 4)
- Exactly one blank space (index 5)
- "AM", "PM", "am" or "pm" (indexes 6 and 7)
If this is it, you can use getline to get a maximum of 8 characters, then just go character by character. Use no character delimiter.
// read in and throw away all white-space, character by character, till you get to first non-white space. "Put back" into the stream any non-white-space character if you have read it in.
char s[9];
getline (s, 9);
// check length of s using strlen. If it isn't 8, flag as invalid input.
// Go through s[] character by character, testing using the criteria above. If any test fails, flag as invalid input.
isspace and isdigit from cctype could come in handy. strlen and strcmp from cstring could also come in handy. peek, get, getline, ignore, unget, and putback from the istream library could be useful.
http://www.cplusplus.com/reference/iostream/istream/
What are you supposed to do if the person enters bad data and what data do you need to be able to handle? Depending on that, you may need to do a variety of things to the stream, including setting or clearing failure bits, or clearing the stream. I'm not trying to over-complicate things, so if I am, I'm sorry. It's just good to know as much as possible what data might be thrown at you, what you need to be able to handle and recover from, and what you don't. If it's simply a cin stream where you ask people to enter times and press enter, then say "Sorry, try again" if it's bad, that's less complicated than if you have to set failure bits at the exact point of failure and allow the stream to be cleared and used again, stuff like that.
This page, as well as the next several pages in the link, might be useful.
http://www.linuxtopia.org/online_boo...mming_074.html