Jump to content

Home

C++ Halp


RpTheHotrod

Recommended Posts

Hey guys!

I've been asked to create a program that gets user input in the form of MM-DD-YYYY and then output how many days it is in that year (also considering leap years).

 

I'm given no instruction on how to do so, but to figure it out. I'm not into arrays yet, and I've only learned about functions a few days ago.

 

Somewhere my logic is flawed in this and I can't quite see it at this point. The program "works", as far as I'm aware, but I'm going back and trying to validate the user input.

 

For ease's sake, 2008 is a leap year and 2009 is not.

 

I'm trying to lock out things such as days beyond 31, or 30 and 31 if feb, and 29 if it's not a leap year.

 

Can anyone see why I'm able to still put in invalid data at times? (February is a good starting point). Try 02-31-2009, 02-31-2008, 02-30-2009, 02-30-2008, 02-29-2009, 02-29-2008.

 

 

 

 

 

 

#include <iostream>

using namespace std;

int month, day, year;
int count = 0;
char dash;
bool evenDay = 0;

void badDate();
bool leapYearCheck();
bool initiallyGood();
bool badDay();

int main()
{

cout << "Please enter a date in MM-DD-YYYY format." << endl;
cin >> month;
cin >> dash;
cin >> day;
cin >> dash;
cin >> year;
cout << endl;

switch(month)
{
	case 4:
	case 6:
	case 9:
	case 11:
		evenDay = 1;
		break;
	default:
		evenDay = 0;
		break;
}

// Testing initial date input
while((initiallyGood() == 0) || (badDay() == 1))
{
	badDate();
}

// Testing input
//cout << month << " " << day << " " << year << endl;

month = month - 1;

/* Testing month change
cout << "New Month: " << month << endl;
*/

switch(month)
	{
		case 1:
		//case 01:
			count = count + 31;
			break;
		case 2:
		//case 02:
			count = count + 59;
			break;
		case 3:
		//case 03:
			count = count + 90;
			break;
		case 4:
		//case 04:
			count = count + 120;
			break;
		case 5:
		//case 05:
			count = count + 151;
			break;
		case 6:
		//case 06:
			count = count + 181;
			break;
		case 7:
		//case 07:
			count = count + 212;
			break;
		case 8:
		//case 08:
			count = count + 243;
			break;
		case 9:
		//case 09:
			count = count + 273;
			break;
		case 10:
			count = count + 304;
			break;
		case 11:
			count = count + 334;
			break;

	}

// If a leap year, add a day.
if (leapYearCheck() == 1)
	{
		count = count + 1;
	}

count = count + day;

cout << "The number of days is " << count << endl;

return 0;
}

void badDate()
{
cin.clear();
cin.ignore(100,'\n');
cout << "Invalid date! Please enter a date in MM-DD-YYYY format (ie 02-15-1984)." << endl;
cin >> month;
cin >> dash;
cin >> day;
cin >> dash;
cin >> year;
cout << endl;

switch(month)
{
	case 4:
	case 6:
	case 9:
	case 11:
		evenDay = 1;
		break;
	default:
		evenDay = 0;
		break;
}

}

bool leapYearCheck()
{
if ((year % 4 == 0) && (year % 100 != 0) || (year % 100 == 0) && (year % 400 == 0))
	return 1;
}

bool initiallyGood()
{
if((((month >= 1) && (month <= 12)) && ((day >= 1) && (day <= 31)) && ((year >= 1) && (year <= 9999))))
	return 1;
}

bool badDay()
{
if(((day >= 1) && (day <= 31)) && (((day == 31) && (evenDay == 1)) || ((month == 2) && (day == 30)) || ((month == 2) && (day == 31)) || ((leapYearCheck() == 0) && ((month == 2) && (day == 29)))))
	return 1;
}

Link to comment
Share on other sites

I fixed it. Apparently functions that return 1 must also have a return 0 (it's not understood to be 0).

 

also, I needed to change...

 

if (leapYearCheck() == 1)

 

to...

 

if ((leapYearCheck()) == 1 && (month != 0) && (month != 1))

 

Corrected code:

 

#include <iostream>

//Jared Parks

using namespace std;

int month, day, year;
int count = 0;
char dash;
bool evenDay = 0;

void badDate();
bool leapYearCheck();
bool initiallyGood();
bool badDay();

int main()
{

cout << "Please enter a date in MM-DD-YYYY format." << endl;
cin >> month;
cin >> dash;
cin >> day;
cin >> dash;
cin >> year;
cout << endl;

switch(month)
{
	case 4:
	case 6:
	case 9:
	case 11:
		evenDay = 1;
		break;
	default:
		evenDay = 0;
		break;
}

// Testing initial date input
while((initiallyGood() == 0) || (badDay() == 1))
{
	badDate();
}

// Testing input
//cout << month << " " << day << " " << year << endl;

month = month - 1;

/* Testing month change
cout << "New Month: " << month << endl;
*/

switch(month)
	{
		case 1:
		//case 01:
			count = count + 31;
			break;
		case 2:
		//case 02:
			count = count + 59;
			break;
		case 3:
		//case 03:
			count = count + 90;
			break;
		case 4:
		//case 04:
			count = count + 120;
			break;
		case 5:
		//case 05:
			count = count + 151;
			break;
		case 6:
		//case 06:
			count = count + 181;
			break;
		case 7:
		//case 07:
			count = count + 212;
			break;
		case 8:
		//case 08:
			count = count + 243;
			break;
		case 9:
		//case 09:
			count = count + 273;
			break;
		case 10:
			count = count + 304;
			break;
		case 11:
			count = count + 334;
			break;

	}

// If a leap year, add a day if not in Janruary or February.
if ((leapYearCheck()) == 1 && (month != 0) && (month != 1))
	{
		count++;
	}

count = count + day;

cout << "The number of days is " << count << endl;

return 0;
}

void badDate()
{
cin.clear();
cin.ignore(100,'\n');
cout << "Invalid date! Please enter a date in MM-DD-YYYY format (ie 02-15-1984)." << endl;
cin >> month;
cin >> dash;
cin >> day;
cin >> dash;
cin >> year;
cout << endl;

switch(month)
{
	case 4:
	case 6:
	case 9:
	case 11:
		evenDay = 1;
		break;
	default:
		evenDay = 0;
		break;
}

}

bool leapYearCheck()
{
if ((year % 4 == 0) && (year % 100 != 0) || (year % 100 == 0) && (year % 400 == 0))
	return 1;
else
	return 0;
}

bool initiallyGood()
{
if((((month >= 1) && (month <= 12)) && ((day >= 1) && (day <= 31)) && ((year >= 1) && (year <= 9999))))
	return 1;
else
	return 0;
}

bool badDay()
{
if(((day >= 1) && (day <= 31)) && (((day == 31) && (evenDay == 1)) || ((month == 2) && (day == 30)) || ((month == 2) && (day == 31)) || ((leapYearCheck() == 0) && ((month == 2) && (day == 29)))))
	return 1;
else 
	return 0;
}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...