Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,210 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 917 people online right now. Registration is fast and FREE... Join Now!




Yet another day of the week program

 
Reply to this topicStart new topic

Yet another day of the week program

cboyd7539
post 11 Mar, 2008 - 10:47 PM
Post #1


New D.I.C Head

*
Joined: 4 Feb, 2008
Posts: 12


My Contributions


Here is yet another day of the week program. This program will tell you the day of the week for any date from the 1700's to 2300's. I wrote this with what I was comfortable with, and it works. I am planning to revise this with some switch statements, and will probably put some of this into functions eventually, but for now it works.

My questions however:

Right now, you have to input whether or not it is a leap year. I have no clue how to make this automatic.

Also, I am having to input the calendar year in the hundreds, and input the last two digits of the year. I can't figure out how to make this one step, and then show this in this form : mm/dd/yy or yyyy.

anyways here is my code:

CODE
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
    // variables
    int calYear;
    int yy;
    int day;
    int month;
    int adjYear;
    int modYear;
    int modMonth;
    int total;
    string dayOfWeek;
    char leapYear;
    
    //inputs for calendar year
    cout<<"Please enter the calendar year by the hundreds from 1700 to 2300"<<endl;
    cout<<"Examples: 1700,1800,1900,2000,etc... >> ";
    cin>>calYear;
    cout<<endl<<endl;
    
    //if statements for calYear adjustment
    if(calYear==1700)
       adjYear=4;
       else
       if(calYear==1800)
          adjYear=2;
          else
          if(calYear==1900)
             adjYear=0;
             else
             if(calYear==2000)
                adjYear=6;
                else
                if(calYear==2100)
                   adjYear=4;
                   else
                   if(calYear==2200)
                      adjYear=2;
                      else
                      if(calYear==2300)
                         adjYear=0;
  
                        
    
   // input for last two digits of the year                    
   cout<<"Please enter the last two digits of the year."<<endl;
   cout<<"Examples: 59,69,79,89,00, etc... >> ";
   cin>>yy;
   cout<<endl<<endl;
  
   //input for the day
   cout<<"Please enter the day >> ";
   cin>>day;
   cout<<endl<<endl;
  
   //input for month
   cout<<"Please enter the month as a number"<<endl;
   cout<<"Example: Jan=1, Dec=12 >> ";
   cin>>month;
   cout<<endl<<endl;
  
   //leap year question
   cout<<"Is the year you chose a leap year? 'y' or 'n' >> ";
   cin>>leapYear;
   cout<<endl<<endl;
  
   //if statements for month adjustment
   if (month==1)
       modMonth=0;
       else
       if (month==2)          
           modMonth=3;
           else
           if (month==3)
               modMonth=3;
               else
               if (month==4)
                   modMonth=6;
                   else
                   if (month==5)
                      modMonth=1;
                      else
                      if (month==6)
                          modMonth=4;
                          else
                          if (month==7)
                             modMonth=6;
                             else
                             if (month==8)
                                modMonth=2;
                                else
                                if (month==9)
                                   modMonth=5;
                                   else
                                   if (month==10)
                                      modMonth=0;
                                      else
                                      if (month==11)
                                         modMonth=3;
                                         else
                                         if (month==12)
                                            modMonth=5;
  
// math
modYear=(yy+(yy/4))%7;
total=(modYear+modMonth+day+adjYear)%7;

//if statements for day of the week string
   if(total==0)
     dayOfWeek="Sunday";
     else
     if(total==1)
       dayOfWeek="Monday";
       else
       if(total==2)
         dayOfWeek="Tuesday";
         else
         if(total==3)
           dayOfWeek="Wednesday";
           else
           if(total==4)
             dayOfWeek="Thursday";
             else
             if(total==5)
               dayOfWeek="Friday";
               else
               if(total==6)
                 dayOfWeek="Saturday";
                
   //if statement for leap year adjustment              
   if(leapYear=='y')
      ++total;
      
    //output  
   cout<<"The day of the week for "<<month<<"/"<<day<<"/"<<yy<<" is a "<<dayOfWeek<<endl<<endl;
                                                                                              
    system("PAUSE");
    return EXIT_SUCCESS;
}


I use the bloodshed compiler, if you are wondering.

Any suggestions you may have would be appreciated.

Thanks

Tony

This post has been edited by cboyd7539: 11 Mar, 2008 - 11:01 PM
User is offlineProfile CardPM

Go to the top of the page

TheMagnitude
post 11 Mar, 2008 - 11:46 PM
Post #2


D.I.C Head

Group Icon
Joined: 12 Jan, 2008
Posts: 88



Dream Kudos: 125
My Contributions


Instead of writing:
cpp

if(calYear==1700)
adjYear=4;
else
if(calYear==1800)
adjYear=2;
else
if(calYear==1900)
adjYear=0;
else
if(calYear==2000)
adjYear=6;
else
if(calYear==2100)
adjYear=4;
else
if(calYear==2200)
adjYear=2;
else
if(calYear==2300)
adjYear=0;

write:
cpp

switch (calYear)
{
case 1700:
adjYear=4;
break;
case 1800:
adjYear=2;
break;
case 1900:
adjYear=0;
break;
// etc
}

As for your question, or problem, I dont have time atm.
User is offlineProfile CardPM

Go to the top of the page

cboyd7539
post 12 Mar, 2008 - 06:46 AM
Post #3


New D.I.C Head

*
Joined: 4 Feb, 2008
Posts: 12


My Contributions


Hey thanks for the info. That cut down quite a bit of code, and made it a whole lot easier to read.
User is offlineProfile CardPM

Go to the top of the page

jeronimo0d0a
post 12 Mar, 2008 - 07:36 AM
Post #4


D.I.C Head

**
Joined: 3 Mar, 2008
Posts: 141


My Contributions


CODE

   // test leap year and set flag, bLeap is a bool. this will tell you if you have a leap year
   bLeap = false;
   if( ! ( iY % 4 ) ) { bLeap = true; }
   if( ! (iY % 100) ) { bLeap = false; }
   if( ! (iY % 400) ) { bLeap = true; }
    
User is offlineProfile CardPM

Go to the top of the page

jeronimo0d0a
post 12 Mar, 2008 - 07:42 AM
Post #5


D.I.C Head

**
Joined: 3 Mar, 2008
Posts: 141


My Contributions


I also found that functions like spellMonth(month), spellWeekday(day) are used so often
you might as well have your own little library of them. I like being able to
#include "jeronimo.lib"
and not have to write them every time.

CODE

char* spellMonth(int month)
{
    switch(month)
    {
        case 1: return "January";
        case 2: return "February";
        case 3: return "March";
        case 4: return "April";
        case 5: return "May";
        case 6: return "June";
        case 7: return "July";
        case 8: return "August";
        case 9: return "September";
        case 10: return "October";
        case 11: return "Navember";
        case 12: return "December";
        default: return "Invalid";
    }    
}
    

[/quote]
User is offlineProfile CardPM

Go to the top of the page

cboyd7539
post 12 Mar, 2008 - 09:16 PM
Post #6


New D.I.C Head

*
Joined: 4 Feb, 2008
Posts: 12


My Contributions


QUOTE(jeronimo0d0a @ 12 Mar, 2008 - 08:36 AM) *

CODE

   // test leap year and set flag, bLeap is a bool. this will tell you if you have a leap year
   bLeap = false;
   if( ! ( iY % 4 ) ) { bLeap = true; }
   if( ! (iY % 100) ) { bLeap = false; }
   if( ! (iY % 400) ) { bLeap = true; }
    



OK you are going to have to talk to me like I am two with this post. I haven't gotten through this far in my programming class, and I have no clue whatsoever of what I am looking at.

Please beat this into my thick skull... please. biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 08:05PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month