Thread: Help with day of the week program

  1. #1
    Registered User
    Join Date
    Jan 2009
    Location
    USA, MD
    Posts
    3

    Unhappy Help with day of the week program

    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:

    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;
    
    }
    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.

    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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I have no idea what your algorithm is, but just as a guess you should probably have twelve cases in your non-leap year, just like you have twelve cases in your leap year?

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could make it extremely easily using <ctime> functions like mktime(), but I'm guessing you're not allowed to use the <ctime> library?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    You could always blow his mind by looking into the utilities in <ctime>. But I'm being facetious. Not to be a grouch, but the code would be easier to read without the double spacing, and with steeper indenting.
    For example:
    Code:
    switch (leapyear) 
        {
            
            case 0:
            {
                 switch (month)
                 {
                  case 1: case 10: { total += 1; }break;
                  case 2: case 11: case 3: { total += 4; }break;
                  case 4: case 7: {/*total+=0*/ }break; //+= 0 does nothing
                  case 5: { total += 2; }break;
                  case 6: { total += 5; }break;
                  case 8: { total += 3; }break;
                  case 9: case 12: { total += 6; }break;
                 }
            }break;
            default:
            {
                 switch (month)
                 {
                  case 1: case 2: { total -= 1; }break;
                 }
            }
        }//leapyear
    A lot is just a matter of taste, but you get the point.
    Last edited by CodeMonkey; 01-14-2009 at 02:55 PM. Reason: augh
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Banned
    Join Date
    Jan 2009
    Posts
    30
    Or to make CodeMonkey's even nicer looking...

    Code:
    switch (leapyear) 
        {
            case 0:
            {
                 switch (month)
                 {
                  case 9: case 12: ++total;
                  case 6: ++total;
                  case 2: case 11: case 3: ++total;
                  case 8: ++total;
                  case 5: ++total;
                  case 1: ++total;
                  case 4: case 7: break;
                 }
            }break;
            default:
            {
                 switch (month)
                 {
                  case 1: case 2: { total -= 1; }break;
                 }
            }
        }//leapyear

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    clever. I guess you could go for -- at the bottom too.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    USA, MD
    Posts
    3
    To be honest, I don't even know what <ctime> functions are . . . ^_^
    We were just given this problem right after learning about switch statements, which is what we're suppose to use in this particular program. And the spacing does look better, but the way that I have mine spaced is the way that was taught to the class.

    So would tabstop's suggestion be a solution? Having 12 cases in the non leap year?

    Thanks for the replies so far.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Punkakitty View Post
    To be honest, I don't even know what <ctime> functions are . . . ^_^
    We were just given this problem right after learning about switch statements, which is what we're suppose to use in this particular program. And the spacing does look better, but the way that I have mine spaced is the way that was taught to the class.

    So would tabstop's suggestion be a solution? Having 12 cases in the non leap year?

    Thanks for the replies so far.
    Looking at it again, I'm believing that the last two cases are just supposed to be an adjustment -- which means that the first case should not have a break so that we "fall through" to the default -- except then you have it backwards, since case 0 is the leap year case, and you want to do these settings in all cases and the adjustment only in the leap years.

    In other words, you need to look at what it is you're supposed to be computing.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by sphynxter View Post
    Or to make CodeMonkey's even nicer looking...
    That's clever until some maintenance guy comes in and moves one of those cases. Since most switch statements don't use fall-through, it's easy to assume that the order of cases doesn't matter and then you've broken something and it's extremely hard to figure out how.

    I'd just use a lookup table, honestly.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Jan 2009
    Location
    USA, MD
    Posts
    3
    My friend looked over my code, and said that it should be written like this:

    Code:
    switch (leapyear) {
    
     case 0:
    
      switch (month){
    
       case 1:
    
       total = total - 1;
    
       break;
    
     case 2:
    
       total = total - 1;
    
       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;
    
    }
    
    }
    
    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;
    I can't test this until I get to school tomorrow, but is this going in the right direction?
    Last edited by Punkakitty; 01-14-2009 at 05:27 PM.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Does it need to be double-spaced like that? I thought only English teachers wanted things handed in double-spaced.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck on a program
    By Northstar in forum C Programming
    Replies: 4
    Last Post: 10-12-2007, 03:02 AM
  2. Missing something in dayofweek program
    By s_ny33 in forum C Programming
    Replies: 2
    Last Post: 11-15-2005, 10:34 AM
  3. day of week
    By s_ny33 in forum C Programming
    Replies: 18
    Last Post: 11-02-2005, 11:56 AM
  4. Simple Calendar question for find day of the week.
    By Extro in forum C Programming
    Replies: 3
    Last Post: 02-25-2003, 09:26 AM
  5. Calendar Problem
    By wordup in forum C Programming
    Replies: 7
    Last Post: 10-29-2002, 03:36 PM

Tags for this Thread