I was doing a Zeller program in C++ but I am stuck in finding some date wich normally should be wrong but nstead I got an answer for that. Here is the code for the Zeller program. Everything work well except some date as February 31 1111, June 31 1111 which normally should be wrong. Any help??
int stringMonthInteger(string strMonth){
int numMonth;
if (strMonth == "January"){
numMonth = 13;
}
else if (strMonth == "February"){
numMonth = 14;
}
else if (strMonth == "March"){
numMonth = 3;
}
else if (strMonth == "April"){
numMonth = 4;
}
else if (strMonth == "May"){
numMonth = 5;
}
else if (strMonth == "June"){
numMonth = 6;
}
else if (strMonth == "July"){
numMonth = 7;
}
else if (strMonth == "August"){
numMonth = 8;
}
else if (strMonth == "September"){
numMonth = 9;
}
else if (strMonth == "October"){
numMonth = 10;
}
else if (strMonth == "November"){
numMonth = 11;
}
else if (strMonth == "December"){
numMonth = 12;
}
else
numMonth = -1;
return numMonth;
}
int main() {
int numMonth;
string strMonth;
int day;
int year = -1;
// The greater of the program
cout << " Welcome to the Zeller Day / Date Calculator! " << endl;
cout << " ************************************************** " << endl;
cout << " Please enter a date, in the following format: " << endl;
cout << " March 17 1976 " << endl;
cin >> strMonth;
cin >> day;
cin >> year;
// Here the program call our main function
numMonth = stringMonthInteger(strMonth);
while (numMonth == -1 || day < 1 || day >31 || year <= 0){
cout << " The date you entered was not in the correct format. Please try again. " << endl;
cin.clear();
cin.ignore(265, '\n');
cout << " Please enter a date, in the following format: " << endl;
cout << " December 7 1941 " << endl;
//day = -1;
//year = -1;
cin >> strMonth;
cin >> day;
cin >> year;
numMonth = stringMonthInteger(strMonth);
}
int dayOFWeek = 0;
if (numMonth == 13 || (numMonth == 14)){
numMonth = numMonth + 12;
year = year - 1;
}
int centuryYear = year % 100;
int century = year / 100;
dayOFWeek = static_cast<int>(day + ((13 * (numMonth + 1)) / 5) + centuryYear + (centuryYear / 4) + (century / 4) + (5 * (century))) % 7;
string dayOfWeekStr;
switch (dayOFWeek)
{
case 0:
dayOfWeekStr = "Saturday";
break;
case 1:
dayOfWeekStr = "Sunday";
break;
case 2:
dayOfWeekStr = "Monday";
break;
case 3:
dayOfWeekStr = "Tuesday";
break;
case 4:
dayOfWeekStr = "Wednesday";
break;
case 5:
dayOfWeekStr = "Thursday";
break;
case 6:
dayOfWeekStr = "Friday";
break;
default:
dayOfWeekStr = "wrong";
break;
}
cout << " The day is " << dayOfWeekStr << "." << endl;
Aucun commentaire:
Enregistrer un commentaire