Here is my code I am working and I cant for life of me get it to print if the year entered is a leap year or not. Please help me fix this.

#include <iostream>

using namespace std;

class dateType
{
  private:

      int dMonth;
      int dDay;
      int dYear;

  public:

      void setDate (int month,int day,int year);
      int getDay() const;
      int getMonth() const;
      int getYear() const;
      void printDate() const;
      bool isLeapYear (int year);
      dateType (int month=0, int day=0, int year=0);
};

void dateType::setDate (int month, int day, int year)
{

  if (year<=2008)
    {
      dYear=year;
      
      if (month<=12)
        {
          dMonth=month;

       switch (month)
         {
           case 1: 
           case 3: 
           case 5: 
           case 7:
           case 8: 
           case 10: 
           case 12:
             if (1 <= day && day <= 31)
                dDay = day;
             else
                dDay = 1;
        break;

           case 4: 
           case 6: 
           case 9: 
           case 11:
              if (1 <= day && day <= 30)
                 dDay = day;
              else
                 dDay = 1;
        break;

           case 2: 
               if (isLeapYear(dYear))
                {
                  if (1 <= day && day <= 29)
                     dDay = day;
                  else
                     dDay = 1;
                }
               else
                {
                  if (1 <= day && day <= 28)
                     dDay = day;
                  else
                     dDay = 1;
                }
	   }
	  }
  }
  }

bool dateType::isLeapYear()const
  {
    if ((dYear % 4 == 0 && dYear % 100 != 0) || (dYear % 400 == 0))
       return true;
    else
       return false;
  }

void dateType::printDate() const
  {
    cout << dMonth << "-" << dDay << "-" << dYear;
  }

int dateType::getMonth() const
  {
    return dMonth;
  }

int dateType::getDay() const
  {
    return dDay;
  }

int dateType::getYear() const
  {
    return dYear;
  }

dateType::dateType (int month, int day, int year)
  {
    dMonth=month;
    dDay=day;
    dYear=year;
  }

void main()
  {
    int m, d, y;
    dateType date (0,0,0);

    cout<<"Enter the Month: ";
    cin>>m;

    cout<<"Enter the Day: ";
    cin>>d;

    cout<<"Enter the Year: ";
    cin>>y;

    date.setDate(m, d, y);

    bool check= date.isLeapYear (y); 

        if (check)
          cout<<"Leap Year: ";
   
    date.printDate();
    system ("pause");
  }

Recommended Answers

All 8 Replies

What do you mean by "not printing"? Does the program compile? If so, what the result it displays when you enter a date? Could you show a sample data you enter and its result?

yes it compiles and this is what I get with entered data

Enter month: 7
Enter day: 8
Enter year: 2011
leap year: 0-0-0Press any key to continue...

However I do have this compile error:
datetype.cpp(82) : error C2511: 'bool dateType::isLeapYear(void) const' : overloaded member function not found in 'dateType'

I wanted the program to state whether the year entered was a leap year or not.

OK... Your setDate() function is not working the way you want... You said if(year<=2008) but you enter a value higher than that, so it does nothing. Also isLeapYear() is declared without "int" passing as argument, but you do that in your main().

Thank you for assistance

Here's code for the leap year program, it works and is perfect:

#include <iostream>
#include <string>

using namespace std;

int main()
{
	int year;

	cout << "Please insert a year ( or 0000 to stop ):" << endl;
	cin >> year;
	
	while ( year != 0000)
	{
		if ( year < 1000)
		{
			cout << "Please type in a number with 4 numeric digits" << endl;
		}
		if ( year%4 == 0 && year%100 != 0 )
		{
			cout << "This is a leap year" << endl;
		}
		else
		if ( year%100 == 0 && year%400 == 0)		
		{
			cout << "This is a leap year" << endl; 
		}
		else
		{
			cout << "This is not a leap year" << endl;
		}		
					
		cout << "Please insert a year ( or 0000 to stop ):" << endl;
		cin >> year;
		if ( year == 0000)
		{
			cout << "Goodbye!" << endl;
		}
	}		
}
commented: Do not post working code, help them modify their code so they can learn to do it themselves. -4

@Cross213
Before you throw in your code, please read what the OP asked for... He/she was asking for bugs in the program which is implemented with "class" not a simple plain function. Your code will break if a user enters a string type instead of a number. Furthermore, your code does not cover the whole date but only year...

PS: And what's the different between an integer value of 0000 and 0?

@Cross213
Before you throw in your code, please read what the OP asked for... He/she was asking for bugs in the program which is implemented with "class" not a simple plain function. Your code will break if a user enters a string type instead of a number. Furthermore, your code does not cover the whole date but only year...

PS: And what's the different between an integer value of 0000 and 0?

Well firstly, the user shouldn't enter a string value, besides, the gist of the code is there and works just fine regardless of that, it can always be tweaked later to make it more user-friendly and also, the OP asked how to make the code state whether it is a leap year or not, and I showed him/her how. Secondly, I use 0000 because the user is asked for a four-digit number, so it is only appropriate for them to enter that as well upon exiting. Finally, the user need not enter a day and month to find out whether it is a leap year, because it makes no difference in end, it's just the year that matters.

Well firstly, the user shouldn't enter a string value, besides, the gist of the code is there and works just fine regardless of that, it can always be tweaked later to make it more user-friendly and also, the OP asked how to make the code state whether it is a leap year or not, and I showed him/her how. Secondly, I use 0000 because the user is asked for a four-digit number, so it is only appropriate for them to enter that as well upon exiting. Finally, the user need not enter a day and month to find out whether it is a leap year, because it makes no difference in end, it's just the year that matters.

Well firstly, a user can enter anything because a user knows nothing about your code. Even though you put a restriction on how to enter, you should also handle exceptions in anyway possible. The OP leap year function part works fine. It is that he/she used it wrong.

Secondly, if a user enter 0, would your program still quit? Simple.

Finally, this code is nothing related to the OP question. He/she knows how to compute leap year and did it correctly. If you really want to show how it works, you should make it looks more like OP code and point out what actually is NOT working.

commented: Exactly! +17
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.