| | |
Search A String for proper format
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 60
Reputation:
Solved Threads: 0
Hello out there!! I am trying to search a string for the proper format of a date and time...... My date will simply be: ##/##/#### and my time will simply be ##:##A.M. or P.M.
I am thinking I can use something like :
Or would this be better:
size_type find_first_not_of(const string &date, LOST HERE DON'T KNOW PROPER SYNTAX)
Obviously greatly confused any Help appreciated as always Thank you
-RICK
I am thinking I can use something like :
C++ Syntax (Toggle Plain Text)
size_type find_first_of(CharType ch, size_type indx = 0) const; //Returns the index of the first occurrence of ch within the invoking string. The search begins at index indx. npos is returned if no match is found. //my date is string the_date and my time is string the_time -- so could I do????>>>>>> size_type find_first_of(CharType ##/##/####, size_type indx = 10) const
C++ Syntax (Toggle Plain Text)
size_type find_first_not_of( const string &str, size_type indx = 0) const; //Returns the index of the first character within the invoking string that does not match any character in str. The search begins at index indx. npos is returned if no mismatch is found.
Obviously greatly confused any Help appreciated as always Thank you
-RICK
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
Input validation can be a challenge, depending how nitpicky you want to be. Since your input format is so specific I'd suggest a grind it out char by char analysis. Maybe something along this lines:
You can get as detailed as you want.
C++ Syntax (Toggle Plain Text)
bool isValidDate(stringType input) { bool result = true; //ensure each placeholder in input is a valid char int numerals[8] = {0, 1, 3, 4, 6, 7, 8, 9) if(input[2] != '/' || input[5] != '/') result = false; for(int i = 0; i < 7; ++i) if(!isdigit(input[numerals[i]])) result = false; //parse input into 3 ints representing day, month, year //now validate that day, month and year are valid integer values for using a calendar. For example 99/99/9999 would not be valid eventhough each char is valid. return result; }
Last edited by Lerner; Mar 7th, 2008 at 7:00 pm.
•
•
Join Date: Mar 2008
Posts: 62
Reputation:
Solved Threads: 7
if(input[2] != '/' || input[5] != '/')
result = false;
checks to make sure there are '/' in the right place, and look at the array that it is being searched through to search indexes for a digit value
int numerals[8] = {0, 1, 3, 4, 6, 7, 8, 9)
XX/XX/XXXX
01 34 6789
notice that it skips the places where the '/' exist because it has already been checked, if they didn't exist there it would return false.
result = false;
checks to make sure there are '/' in the right place, and look at the array that it is being searched through to search indexes for a digit value
int numerals[8] = {0, 1, 3, 4, 6, 7, 8, 9)
XX/XX/XXXX
01 34 6789
notice that it skips the places where the '/' exist because it has already been checked, if they didn't exist there it would return false.
![]() |
Similar Threads
- Auto Email Reminder (ASP.NET)
- GUI buttons Inventory Part 5 (Java)
- urgent (C)
- String operations (C)
Other Threads in the C++ Forum
- Previous Thread: Making TImage Ful Screen
- Next Thread: graph..
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






