初学循环if的问题 闰年2月28/29
程序代码:
// Month Day Year Class using namespace std; #include <string> class CMonthDayYear { public: // Default Constructor for CMonthDayYear CMonthDayYear(){ cout << "\nDefault MonthDayYear's Constructor is called!\n\n"; } // Validate Facilitator void ValidatingTheDate(int Month, int Day, int Year) { string NameOfMonth; bool Check; Check = true; if ( Month < 1 || Month > 12 ) { cout << "\nYour Input is : " << Month << "/" << Day << "/" << Year << "\n" << "INVALID input of month : *** " << Month << " *** OUT OF RANGE" << endl << endl; Check = false; } // End if of Month input check if ( 1 <= Month <= 12 ) { switch(Month) { case 1: NameOfMonth = "January"; break; case 2: NameOfMonth = "February"; break; case 3: NameOfMonth = "March"; break; case 4: NameOfMonth = "April"; break; case 5: NameOfMonth = "May"; break; case 6: NameOfMonth = "June"; break; case 7: NameOfMonth = "July"; break; case 8: NameOfMonth = "August"; break; case 9: NameOfMonth = "September";break; case 10: NameOfMonth = "October"; break; case 11: NameOfMonth = "November"; break; case 12: NameOfMonth = "December"; break; default:; } // End of Switch number to nameofmonth for ( int LeapYear = 1500; LeapYear <= 2500; LeapYear ++ ) { if( (Month == 2) && (LeapYear % 4 == 0) && (Year == LeapYear) && (Day < 1 || Day > 29) ) { cout << "\nYour Input is : " << Month << "/" << Day << "/" << Year << "\n" << "INVALID input in Leap Year month : *** " << NameOfMonth << " ***" << "\nYour input in month *** " << NameOfMonth << " *** is : " << Day << endl << endl; Check = false; /* if ( Year == LeapYear ) { if ( Month == 2 ) { if ( Day < 1 || Day > 29 ) { } // LeapYear's Feb 1-29 } // End if of Day Check on Feb. } // LeapYear is the Year input */ } // End if of Day Check on Leap Year Feb. else if ((Month == 2) && (LeapYear % 4 != 0) && (Day < 1 || Day > 28)) { cout << "\nYour Input is : " << Month << "/" << Day << "/" << Year << "\n" << "INVALID input in month : *** " << NameOfMonth << " ***" << "\nYour input in month *** " << NameOfMonth << " *** is : " << Day << endl << endl; Check = false; } // End if of Day Check on Normal Year Feb. } // End of For/If for checking leap year if ( (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12) && ( Day < 1 || Day > 31 )) { cout << "\nYour Input is : " << Month << "/" << Day << "/" << Year << "\n" << "INVALID input in month : *** " << NameOfMonth << " ***" << "\nYour input in month " << NameOfMonth << " *** is : " << Day << endl << endl; Check = false; } // End if of Day Check on long months if ( (Month == 4 || Month == 6 || Month == 9 || Month == 11) && ( Day < 1 || Day > 30 )) { cout << "\nYour Input is : " << Month << "/" << Day << "/" << Year << "\n" << "INVALID input in month : " << NameOfMonth << "\nYour input in month *** " << NameOfMonth << " *** is : " << Day << endl << endl; Check = false; } // End if of Day Check on short months } // End of month check for loop if (Check) cout << "\nYour Input is : " << Month << "/" << Day << "/" << Year << "\nThe Month is : " << NameOfMonth << "\nThe Day is : " << Day << "\nThe Year is : " << Year << "\n\n"; } private: };那个for不知道怎么用了
把那个闰年二月或正常的二月拿出来都可以用, 放到for里就sb了。 那些判断没有错啊? …… 自我感觉应该是没错, 现在想到的只有放码的位置了, 试了很多不同的放法都不行, 有前辈来指教一下吗? 谢谢!!
PS: 初学C++初到这个网站,刚刚看到给分的问题, 这里问问题一定要分才可以吗?
[ 本帖最后由 nubore 于 2010-9-14 09:39 编辑 ]