| | |
Input time as hh:mm and no other way
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 17
Reputation:
Solved Threads: 0
So my program is coming along, but now I am writing in the range checking to ensure that the inputs are actually what they should be. My code works fine for time, except for the fact that white space gets ignored. I need to ensure that a user cannot input "10: 09 AM". It must be in the format hh:mm
My code:
Thank you for any suggestions you might have. =]
My code:
C++ Syntax (Toggle Plain Text)
void Time::makeAppt() { Time startTime, endTime; cout << "Start time >>"; cin >> startTime; cout << "End time >> "; cin >> endTime; } istream &operator>>( istream &input, Time &time) { input >> time.hour; input.ignore(); input >> time.minute; input.ignore(); input >> time.period; return input; }
•
•
Join Date: Mar 2009
Posts: 24
Reputation:
Solved Threads: 9
0
#2 Nov 5th, 2009
How about using cin.get() to get one character at a time, verifying that the order is digit, digit [convert to int and check range], colon, digit, digit [convert to int and check range], end-of-line. Maybe ignore leading or trailing white space to be nice. Not sure how you're going to indicate an error -- throw an exception maybe, or just exit() after an error message?
•
•
Join Date: Jan 2008
Posts: 3,842
Reputation:
Solved Threads: 503
0
#4 Nov 5th, 2009
•
•
•
•
How would I use the cin.get() within my overloaded operator? As for the errors, well, I have to build certain messages into it and loop it back around to handle asking for the input again. I'm not worried about that part.
If you read it in as a c-style string, you can use getline and specify 6 characters (5 + 1) maximum. Or use get () five times.
http://www.cplusplus.com/reference/i...tream/getline/
isdigit could come in handy from cctype.
http://www.cplusplus.com/reference/c...ctype/isdigit/
To use get or getline using a c-string, declare a C-string of size 6. If you want to use get, loop through, specifying the character with the loop control variables.
C++ Syntax (Toggle Plain Text)
char c[6]; for (int i = 0; i < 5; i++) { cin.get (c[i]); } c[5] = 0;
Don't have a compiler handy , but I think this should work. One potential problem is if the user doesn't type in at least 5 characters. That's why getline may be superior here.
http://www.cplusplus.com/reference/i...m/istream/get/
[edit]
Replace "cin" with "input" above. I didn't pay attention to the fact that you had an istream, not necessarily cin.
[/edit]
Last edited by VernonDozier; Nov 5th, 2009 at 6:25 pm. Reason: Assumed cin as the stream earlier.
•
•
Join Date: Oct 2009
Posts: 17
Reputation:
Solved Threads: 0
0
#5 Nov 5th, 2009
getline() seems to be doing the trick, but for some reason, it will only process once, not twice.
From my original code, it should run through it once, store it in startTime, then run through it again (when I call the operator once more) and store it in endTime. Instead, it just runs through it for startTime, then leaves, but the data is correctly stored in startTime.
C++ Syntax (Toggle Plain Text)
istream &operator>>( istream &input, Time &time) { char charHour[60], charMinute[60]; input.getline(charHour, 3, ':'); input.getline(charMinute, 3, ' '); input.getline(time.period, 3, ' '); time.hour = atoi(charHour); time.minute = atoi(charMinute); }
From my original code, it should run through it once, store it in startTime, then run through it again (when I call the operator once more) and store it in endTime. Instead, it just runs through it for startTime, then leaves, but the data is correctly stored in startTime.
•
•
Join Date: Oct 2009
Posts: 17
Reputation:
Solved Threads: 0
0
#6 Nov 5th, 2009
So I ended up figuring out how to do it to allow for white space and get the information as needed. Problem is, I cannot seem to store the AM/PM portion.
When I output time.hour and time.minute, they work just fine, but time.period doesn't seem to store anything.
Edit: Take that back. It still has a problem with white space (It's part of the assignment that people can't follow instructions to input a time...) as well.
C++ Syntax (Toggle Plain Text)
istream &operator>>( istream &input, Time &time) { char s[256]; time.period = new char[3]; input.getline(s, 256); time.hour = atoi(strtok(s,":")); time.minute = atoi(strtok(NULL," ")); time.period = strtok(NULL, " "); }
Edit: Take that back. It still has a problem with white space (It's part of the assignment that people can't follow instructions to input a time...) as well.
Last edited by dotnabox; Nov 5th, 2009 at 7:07 pm.
•
•
Join Date: Jan 2008
Posts: 3,842
Reputation:
Solved Threads: 503
0
#7 Nov 6th, 2009
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:
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.
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
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.
C++ Syntax (Toggle Plain Text)
// 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
Last edited by VernonDozier; Nov 6th, 2009 at 1:22 am.
•
•
Join Date: Mar 2009
Posts: 24
Reputation:
Solved Threads: 9
1
#8 Nov 6th, 2009
Sorry for the tangent, but "time" is not a C++ keyword. The fact that it's highlighted is maybe because the syntax highlighter handles multiple languages, and "time" is a keyword in some of them (for instance, in the
bash shell); or maybe because there is a Unix/POSIX/C standard function called time() from time.h . But "time" is not a C or C++ keyword, and the global function time() wouldn't conflict with a member variable called "time." •
•
Join Date: Jan 2008
Posts: 3,842
Reputation:
Solved Threads: 503
0
#9 Nov 6th, 2009
•
•
•
•
Sorry for the tangent, but "time" is not a C++ keyword. The fact that it's highlighted is maybe because the syntax highlighter handles multiple languages, and "time" is a keyword in some of them (for instance, in thebashshell); or maybe because there is a Unix/POSIX/C standard function calledtime()fromtime.h. But "time" is not a C or C++ keyword, and the global functiontime()wouldn't conflict with a member variable called "time."
http://www.cppreference.com/wiki/keywords/start
![]() |
Similar Threads
- Time validation code (C)
- Keep time and wait for input (Assembly)
- How can I use a delimter to take sevral variables from the same input (C++)
- Time Difference in VB 6.0 (Visual Basic 4 / 5 / 6)
- check is time and insert null time (PHP)
- Time Difference solution? (C++)
- query nearest time (Visual Basic 4 / 5 / 6)
- Clearing input (C)
- inputing system("date /t") and system("time /t") into a file (C++)
Other Threads in the C++ Forum
- Previous Thread: overloading assignment operator problem
- Next Thread: Simple Winsock Question?
Views: 379 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






