|
begginer::problems with exception
I'm just new to c++ and there is a bug in my simple program. I make class Date. something strange when i call d1.getCurrentDayName() it should print days name but it prints exception message which i dont know where it comes from. But when i change d1 with oter input this function print the day name.
this is the main program:
//DRIVER
#include "date.h"
#include <stdio.h>
int main()
{ int n;
try{
Date d1(24,6,1983);
Date d2(23,6,1985);
printf("%d-%d-%d\n",d2.getDayOfMonth(),d2.getMonth(), d2.getYear());
printf(" %s\n",d2.getDateAsString());
printf("day=%s\n",d2.getCurrentDayName());
printf("%d-%d-%d\n",d1.getDayOfMonth(),d1.getMonth(), d1.getYear());
printf(" %s\n",d1.getDateAsString());
printf("day=%s\n",d1.getCurrentDayName());
}
catch(date_exception& S)
{
S.DisplayMsg();
}
catch(...)
{
printf("unknown error\n");
}
return 0;
}
//FILE DATE.H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//EXCEPTION
class date_exception{
public:
//ctor
date_exception(int x):err_id(x){
numexcept++;
}
//dtor
~date_exception(){}
//cctor
date_exception(const date_exception& S ):err_id(S.err_id){}
//assignment
date_exception& operator=(const date_exception& S ){}
//service
void DisplayMsg()const{
printf("%s",err_msg[err_id]);
}
static int NumException(){
return numexcept;
}
private:
static int numexcept;
const int err_id;
static char* err_msg[];
};
class Date {
protected:
int year;
int month;
int dayofmonth;
static int arrdate[];
static const char * arrmonth[];
static char * arrday[];
public:
//ctor
Date(int , int , int )throw (date_exception);
Date ();
//cctor
Date(const Date&);
Date& operator=(const Date&);
//GET
int getDayOfMonth();
int getMonth();
int getYear();
const char *getMonthName();
char *getDateAsString();
char *getCurrentDayName();
//SET
void setDayOfMonth(int )throw (date_exception);
void setMonth(int )throw (date_exception);
void setMonthByName(const char *)throw (date_exception);
void setYear(int )throw (date_exception);
long int getNumberOfDaysSinceEpoch();
int isLeapYear();
};
#endif
//FILE DATE.CC
#include "date.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//static member initialization
int
Date:: arrdate[]={0,31,28,31,30,31,30,31,30,31,31,30,31};
const char *
Date::arrmonth[]={"","January","February","March","April",
"Mei","June","July","August","September","October","November","December"};
char *
Date::arrday[]={"sun","mon","tue","wed","thr","fri","sat"};
int
date_exception::numexcept=0;
char*
date_exception::err_msg[]={"date must > 0", "date too big",
"month must > 0", "month must <=12",
"minimum year 1970","month name not defined"
"input not valid in constructor"};
//CTOR
Date::Date(int dd, int mm, int yy)throw (date_exception)
{ if(yy < 1970) throw date_exception(6);
year=yy;
if(mm<1) throw date_exception(6);
if(mm>12) throw date_exception(6);
month=mm;
if(dd <1) throw date_exception(6);
if(isLeapYear()) arrdate[2]=29;
if(dd > arrdate[mm] ) throw date_exception(6);
dayofmonth=dd;
arrdate[2]=28;
}
Date::Date()
{
dayofmonth=1;
month=1;
year=1970;
}
//cctor
Date::Date(const Date& D)
{
dayofmonth=D.dayofmonth;
month=D.month;
year=D.year;
}
//operator assignment
Date& Date::operator=(const Date& D){
printf("operator=\n");
dayofmonth=D.dayofmonth;
month=D.month;
year=D.year;
return *this;
}
//GET
int
Date::getDayOfMonth()
{ return dayofmonth; }
int
Date::getMonth()
{ return month; }
int
Date::getYear()
{ return year; }
const char*
Date::getMonthName()
{ return (arrmonth[month]); }
char*
Date::getDateAsString()
{ char temp[35];
char * StrDate;
snprintf(temp,sizeof(temp),"%d %s %d",dayofmonth,arrmonth[month],year);
StrDate=(char *) malloc(sizeof(char) * strlen(temp));
strcpy(StrDate,temp);
return(StrDate);
}
char *
Date::getCurrentDayName()
{ int idx=((getNumberOfDaysSinceEpoch()) % 7)+3;
printf("jlh Hari= %d\n",getNumberOfDaysSinceEpoch());
if(idx>7) idx-=7;
return(arrday[idx]);
}
//SET
void
Date::setDayOfMonth(int ndayofmonth)throw (date_exception)
{
if(ndayofmonth < 1) throw date_exception(0);
if(isLeapYear() && month==2){
arrdate[2]=29;
}
else
{
if(ndayofmonth > arrdate[month]) throw date_exception(1);
else dayofmonth=ndayofmonth;
}
arrdate[2]=28;
}
void
Date::setMonth(int nmonth)throw (date_exception)
{
if(nmonth > 12) throw date_exception(3);
else{
if (nmonth < 1) throw date_exception(2);
else month=nmonth;
}
}
void
Date::setMonthByName(const char *monthname)throw (date_exception)
{
int i=1;
int found=0;
while(i<=12 && !found){
if (strcasecmp(arrmonth[i],monthname)==0){
month=i;
found=1;
}
else i++;
}
if(i>12) throw date_exception(5);
}
void
Date::setYear(int nyear)throw (date_exception)
{
if (nyear >= 1970) year=nyear;
else throw date_exception(4);
}
long int
Date::getNumberOfDaysSinceEpoch()
{
Date temp(1,1,1970);
long int waktu=0;
int i;
while(temp.year< year){
if(temp.isLeapYear()) {waktu+=366;}
else{ waktu+=365;}
temp.year++;
}
//sisa bulan
if (isLeapYear()) arrdate[2]=29;
for(i=1;i<month;i++ )
{
waktu+=arrdate[i];
}
arrdate[2]=28;
//sisa hari
waktu+=dayofmonth;
return waktu;
}
int
Date::isLeapYear()
{ return(year%4==0 && (year%100 !=0 || ( year%400==0))); }
|