#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;
}
}
20 Replies - 394 Views - Last Post: 29 June 2012 - 11:24 PM
#1
error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 12:33 PM
Replies To: error with output when entering century yrs i.e. 2/29/2000
#2
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 12:54 PM
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
#3
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 12:54 PM
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.
#4
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 01:02 PM
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
#5
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 01:06 PM
axnjxn, on 29 June 2012 - 10:02 PM, said:
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.
#6
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 01:09 PM
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
#7
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 01:16 PM
axnjxn, on 29 June 2012 - 01:02 PM, said:
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
#8
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 01:20 PM
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
#9
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 01:29 PM
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
#10
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 01:34 PM
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.
#11
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 01:39 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?
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.
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.
#12
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 01:40 PM
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
#13
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 01:52 PM
#14
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 02:06 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:
same problem with the century yrs
i thank you for your help though
#15
Re: error with output when entering century yrs i.e. 2/29/2000
Posted 29 June 2012 - 02:38 PM
//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
|
|

New Topic/Question
Reply



MultiQuote



|