Example using the TDate date object
This example uses the TDate object from the service classes part
of the Borland Object Windows Library.
The TDate object is used for its member functions IsValid to verify
a date input and at a later point to print the date in a format
including the day of week.
In addition the date is printed using the AsString() member function
and the HowToPrint static variable is set to force the print format.
| Example using the TDate date object |
#ifndef _DATETEST_CPP_
#define _DATETEST_CPP_
// INCLUDES
//
#include
#include
#include
#include
//============================================
// NAME: _DATETEST_CPP_.CPP -
// TYPE: C++ SOURCE FILE
// SYNOPSIS:
// DESCRIPTION: EXAMPLE OF TDATE
// SYNOPSIS: USES TDATE AS A VALIDATOR AND
// TO PRINT DATE WITH DAY OF WEEK
// BUGS:
// SEE ALSO:
////////////////////////////////////////////
//--------------------------------------------
// Function Prototypes to place in header
void PrintDate ( TDate& );
//--------------------------------------------
|
int main()
{
// types are unsigned int declared in date.h
DayTy outDay;
MonthTy outMonth;
YearTy outYear;
// user inputs date
cout<<"\n\nEnter a date "
<< "\n Day: ";
cin>> outDay;
cout<< "\n Month: ";
cin>> outMonth;
cout<< "\n Year: ";
cin>> outYear;
cin.ignore(); // dispense with final '\n'
// initialise a TDate obj
TDate date1(outDay, outMonth, outYear);
TDate::SetPrintOption(TDate::EuropeanNumbers);
string astring;
astring =date1.AsString();
cout << "\nstring is: " << astring;
// if date is valid output file
if( date1.IsValid() )
{
ofstream fout;
if( !fout) //on file openeing error
{
cerr<< "\nunable to open file for output"
<< "\nPress any key to exit... ";
while( !cin.get() )
;
return -1;
}
// output to file
fout.open("c:/data/owl/datetest.dat");
fout<< outDay <<'\t' // '\t' handy delimiter
<< outMonth <<'\t'
<< outYear <<'\t';
fout.close();
ifstream fin("c:/data/owl/datetest.dat");
if(!fin) // on file open error
{
cerr<< "\nunable to open file for input"
<< "\nPress any key to exit... ";
while( !cin.get() )
;
return -1;
}
// some new variables
DayTy inDay;
MonthTy inMonth;
YearTy inYear;
fin>> inDay;
fin >> inMonth;
fin >> inYear;
fin.close();
// initialise a different TDate obj
TDate date2(inDay, inMonth, inYear);
cout << "\nThe saved and loaded date is: ";
PrintDate(date2);
}
// date not valid
else
{
cerr << "The date input is not valid";
}
// display output for user
cout<< "\nPress any key to exit... ";
while( !cin.get() )
;
return 0;
} // main()
//////////////////////
// Function: void PrintDate ( TDate& )
// Description: prints TDate with pretty
// format including day of wk
// returns: void
// Argument1 ref to TDate to print
/////////
void
PrintDate ( TDate& date ) // prints a date
{
cout << date.NameOfDay() << " ";
cout << date.NameOfMonth() << " ";
cout << date.DayOfMonth() << ", " << date.Year();
}
//===================================================
#endif _DATETEST_CPP_
|
|
|
|
|