error with output when entering century yrs i.e. 2/29/2000

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 394 Views - Last Post: 29 June 2012 - 11:24 PM Rate Topic: -----

#1 darylj42  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-June 12

error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 12:33 PM

the error is that my program will not give the right output for century leap years. i've been playing with it for 3 days now and can't determine what my error is i am using a switch and the problem is with case 2





#include <iostream>
#include <iomanip>
#include <string>
#include<conio.h>
using namespace std;

void getDate( int&, int&, int&);  	// get month, day and year from user and store them
// in reference arguments sent by caller  

int ckDate( int, int, int);	// check date based on three int values sent by caller
				// return an int indicating the status of the date 

void displayMsg( int);		// display a message based on the date status

int main()
{
	int month, day, year, errorMessageID;

	month = 0;
	day = 0 ;
	year = 0;
	errorMessageID = 0;

	while (!cin.eof())
	{
	getDate( month, day, year);
		if (!cin.eof())
	{

	errorMessageID = ckDate( month, day, year);	
	displayMsg(errorMessageID);	
		}
	}
	_getch();
	return 0;

}




void getDate( int& m, int& d, int& y)
{
	char slash;
	cout << "enter date";
	cin >> m >> slash >> d >> slash >> y;


	if (cin.eof())
		cout << "Good bye";
	else
		cout << m << "/" << d << "/" << y;
	//displayMsg(ckDate ( m, d,y));
}
int ckDate( int m, int d, int y)
{
	if ((y < 999) || (y > 10000))
	{
		return 1;
	}
	if ((m > 12) || (m < 1))
	{
		return 2;
	}
	switch (m)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if (( d > 31) || (d < 1))
			{
				return 3;
			}
		case 4:
		case 6:
		case 9:
		case 11:
			if (( d > 30) || (d < 1))
			{
				return 4;
			}
		case 2:
			if  (m == 2 && (y%4 == y%100 !=0) || y%400 == 00 && d>29 )
			{
							return 5;
			}
			if (m == 2 && (d>=29 && (y%4 !=0 && y%400 != 0)))
			{
				return 6;
			}
		
	}
	if ((m > 12) || (m < 1))
	{
		return 1;
	}

	
	return 0;
}

void displayMsg( int errorID)
{
	switch (errorID)
	{
		case 0:
			cout<< "Good Date"<<endl;
			break;
		case 1:
			cout<< "Bad Year"<<endl;
			break;
		case 2:
			cout<< "Bad Month"<<endl;
			break;
		case 3:
			cout<< "Bad Day not 1-31"<<endl;
			break;
		case 4:
			cout<< "Bad Day not 1-30"<<endl;
			break;
		case 5:
			cout<< "Bad Day not 1-29"<<endl;
			break;
		case 6:
			cout<< "Bad Day not 1-28"<<endl;
			break;
		default:
			break;
	}

}


Is This A Good Question/Topic? 0
  • +

Replies To: error with output when entering century yrs i.e. 2/29/2000

#2 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 142
  • Joined: 04-February 12

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 12:54 PM

Line 87:
if  (m == 2 && (y%4 == y%100 !=0) || y%400 == 00 && d>29 )

First, change the y%4 == y%100 != 0 because you want y%4 == 0 but NOT y%100 == 0.
Then I would add some parenthesis to ensure proper logic comparisons.
if  (m == 2 && (y%4 == 0 && y%100 != 0) || (y%400 == 00) && d>29 )

This post has been edited by axnjxn: 29 June 2012 - 12:59 PM

Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 12:54 PM

One problem with is that your switch does not have any breaks. So if m is 12 for example and d is 31, it will first check whether d is greater than 31 or smaller than 1. Since that isn't the case it will fall through to the next case and check whether d is greater than 30 or smaller than 1. This is true (since 31 is greater than 30), so it will return 4. Clearly this is not intended.

Then your precedence if statement for the leap year condition is wrong. First of all note that && binds tighter than ||, so the m == 2 condition doesn't apply to anything after the || (though of course that'd be already covered by the switch anyway once you fix the missing breaks).

Then you can't chain operators like you're trying to do with y%4 == y%100 !=0. What that does is check whether the result of y%4 == y%100 is not equal to 0. So basically it's the same as just y % 4 == y % 100.
Was This Post Helpful? 0
  • +
  • -

#4 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 142
  • Joined: 04-February 12

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 01:02 PM

His first switch statement always returns a value. I didn't think you needed to have break if a return occurs. I could be wrong, though.

Also, a leap year requires TRUE for y%4 == 0 and FALSE for y%100 == 0 unless y%400 == 0. So to correct myself from above:

if  (m == 2 && y%4 == 0 && (y%100 != 0 || y%400 == 0) && d>29 )


EDIT: This way, if month is February and year is divisible by 4 but not divisible by 100 unless it is also divisible by 400 and the day is larger than 29, display an error for an invalid leap year date.

This post has been edited by axnjxn: 29 June 2012 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 01:06 PM

View Postaxnjxn, on 29 June 2012 - 10:02 PM, said:

His first switch statement always returns a value. I didn't think you needed to have break if a return occurs. I could be wrong, though.


You don't need a break if a return occurs, but his first case does not always return a value. It returns a value if d is greater than 31 or smaller than 1. Otherwise it falls through to the next case, which is not intended.
Was This Post Helpful? 2
  • +
  • -

#6 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 142
  • Joined: 04-February 12

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 01:09 PM

Good point. I didn't notice that. Paying too much attention to the if statement. I always use break statements as a matter of using switch.

I think his logic is messed up on that test line, though. Should be
if  (m == 2 && y%4 == 0 && (y%100 != 0 || y%400 == 0) && d>29 )

This post has been edited by axnjxn: 29 June 2012 - 01:10 PM

Was This Post Helpful? 0
  • +
  • -

#7 darylj42  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-June 12

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 01:16 PM

View Postaxnjxn, on 29 June 2012 - 01:02 PM, said:

His first switch statement always returns a value. I didn't think you needed to have break if a return occurs. I could be wrong, though.

Also, a leap year requires TRUE for y%4 == 0 and FALSE for y%100 == 0 unless y%400 == 0. So to correct myself from above:

if  (m == 2 && y%4 == 0 && (y%100 != 0 || y%400 == 0) && d>29 )


EDIT: This way, if month is February and year is divisible by 4 but not divisible by 100 unless it is also divisible by 400 and the day is larger than 29, display an error for an invalid leap year date.


i tried your code and no i have the following 15 errors:

Error 1 error C2059: syntax error : 'if' g:\itcs2530\my projects\project4\project4\validatedate.cpp 97 1 project4
Error 2 error C2143: syntax error : missing ';' before '{' g:\itcs2530\my projects\project4\project4\validatedate.cpp 98 1 project4
Error 3 error C2447: '{' : missing function header (old-style formal list?) g:\itcs2530\my projects\project4\project4\validatedate.cpp 98 1 project4
Error 4 error C2059: syntax error : 'return' g:\itcs2530\my projects\project4\project4\validatedate.cpp 103 1 project4
Error 5 error C2059: syntax error : '}' g:\itcs2530\my projects\project4\project4\validatedate.cpp 104 1 project4
Error 6 error C2143: syntax error : missing ';' before '}' g:\itcs2530\my projects\project4\project4\validatedate.cpp 104 1 project4
Error 7 error C2059: syntax error : '}' g:\itcs2530\my projects\project4\project4\validatedate.cpp 104 1 project4
Error 8 error C2143: syntax error : missing ';' before '{' g:\itcs2530\my projects\project4\project4\validatedate.cpp 107 1 project4
Error 9 error C2447: '{' : missing function header (old-style formal list?) g:\itcs2530\my projects\project4\project4\validatedate.cpp 107 1 project4
10 IntelliSense: expected a declaration g:\itcs2530\my projects\project4\project4\validatedate.cpp 97 2 project4
11 IntelliSense: expected a declaration g:\itcs2530\my projects\project4\project4\validatedate.cpp 104 1 project4
12 IntelliSense: expected a declaration g:\itcs2530\my projects\project4\project4\validatedate.cpp 130 4 project4
13 IntelliSense: this declaration has no storage class or type specifier g:\itcs2530\my projects\project4\project4\validatedate.cpp 131 3 project4
14 IntelliSense: expected a ';' g:\itcs2530\my projects\project4\project4\validatedate.cpp 131 10 project4
15 IntelliSense: expected a declaration g:\itcs2530\my projects\project4\project4\validatedate.cpp 133 2 project4
Was This Post Helpful? 0
  • +
  • -

#8 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 142
  • Joined: 04-February 12

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 01:20 PM

That's not happening because of a change in your if logic as long as your open ( and closed ) are matching correctly like I have it. Did you add your break statements to your first switch statement?

Also, make sure you have { and } properly inserted in your code. I also see a possibility for a missing semicolon ;. But, honestly, I think it has to do with the missing break statements that sepp2k mentioned.

I would insert a break statement after the if in your switch statements.

This post has been edited by axnjxn: 29 June 2012 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#9 darylj42  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-June 12

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 01:29 PM

enter date2/33/2012
2/33/2012Good Date
enter date

that should return bad day not 1-29
also this should return bad day not 1-28

enter date 2/33/1100
2/33/1100Bad Day not 1-29
Was This Post Helpful? 0
  • +
  • -

#10 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 142
  • Joined: 04-February 12

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 01:34 PM

I don't know what to say. Take a look at your parenthesis. I mean, just look at the logic of the statement.
 if (m == 2 

If it is February
 if (m == 2 && y%4 == 0

If it is February and the year is divisible by 4
 if (m == 2 && y%4 == 0 && (y%100 != 0 || y%400 == 0) 

If it is February and the year is divisible by 4 and year is NOT divisible by 100 unless divisible by 400
 if (m == 2 && y%4 == 0 && (y%100 != 0 || y%400 == 0) && d>29)

If it is February and the year is divisible by 4 and year is either NOT divisible by 100 or divisible by 400 and the day is greater than 29.
Was This Post Helpful? 0
  • +
  • -

#11 darylj42  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-June 12

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 01:39 PM

[quote name='axnjxn' date='29 June 2012 - 01:20 PM' timestamp='1341001230' post='1654346']
That's not happening because of a change in your if logic as long as your open ( and closed ) are matching correctly like I have it. Did you add your break statements to your first switch statement?

so are you saying that i should add breaks in the following switch

switch (m)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if (( d > 31) || (d < 1))
			{
				return 3;
			}
		case 4:
		case 6:
		case 9:
		case 11:
			if (( d > 30) || (d < 1))
			{
				return 4;
			}
		case 2:
			if  (m == 2 && (y%4 == y%100 !=0) || y%400 == 00 && d>29 )
			{
							return 5;
			}
			if (m == 2 && (d>=29 && (y%4 !=0 && y%400 != 0)))
			{
				return 6;
			}
		
	}
	if ((m > 12) || (m < 1))
	{
		return 1;
	}

	
	return 0;


MOD EDIT:
Please use code tags when posting code.

:code:

This post has been edited by jimblumberg: 29 June 2012 - 02:30 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Was This Post Helpful? 0
  • +
  • -

#12 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 142
  • Joined: 04-February 12

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 01:40 PM

Even 2/29/1100 should be invalid because 1100%400 != 0

Perhaps you should put an else if instead of just another if after it checks the leap year. You can also remove the m == 2 from those if statements since you've already verified m == 2 with the case 2:

EDIT:
int ckDate( int m, int d, int y)
{
	if ((y < 999) || (y > 10000))
	{
		return 1;
	}
	if ((m > 12) || (m < 1))
	{
		return 2;
	}
	switch (m)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if (( d > 31) || (d < 1))
			{
				return 3;
			}
                break;
		case 4:
		case 6:
		case 9:
		case 11:
			if (( d > 30) || (d < 1))
			{
				return 4;
			}
                break;
		case 2:
			if  (y%4 == 0 && (y%100 !=0 || y%400 == 0) && d>29 )
			{
			    return 5;
			}
			else if ((y%4 !=0 || (y%100 == 0 && y%400 != 0))&& d>28)
			{
			    return 6;
			}
                break;
	}
	if ((m > 12) || (m < 1))
	{
		return 2;
	}

	return 0;
}

void displayMsg( int errorID)
{
	switch (errorID)
	{
		case 0:
			cout<< "Good Date"<<endl;
			break;
		case 1:
			cout<< "Bad Year"<<endl;
			break;
		case 2:
			cout<< "Bad Month"<<endl;
			break;
		case 3:
			cout<< "Bad Day not 1-31"<<endl;
			break;
		case 4:
			cout<< "Bad Day not 1-30"<<endl;
			break;
		case 5:
			cout<< "Bad Day not 1-29"<<endl;
			break;
		case 6:
			cout<< "Bad Day not 1-28"<<endl;
			break;
		default:
			break;
	}

}


This post has been edited by axnjxn: 29 June 2012 - 01:50 PM

Was This Post Helpful? 0
  • +
  • -

#13 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 142
  • Joined: 04-February 12

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 01:52 PM

You can also completely remove that last if statement as it is redundant. You already checked for a valid month range before the switch statement.
Was This Post Helpful? 0
  • +
  • -

#14 darylj42  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-June 12

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 02:06 PM

[quote name='axnjxn' date='29 June 2012 - 01:40 PM' timestamp='1341002415' post='1654368']
Even 2/29/1100 should be invalid because 1100%400 != 0

Perhaps you should put an else if instead of just another if after it checks the leap year. You can also remove the m == 2 from those if statements since you've already verified m == 2 with the case 2:

same problem with the century yrs
i thank you for your help though
Was This Post Helpful? 0
  • +
  • -

#15 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 142
  • Joined: 04-February 12

Re: error with output when entering century yrs i.e. 2/29/2000

Posted 29 June 2012 - 02:38 PM

Please be more specific. What do you mean "same problem with the century yrs"? I got this to work just fine. I cleaned it up a bit for you, as well. You don't need to include all those files. You don't need any work with string or iomanip or conio

//test

#include <iostream>

using namespace std;

//get month, day and year from user by utilizing pass-by-reference
void getDate( int&, int&, int&);

//check date based on pass-by-value parameters, return a status int
int ckDate( int, int, int);  

// display a message based on the date status
void displayMsg( int);	   		  

int main()
{
	int month, day, year, errorMessageID, again;

	month = 0;
	day = 0 ;
	year = 0;
	errorMessageID = 0;

	while (true)
	{
		getDate( month, day, year);
		errorMessageID = ckDate( month, day, year);	
		displayMsg(errorMessageID);
		cout << "\nAgain? (1 for yes): ";
		cin >> again;
		cout << endl;
		if (again != 1)
			return 0;
	}

	return 0;

}

void getDate( int& m, int& d, int& y)
{
	char slash;
	cout << "enter date: ";
	cin >> m >> slash >> d >> slash >> y;
	cout << m << "/" << d << "/" << y;
}

int ckDate( int m, int d, int y)
{
	if (y < 999 || y > 10000)
	{
		return 1;
	}
	if ((m > 12) || (m < 1))
	{
		return 2;
	}
	switch (m)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if (( d > 31) || (d < 1))
			{
				return 3;
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			if (( d > 30) || (d < 1))
			{
				return 4;
			}
			break;
		case 2:
			if  (y%4 == 0 && (y%100 !=0 || y%400 == 0) && d>29 )
			{
				return 5;
			}
			else if (y%4 != 0 || (y%100 ==0 && y%400 != 0) && d > 28)
			{
				return 6;
			}
			break;
	}
	
	return 0;
}

void displayMsg( int errorID)
{
	switch (errorID)
	{
		case 0:
			cout<< "\nGood Date"<<endl;
			break;
		case 1:
			cout<< "\nBad Year"<<endl;
			break;
		case 2:
			cout<< "\nBad Month"<<endl;
			break;
		case 3:
			cout<< "\nBad Day not 1-31"<<endl;
			break;
		case 4:
			cout<< "\nBad Day not 1-30"<<endl;
			break;
		case 5:
			cout<< "\nBad Day not 1-29"<<endl;
			break;
		case 6:
			cout<< "\nBad Day not 1-28"<<endl;
			break;
		default:
			break;
	}

}


This post has been edited by axnjxn: 29 June 2012 - 02:43 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2