Search A String for proper format

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

Search A String for proper format

 
0
  #1
Mar 7th, 2008
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 :
  1.  
  2. size_type find_first_of(CharType ch,
  3. size_type indx = 0) const;
  4. //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.
  5.  
  6. //my date is string the_date and my time is string the_time -- so could I do????>>>>>>
  7.  
  8. size_type find_first_of(CharType ##/##/####, size_type indx = 10) const
Or would this be better:
  1. size_type find_first_not_of(
  2. const string &str,
  3. size_type indx = 0) const;
  4. //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.
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Search A String for proper format

 
1
  #2
Mar 7th, 2008
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:
  1. bool isValidDate(stringType input)
  2. {
  3. bool result = true;
  4. //ensure each placeholder in input is a valid char
  5. int numerals[8] = {0, 1, 3, 4, 6, 7, 8, 9)
  6. if(input[2] != '/' || input[5] != '/')
  7. result = false;
  8. for(int i = 0; i < 7; ++i)
  9. if(!isdigit(input[numerals[i]]))
  10. result = false;
  11.  
  12. //parse input into 3 ints representing day, month, year
  13.  
  14. //now validate that day, month and year are valid integer values for using a calendar. For example
  15. 99/99/9999 would not be valid eventhough each char is valid.
  16.  
  17. return result;
  18. }
You can get as detailed as you want.
Last edited by Lerner; Mar 7th, 2008 at 7:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

Re: Search A String for proper format

 
0
  #3
Mar 8th, 2008
Thanks much Lerner! I was also checking out the isDigit(), I thought it would have a place, but couldn't see how to exactly use it along with verifying for '/'
-Rick
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 62
Reputation: Joatmon is an unknown quantity at this point 
Solved Threads: 7
Joatmon Joatmon is offline Offline
Junior Poster in Training

Re: Search A String for proper format

 
0
  #4
Mar 8th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC