Hello
I'm in a begginer C++ programming class. I don't understand stand my teacher. I'm looking for some help with this program. Its suppose to check if the date is valid, if the date is valid it should output the, if not then it shoud automatically loop back to the begginning of the program. My problem is that even when the date is valid it still outputs a not valid statement and it wont go into the next part of the program that ask does the user do they want to enter another date! I just need some help!

#include <iostream>

using namespace std;


int main()
{
	// Variables
	int month, day, year;	// the date user input
	char answer, NV;

		// greeting
	cout << "This program will find out the day of week of any " << endl
		<< "date (later than January 1, 1860) you entered. \n\n";

	do
	{


			// menu and user input
			cout << "Please enter a date (mm dd yyyy): " << endl;
			cout << "Month (1 - 12): ";
			cin >> month;
			cout << "Day   (1 - 31): ";
			cin >> day;
			cout << "Year (1860 - ): ";
			cin >> year;
			if ( (month==1 || month ==3 || month == 5 || month == 7 || month== 8 || month== 10 || month==12)
				&& (day >= 1 || day<=31)
				&& (year>=1860 ) )
			{
				cout << endl << endl << "You entered "
						<< month << "/" << day << "/" << year << ".\n\n" ;
			}
			if ( (month==4 || month ==6 || month == 9 || month == 11)
					&& (day >= 1 || day<=30)
					&& (year>=1860 ) )
			{
				cout << endl << endl << "You entered "
						<< month << "/" << day << "/" << year << ".\n\n" ;
			}

            if((month==2) && (day>=1 || day<=28 || 29) && (year>=1860) &&
					((year)>0) && !((year)%4) &&
					( ((year)%100) || !((year)%400) ) )
			{
				// Echo user input
				cout << endl << endl << "You entered "
						<< month << "/" << day << "/" << year << ".\n\n" ;
			}
                                                 else;
                {
                cout<< "Date is not valid,enter another date."<< endl;
                }
	}while(NV);       

            // Repeat?
			cout << "Do you want to try another date? (Y/N): ";
			cin >> answer;
    while (answer == 'Y' || answer == 'y');
	// Bye-bye message
	cout << "Thanks for using the program. Bye-bye." << endl << endl;


	return 0;

Recommended Answers

All 6 Replies

variable NAV is used without ever being initialized or changed so it contains just some random value. You need to rethink what you want to do with that variable.

Also, your logic for checking valid day of month will fail to do its job

if ( (month==1 || month ==3 || month == 5 || month == 7 || month== 8 || month== 10 
         || month==12)  && (day >= 1 || day<=31) && (year>=1860 ) )

Look at your test for valid day. I think any number at all will pass that test.

When testing for inclusion in a range, use && (AND).

If you need to test for exclusion from a range, use || (OR) and have your relationships test for above and below the range limits, like if( day < 1 || day > 31 ) //it's invalid

He's checking to see if the month has 31 days, so the || operator is appropriae there. However, IMO a switch statement would be more explicit

switch(month)
{
    case 1: // January
    case 3:  // March
    case 5:  // May
    case 7: // July
    case 8: // August
    case 10:  // October
    case 12:  // december
        // these months have 31 days
        if( day > 0 && day < 32)
            // the day is valid
        break;
    case 2: // February
        // check the day
        break;
    default: // all the reset have 30 days
         // check the day
         break;
}

Note that there is no check for year in the above because the value of year is not related to any given month or day. Just check it once before or after the above switch statement.

Also, your logic for checking valid day of month will fail to do its job

He's checking to see if the month has 31 days, so the || operator is appropriae there.

Must be old age creaping up on me for that mistake. :)

if((month==2) && (day>=1 || day<=28 || 29)

This snippet above might be problematic as well. Why not just make it <=29 and call it a day? Otherwise you will have to test for a leap year.

Actually that snipped doesn't work at all for February not because of the syntax error but because we have to check for leap year in order to determine the maximum number of days in February.

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.