Date Validation Function

So I'm writing an assignment for class that needs a date validation function of sorts. The user is supposed to enter a calendar date in the form mm/dd/yy. I have to check to make sure it is in this form, then make the date so it is always 8 characters long(ie 1/1/12 -> 01/01/12). Then it needs to make sure that the month, day, and year is valid.

I've been trying to write it all night, but everything is messed up. I'm going to take a break before my brain fries.

You guys have any ideas?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void dateValidation(string shipDate)
{
     string temp, newDate;
     temp = shipDate;
     
     for(int i = 0; i < temp.length(); i++)
     {
             if(temp[i] == "-" || temp[i] == "\")
             {
                        temp[i] = '/';
             }
     }
     
     i = 0;
     temp = "";
     
     do
     {
         do
         {
             temp += shipDate[i] ;        
         }
         while(temp[i] != '/');
         
          cout << temp.atoi;
          system("pause");
          
     }while(i);
} 
Last edited on
I have several ideas.
What have you got so far so I can help you work off of it?
I edited it above. It throws errors left and right, but you asked for what I have right now. The idea is to grab the numbers until a '/' is detected (all '-' and '\' are turned to '\' in the for loop) and add that to a temp string. That temp string should then be able to turn into an int. From there you put it into the newDate string, and throw a '/' in and repeat all this until the date is complete.
The first thing i noticed was on line 8. In the if statement, there's an error in the second condition. '\' is an escape character, which means it gives the character immediately following a different meaning. a \ followed by a quotation mark nullifies a quotation mark, so it's picking up the rest of your code as a continuous quote. Change line 8 to this:
if(temp[i] == '-' || temp[i] == '\\')
'\\' evaluates to '\' after the program is built.
Also, you need to encapsulate those in single quotes, because temp[i] is a char type, not a string type.

As for the second part, i'm not too sure what you're trying to accomplish there.
Line 14, i hasn't been defined yet. You defined i in the scope of the for loop, but nowhere else, so the implementation doesn't know what i is.
I would scrap everything after the for loop and take a different approach.

Look into the function sscanf: http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/
An example relating to date format:
1
2
3
4
5
6
string date = "11/20/12";
int month = 0, day = 0, year = 0;

sscanf(date.c_str(), "%i/%i/%i", &month, &day, &year);

cout << Month: " << month << "\nDay: " << day << "\nYear: " << year << endl; 

The output of that code would look like:
Month: 11
Day: 20
Year: 12


You could use sscanf to pull month day and year from the string. If any of those values are less than ten, add a zero infront of it when outputting it (to meet your requirements.)

Having the month, day, and year separate, you could also check boundaries. Day can't be less than 1 or greater than 31, month can't be less than 0 or greater than 12.
gamermonk,
first thing is to create a design of what the program is to do - taken from your requirements. Once you have a high level design, then you can add detail to make it into a lower level design,
once you have that, then you can code directly.

Requirements:
So I'm writing an assignment for class that:
#1) needs a date validation function of sorts.
#2) The user is supposed to enter a calendar date in the form mm/dd/yy.
#3) I have to check to make sure it is in this form,
#4) then make the date so it is always 8 characters long
#4A- IF ( 1/1/12 THEN 01/01/12).

#5) Then it needs to make sure that the month, day, and year is valid.

OK

So you now have a MAIN() that will call 5 Functions.


MAIN()
Enter_date() // #2
Check_date() // #3
Fix_date() // #4
Check_month() // #5 Month
Check_day () // #5 Day - - (29) || (30) || (31), chk'd against the month.
Check_year() // #5 Year
Print_Something()
Hang_out() // Either PAUSE the screen or ask for new dates

Return() // eof()

Keep adding detail to the steps above,
like on paper, until each step is mostly finished.

THEN type your program in and try it out.






Last edited on
Topic archived. No new replies allowed.