Hi,
Let me apologize in advance if this is in the wrong section or if this question has already been asked and answered. I've looked around the site, but have probably overlooked the answer if it's already here.
So, for my computer programming class (I'm a beginner), we are to create a program that "determines the day of the week a person was born on based on the birth date he or she enters". I've written the program and showed the teacher. It was perfect until he tested it with the date 12 - 7 - 1941. It printed out the Monday, instead of Sunday. So there is something wrong with the way I did the leapyear.
Here is my code:
Now, my teacher said that I had to fix the way I did the switch statement that is determining when a year is a leap year (leap years had to be taken into consideration). Also, we are only doing dates from 1900 and on. So any year that is entered from before 1900 will not print anything out.Code:#include <iostream> using namespace std; int main() { int birthyear; int year; int day; int month; int leapyear; int total; int birthday; cout << "Enter your year of birth (yyyy): " << endl; cin >> birthyear; cout << "Enter your day of birth (dd): " << endl; cin >> day; cout << "Enter your month of birth (mm): " << endl; cin >> month; year = birthyear - 1900; leapyear = year % 4; total = (year / 4) + year + day; switch (leapyear) { case 0: switch (month){ case 1: total = total + 1; break; case 2: total = total + 4; break; case 3: total = total + 4; break; case 4: total = total + 0; break; case 5: total = total + 2; break; case 6: total = total + 5; break; case 7: total = total + 0; break; case 8: total = total + 3; break; case 9: total = total + 6; break; case 10: total = total + 1; break; case 11: total = total + 4; break; case 12: total = total + 6; break; } default: switch (month){ case 1: case 2: total = total - 1; break; } } birthday = total % 7; switch (birthday){ case 1: cout << "Sunday" << endl; break; case 2: cout << "Monday" << endl; break; case 3: cout << "Tuesday" << endl; break; case 4: cout << "Wednesday" << endl; break; case 5: cout << "Thursday" << endl; break; case 6: cout << "Friday" << endl; break; case 7: cout << "Saturday" << endl; break; } return 0; }
What I am not understanding is how I can fix this. The teacher didn't do a good job explaining what he found wrong, just that I needed to fix it. So I would appreciate if someone could explain what is wrong.
Thanks in advance,
Tiffany