I am having trouble in the Date::SetDate() function. Basically I don\'t know why
ID: 3638391 • Letter: I
Question
I am having trouble in the Date::SetDate() function. Basically I don't know why at the end my evaluation for leap year is always false at the end of this function. At beginning of this function that's the first thing I test for, and it works as expected; it prints "yes its leap year" when year is true and "no its not a leap year" etc. However, at the end of this function, the very same bool variable that worked well in the beginning becomes FALSE every time. I just cant see where I changed the value of this bool isLeap.class Date
{
private:
int Day, Month, Year;
bool isLeap;
public:
//CONSTRUCTOR
Date();
//ACCESSORS void SetDate(int, int, int); //Checks for validity of input
void DisplayNumerically() const ; //const promises data will not be changed
void DisplayTextually() const;
};
Date::Date() //Inital default values
{
Day = 1;
Month = 1;
Year = 0;
isLeap = false;
}
void Date::SetDate(int Y, int M, int D) //Defind method SetDate
{
Year = Y;
Month = M;
Day = D;
if ( (Year%4 == 0) && (Year%100 !=0 ) ||( Year %400== 0) ) //**Works OK*//
{
isLeap == true;
cout << "yes its leap"<<endl;
cout << isLeap<<endl;
}
else //**Works ok**//
{
isLeap == false;
cout <<" no its not leap"<<endl;
cout<< isLeap<<endl;
}
if (Year <0 )
{
cout <<"1.ERROR:-Invalid Year now terminating..."<<endl;
exit(0);
}
//if( ( Year < 0 ) || ( Year > 2012) && (Month > 2 ) )
//{
// cout << "1.ERROR:- Invalid year now terminating..."<<endl;
// exit(0);
//}
if (Month > 12 || Month < 1)
{
cout << "2.ERROR:- Invalid Month now terminating..."<<endl;
exit(0);
}
if (Day < 1 || Day > 31)
{
cout << "3.ERROR:- INvallid # of Days now terminating..."<<endl;
exit(0);
}
if ( ( Month == 4 || Month == 6 || Month == 9 || Month == 11) && (Day >= 31 ) ) //Months with 31 days and up
{
cout <<"4.Error-: Invalid # of Days with Mo. now terminating..."<<endl;
exit(0);
}
//if ( isLeap == true && Month == 2 && Day > 29 )
//{
// cout << "6.ERROR: Too many days for february (leap) > 29 now terminating..."<<endl;
//exit(0);
//}
if ( Day > 28 && Month == 2 && isLeap == false ) //If Not Leap year and february has more than 28 days
{
cout << "5.ERROR:- Invalid # of Days with Mo. LEAPFALSE now terminating..."<<endl;
exit(0);
}
//else if ( isLeap == true && Month == 2 && Day ==29)
if (isLeap == false) //*****/*/*Dont know why its always false**////
cout << "FALSE"<<endl; ///This ALWays prints--WHY????????///////
}
int main()
{
Date OneDate;
int Y, M, D;
cout <<" ------------------- Enter a year, month and day numerically: "<<endl;
cin >> Y >> M >> D;
OneDate.SetDate(Y,M,D);
cout<<endl<<endl;
OneDate.SetDate(Y,M,D);
return 0;
} I am having trouble in the Date::SetDate() function. Basically I don't know why at the end my evaluation for leap year is always false at the end of this function. At beginning of this function that's the first thing I test for, and it works as expected; it prints "yes its leap year" when year is true and "no its not a leap year" etc. However, at the end of this function, the very same bool variable that worked well in the beginning becomes FALSE every time. I just cant see where I changed the value of this bool isLeap.
class Date
{
private:
int Day, Month, Year;
bool isLeap;
public:
//CONSTRUCTOR
Date();
//ACCESSORS void SetDate(int, int, int); //Checks for validity of input
void DisplayNumerically() const ; //const promises data will not be changed
void DisplayTextually() const;
};
Date::Date() //Inital default values
{
Day = 1;
Month = 1;
Year = 0;
isLeap = false;
}
void Date::SetDate(int Y, int M, int D) //Defind method SetDate
{
Year = Y;
Month = M;
Day = D;
if ( (Year%4 == 0) && (Year%100 !=0 ) ||( Year %400== 0) ) //**Works OK*//
{
isLeap == true;
cout << "yes its leap"<<endl;
cout << isLeap<<endl;
}
else //**Works ok**//
{
isLeap == false;
cout <<" no its not leap"<<endl;
cout<< isLeap<<endl;
}
if (Year <0 )
{
cout <<"1.ERROR:-Invalid Year now terminating..."<<endl;
exit(0);
}
//if( ( Year < 0 ) || ( Year > 2012) && (Month > 2 ) )
//{
// cout << "1.ERROR:- Invalid year now terminating..."<<endl;
// exit(0);
//}
if (Month > 12 || Month < 1)
{
cout << "2.ERROR:- Invalid Month now terminating..."<<endl;
exit(0);
}
if (Day < 1 || Day > 31)
{
cout << "3.ERROR:- INvallid # of Days now terminating..."<<endl;
exit(0);
}
if ( ( Month == 4 || Month == 6 || Month == 9 || Month == 11) && (Day >= 31 ) ) //Months with 31 days and up
{
cout <<"4.Error-: Invalid # of Days with Mo. now terminating..."<<endl;
exit(0);
}
//if ( isLeap == true && Month == 2 && Day > 29 )
//{
// cout << "6.ERROR: Too many days for february (leap) > 29 now terminating..."<<endl;
//exit(0);
//}
if ( Day > 28 && Month == 2 && isLeap == false ) //If Not Leap year and february has more than 28 days
{
cout << "5.ERROR:- Invalid # of Days with Mo. LEAPFALSE now terminating..."<<endl;
exit(0);
}
//else if ( isLeap == true && Month == 2 && Day ==29)
if (isLeap == false) //*****/*/*Dont know why its always false**////
cout << "FALSE"<<endl; ///This ALWays prints--WHY????????///////
}
int main()
{
Date OneDate;
int Y, M, D;
cout <<" ------------------- Enter a year, month and day numerically: "<<endl;
cin >> Y >> M >> D;
OneDate.SetDate(Y,M,D);
cout<<endl<<endl;
OneDate.SetDate(Y,M,D);
return 0;
}
Explanation / Answer
void Date::SetDate(int Y, int M, int D) //Defind method SetDate { Year = Y; Month = M; Day = D; if ( (Year%4 == 0) && (Year%100 !=0 ) ||( Year %400== 0) ) //**Works OK*// { isLeap == true; coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.