lfnetwork.com mark read register faq members calendar

Thread: C++ Halp
Thread Tools Display Modes
Post a new thread. Add a reply to this thread. Indicate all threads in this forum as read. Subscribe to this forum. RSS feed: this forum RSS feed: all forums
Old 11-02-2010, 10:58 PM   #1
RpTheHotrod
LFN Staff Emeritus
 
RpTheHotrod's Avatar
 
Join Date: Apr 2002
Posts: 2,798
LFN Staff Member 10 year veteran! Forum Veteran 
C++ Halp

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.






Code:
#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;
}


Current in-game name is #include
RpTheHotrod is offline   you may: quote & reply,
Old 11-03-2010, 08:36 PM   #2
Boba Rhett
I Slay Dragons
 
Boba Rhett's Avatar
 
Status: Administrator
Join Date: Jul 2000
Location: Man-Room in Missouri
Posts: 13,806
Current Game: Scrolling Eldery
LFN Staff Member 10 year veteran! LF Jester Helpful! 
Could you elaborate on what symptoms you are seeing and if they are constant or random?

I didn't really look closely but there may be a bracketing issue in badDay.
Boba Rhett is offline   you may: quote & reply,
Old 11-03-2010, 10:17 PM   #3
RpTheHotrod
LFN Staff Emeritus
 
RpTheHotrod's Avatar
 
Join Date: Apr 2002
Posts: 2,798
LFN Staff Member 10 year veteran! Forum Veteran 
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:

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;
}


Current in-game name is #include
RpTheHotrod is offline   you may: quote & reply,
Post a new thread. Add a reply to this thread. Indicate all threads in this forum as read. Subscribe to this forum. RSS feed: this forum RSS feed: all forums
Go Back   LucasForums > Network > JediKnight Series > Community > Yoda’s Swamp > C++ Halp

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:26 AM.

LFNetwork, LLC ©2002-2011 - All rights reserved.
Powered by vBulletin®
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.