Hello all amatuer user of C++ please help me recode this

the question is this:
The function String_To_MDY() assumes the user correctly enters the date in the form mm/dd/yyyy. Recode this function to include the following test. Test the string ensure the user enters two digits, followed by a slash, followed by two more digits and a slash, followed by four digits. If not an error message appears and exits the program

#include <iostream>
 #include <iomanip>
 #include <cstdlib>
 #include <cctype>
 
using namespace std;
 
struct DATE_STRUCT
 {
 int month,
 day,
 year;
 };
 
void String_To_MDY(char*, DATE_STRUCT&);
 bool Validate_Date(DATE_STRUCT&);
 char* Strchcpy(char*, char*, int);
 
int main()
 {
 char date_string [11];
 DATE_STRUCT mdy_date;
 char response[2];
 cout << endl << endl;
 cout << "Do you want to convert and validate a date?(Y/N): ";
 cin.getline(response, 2);
 
while (toupper(*response) == 'Y')
 {
 cout << endl;
 cout << "Enter the date in mm/dd/yyyy form: ";
 cin.getline(date_string, 11);
 
String_To_MDY (date_string, mdy_date);
 
cout << endl;
 cout << "The converted date is the following:" << endl << endl;
 cout << "Month:" << setw(4) << mdy_date.month << endl;
 cout << "Day: " << setw(4) << mdy_date.day << endl;
 cout << "Year: " << setw(4) << mdy_date.year << endl;
 
if ( Validate_Date(mdy_date) )
 {
 cout << endl;
 cout << "The date is valid.";
 }
 else
 {
 cout << endl;
 cout << "The date is invalid.";
 }
 
cout << endl << endl;
 cout << "Do you want to convert and validate a date?(Y/N): ";
 cin.getline(response, 2);
 
}
 cout << endl;
 return 0;
 } // End of main()
 
void String_To_MDY(char* date_ptr, DATE_STRUCT& date_struct)
 {
 char month[3],
 day[3],
 year[5];
 char* ch_ptr;
 
ch_ptr = date_ptr;
 
ch_ptr = Strchcpy(month, ch_ptr, '/');
 ++ch_ptr;
 ch_ptr = Strchcpy(day, ch_ptr, '/');
 ++ch_ptr;
 Strchcpy(year, ch_ptr, '\0');
 date_struct.month = atoi(month);
 date_struct.day = atoi(day);
 date_struct.year = atoi(year);
 return;
 } // End of String_To_MDY()
 
char* Strchcpy(char* target, char* source, int ch)
 {
 while (*source != ch && *source != '\0')
 {
 *target = *source;
 ++target;
 ++source;
 }
 
*target = '\0';
 
return source;
 
} // End of Strchcpy()
 
bool Validate_Date(DATE_STRUCT& date_struct)
 {
 int ny_days [13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 int ly_days [13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
if (date_struct.month < 1 || date_struct.month > 12)
 return false;
 
if (date_struct.day < 1)
 return false;
 if (date_struct.year % 4 == 0)
 {
 if (date_struct.day > ly_days[date_struct.month])
 return false;
 }
 else
 if (date_struct.day > ny_days[date_struct.month])
 return false;
 
if (date_struct.year < 1980)
 return false;
 
return true;
 
} // End of Validate_Date()

Is that code what you were given with this assignment, or does it represent actual work you've done? It's not clear what you want help with--we're not going to do the work for you, but if you've made an effort and have something specific you're stuck on, we're happy to help with that.

Be more specific, ask what part you dont understand. No one will do the job for you.

I understand and am not looking for a hand out. The code written above was for last weeks assignment. I am being asked to recode it to check for the correct input and issue a statement if it is not in that format.

I am stuck on how to attack this problem without having to re-write the entire project.
Could I simply create a new function? if so how do I code it with the use of all the pointers?
Could I use an if statement somewhere?
Just interested in best way to tackle and resolve with the least amount of work and headache

Adding a function should be sufficient. Since you already have code that can step through a date-string and divide it into pieces, it should be easy for you to write a function that steps through and verifies that the pieces are the correct length and contain valid characters.

Or you could combine the two into:

  • check whether the first two characters are numeric digits
  • if so, convert to integer
  • check whether the resulting integer is between 1 and 12 inclusive
  • check whether the next character is a slash
  • ...

and if everything succeeds, copy the integers into your date-struct object.

Adding a function should be sufficient. Since you already have code that can step through a date-string and divide it into pieces, it should be easy for you to write a function that steps through and verifies that the pieces are the correct length and contain valid characters.

Yes, this is the best solution. Have it return a TRUE or FALSE to show whether the string was good or bad.

Or you could combine the two into:

  • check whether the first two characters are numeric digits
  • if so, convert to integer
  • check whether the resulting integer is between 1 and 12 inclusive
  • check whether the next character is a slash
  • ...

and if everything succeeds, copy the integers into your date-struct object.

Bad suggestion. Get in the habit of writing modular code. One function does one thing. Another function does one more thing. And so on.

Use many simple solutions to ultimately solve the complex problem.

When you start mixing your code it can become very hard to debug. Also, if your last character is bad, look at all the work you did just to throw out the string.

WaltP,

In general I agree with you; however, converting a couple of substrings to integers is not a lot of "wasted work" if you determine that the last character is invalid, and is it any less wasteful to iterate over the same string in two separate functions, once to verify the character-by-character validity and again to extract the values? Also, checking that a couple of successive characters are digits could well be considered a valid part of the task of converting the substring to an integer. Be wary of turning good advice into a religion. :)

And to you new programmers, listen to Walt, he's really good at what he does!

Be wary of turning good advice into a religion. :)

Not turning anything into a religion. Just using standard good practices from a lot of years of programming.

But if you want a religion, I did walk on water once. I knew where the stones were... :icon_mrgreen:

Oh, and thanks...

To all that responded thank you so much for the adivce, I guess a new function will be the way to go, now to create it lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.