-
Qns about getting dates
Sample code:
cin.getline(date,11,'\n'); (in the format of dd/mm/yyyy)
Qn:I have to validate the date. ( checking if day exists 1<day<31, eg...
)
however this involves abstracting the date into day month and year.
Any advice on how to do this?
Thanks in advance.
-
Re: Qns about getting dates
"student" <none@email.com> wrote:
>
>Sample code:
>
>cin.getline(date,11,'\n'); (in the format of dd/mm/yyyy)
>
>Qn:I have to validate the date. ( checking if day exists 1<day<31, eg...
>)
>however this involves abstracting the date into day month and year.
>Any advice on how to do this?
>
>Thanks in advance.
>
>you can put the substring dd in an other string
>then you can use function like
int atoi(char*)
try this code
/////////
char date[12];
char D[3];
cin.getline(date,11,'\n');
//get the substring you need you may have better way
for(int i=0;i<2 && date[i]!='\\';i++)
D[i]=date[i];
int day;
///convert the string to int
day= atoi(D);
Good Luck
-
Re: Qns about getting dates
"student" <none@email.com> wrote:
>
>Sample code:
>
>cin.getline(date,11,'\n'); (in the format of dd/mm/yyyy)
>
>Qn:I have to validate the date. ( checking if day exists 1<day<31, eg...
>)
>however this involves abstracting the date into day month and year.
>Any advice on how to do this?
>
>Thanks in advance.
>
>
>
Hi, here's my suggestion:
#include <cstring>
#include <sstream>
#include <ctime>
#include <iostream>
using namespace std;
class InvalidDate
{
public:
InvalidDate() {}
~InvalidDate(){}
const char * GetError()
{
return "Invalid Date Value";
}
};
class Date
{
int _Day;
int _Mon;
int _Year;
bool ParseDateString(const char *szDateToParse)
{
//szDateToParse expected format MM/DD/YYYY
//NOTE: I'm not checking for leap year. Do it yourself!!!
bool bret = false;
if (szDateToParse &&
strlen(szDateToParse) == 10 &&
szDateToParse[2] == '/' &&
szDateToParse[5] == '/')
{
string s = szDateToParse;
_Mon = atoi(s.substr(0,2).c_str());
_Day = atoi(s.substr(3,2).c_str());
_Year = atoi(s.substr(6, 4).c_str());
bret = Validate();
}
return bret;
}
bool Validate()
{
bool bret;
bret = (_Day >= 1 && _Day <= 31) &&
(_Mon >= 1 && _Mon <= 12) &&
CheckDayOfMonth();
if (bret && _Mon == 2 && _Day > 29)
bret = false;
return bret;
}
bool CheckDayOfMonth()
{
bool bret;
const int _DaysPerMonth[] = {0, 31, 29, 31, 30, 31, 30, 31, 30, 30, 31,
30, 31};
bret = _Day <= _DaysPerMonth[_Mon];
return bret;
}
public:
Date()
{
time_t t;
time(&t);
struct tm *pt = localtime(&t);
_Day = pt->tm_mday;
_Mon = pt->tm_mon+1;
_Year = pt->tm_year+1900;
}
Date(int Day, int Mon, int Year) :_Day(Day),_Mon(Mon),_Year(Year)
{
if (!Validate())
{
throw InvalidDate();
}
}
Date (const char *szDate) //expects a string containing Date in MM/DD/YYYY
format
{
if (!ParseDateString(szDate)) throw InvalidDate();
}
~Date() {}
friend ostream& operator << (ostream& os, const Date& date) // for cout
{
os << date._Mon << "/" << date._Day << "/" << date._Year ;
return os;
}
friend istream& operator >> (istream& is, Date& date) // for cin
{
is >> date._Mon >> date._Day >> date._Year ;
if (!date.Validate()) throw InvalidDate();
return is;
}
};
int main()
{
try
{
char s[11];
cout << "Enter a valid date [MM/DD/YYYY]: ";
cin.getline(s, sizeof s);
Date dt = s; //constructs a date object from a string
cout << "Date: " << dt << endl; //executed only if dt is a valid Date object
Date today; //construct a default Date object
cout << "Today is: " << today << endl;
Date dtInput;
cout << "Enter new date [MM DD YYYY]: "; //build a Date object from standard
input
cin >> dtInput;
cout << dtInput << endl;
Date dt2(2,30,2000); //create invalid date object
cout << "Date: " << dt << endl; //not reached, because an exception will
be thrown
}
catch (InvalidDate id)
{
cout << id.GetError() << endl;
}
return 0;
}
I hope this helps.
Luis
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks