// 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();
}

Dani AI

Generated

As already found, the original post contains visible typos and a mismatched parenthesis — those must be fixed first. If the program still appears to “loop forever” afterward, the symptom almost always comes from one of three runtime problems: (1) the input stream is failing so repeated reads return no new values; (2) the validator silently rejects valid input because of logic or inconsistent names (for example mixing a local y and the member year); or (3) the increment branch leaves the date unchanged in a particular case. Fix warnings and then check these items in order.

A common and subtle cause is a failed extraction from cin. When cin >> day >> month >> year fails the stream keeps its failbit set and subsequent extractions do nothing, so the loop keeps iterating with the same values. A robust read clears and discards bad input, for example:

int d,m,y;
while (true) {
  cout << "Enter day month year: ";
  if (!(cin >> d >> m >> y)) {
    cout << "Bad numeric input\n";
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    continue;
  }
  cout << "Read: " << d << "/" << m << "/" << y << '\n';
  break;
}

Simplify date logic in valid() and operator++() to reduce mistakes. Use the standard leap rule and a small month-length table:

bool leap = (year%4==0 && (year%100!=0 || year%400==0));
int mdays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int maxd = (mdays[month] == 28 ? (leap ? 29 : 28) : mdays[month]);

Also ensure valid’s prototype matches its definition (use friend int valid(const udate&)) and keep member names consistent. Consider implementing pre-increment as udate& operator++() so it behaves like a normal ++.

Test with edge cases (28/2 on leap and non-leap years, 31/12, 30/4). Prefer int main() and standard iostreams instead of conio.h. With warnings enabled and the input + table-based approach above, the true cause of the “infinite” loop is usually revealed quickly.

Recommended Answers

All 3 Replies

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.