// PROGRAM TO FIND THE NEXT DATE  OF A GIVEN DATE

#include<iostream.h>
#include<conio.h>

class udate
{
	
	int day, month, year;
	public:
		void read()
		{
			cin>>day>>month>>year;
		}
		void write()
		{
			cout<<day<<"/"<<month<<"/"<<year;
		}
	friend int valid(udate);
	void operator++();
};

int valid(update D)
{
	int d = D.day;
	int m = D.month;
	int y = D.year;
	
	if(d <= 0|| d>31 || m <= 0 || m>12 || y<=0)
		return(0);
	else
		if(( m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12) && d>31)
			return(0);
		else
			if(( m==4 || m==6 || m==9 || m==11) && d>30)
				return(0);
			else
				if( m==2 )
				{
					
					if((y%100==0 && y%400==0 && d>29)
						|| (y%100==0 && y%400!=0 && d>28))
						return(0);
					else
						if((y%4==0 && d>29) || (y%4!=0 && d>28))
							return(0);
				}
				
				return(1);
}

void udate :: operator ++()
{
	
	if(month==2)
	{
		if((y%100==0 && year%400==0 && day==29) || (year%100==0 && year%400!=0 && day==28))
		{
			day=1;
			month ++;
		}
		
		else
			if(year%4==0 && day==29)||(year%4!=0 && day==28))
			{
				day=1;
				month++;
			}
			
			else
				day++;
	}
	
	else
		if((month==1 || month==3 || month ==5 || month==7
			|| month==8 || month==10 || month==12) && day==31)
		{
			day=1;
			if(month==12)
			{
				month = 1;
				year++;
			}
			else
				month++;
		}
		
	else
	{
		if((month==4 || month==6 || month==9 || month==11) && day == 30)
		{
			day=1;
			month++;
		}
		else
			day++;
	}
}

void main()
{
	udate d1;
	clrscr();
	
	cout<<" PROGRAM FOR FINDING THE NEXT DATE USING OPERATOR OVERLOADING '++'\n\n";
	
	while(1)
	{
		cout<<"ENTER THE DATE :";
		d1.read();
		
		if(!valid(d1))
			cout<<"INVALID DATE PLEASE ENTER THE CORRECT DATE ..."<<endl;
		else
			break;
	}
	
	cout<<endl<<"\n THE GIVEN DATE IS :";
	d1.write();
	++d1;
	
	cout<<endl<<"\n AFTER INCREMENTING :";
	d1.write();
	getch();
}

Greetings,

There are a few typographical and syntax issues with your program. Firstly, lets look at the minor issues:

  • Point A

    int valid(update D)

    I'm guessing that update should be udate, hence the class.

  • Point B

    In your operator overload function operator++() one of your if statements have a slight issue: if[/color](year%4==0 && day==29)||(year%4!=0 && day==28))As seen, you open a pararenthesis and close it. Open another and then close two. We can simple fix this by adding a parenthesis in the beginning of the if statement: if(year%4==0 && day==29)||(year%4!=0 && day==28)) So far this has worked, and your program compiled just fine after this. Without using the code blocks it was difficult for me to see if your programs if/else statements were properly called, though it seemed fine once I tabbed things over in my compiler. If you have further questions, please feel free to ask.

I hope this helps,

Stack Overflow

thanks for ur reply...
but didn't it go into an infinite loop even after correcting :confused:

Greetings,

How about if we try this:

int main() {
	udate d1;

	cout << "PROGRAM FOR FINDING THE NEXT DATE USING OPERATOR OVERLOADING '++'" << endl << endl;

	while (1) {
		cout << "ENTER THE DATE: ";
		d1.read();

		if (!valid(d1)) {
			cout << "INVALID DATE PLEASE ENTER THE CORRECT DATE ..." << endl;
			continue;
		}

		cout << endl << "THE GIVEN DATE IS: ";
		d1.write();
		++d1;

		cout << endl << "AFTER INCREMENTING: " << endl << endl;
		d1.write();
		getch();
	}

	return 0;
}

What have we changed?
We moved our given date and incrementing lines inside the infinite loop. What we did before was break out of the loop when it was time to write the date. Indeed the program won't infintely loop if we break it to soon.

How to we get the user to ask again?
Simple. We take advantage of the infinite loop. The continue statement is related to break though it causes the next iteration of the enclosing while loop to begin. Exactly what we need! When we don't get the results, we start over. The while loop will always run, so why not start back at the beginning.


Hope this helps,
- Stack Overflow

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.