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 :

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

Or would this be better:

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.

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

9 Years
Discussion Span
Last Post by Joatmon

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:

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

You can get as detailed as you want.

Votes + Comments

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 '/'


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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.