9 Replies - 893 Views - Last Post: 24 January 2011 - 06:41 PM Rate Topic: -----

#1 Cplusishard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-January 11

How to start the next month with a new day! Calendar Program

Posted 23 January 2011 - 10:52 AM

This is my program and i want to know how to start the next month with the day it's supposed to be on.



#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int year;
int firstday;
int month;
void PrintHeading ();
void DoAMonth(int,int,int&);
int numdays;
int janday;


int main(int argc, char *argv[])
{
    cout << "Calendar Program. " << endl;
    cout << "What year do you want a calendar for? " << endl;
    cin >> year;
    cout << "What day of the week does Jan.1 fall on? " << endl;
    cout << "Enter 1 for Sunday, 2 for Monday, etc. " << endl;
    cin >> firstday;
    cout << endl << endl;
    
    while (month >= 0 && month <= 12)
    {
          PrintHeading ();
          DoAMonth (year, month, janday);
          month++;
          }
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
  void DoAMonth(int year, int month, int& day)
    {
  
    if(month == 1 ||month ==  3 ||month == 5 ||month== 7 ||month ==  8 ||month== 10 ||month == 12)
    {
             numdays = 31;
            
            }
            
    else if(month == 4 ||month == 6 ||month == 9 ||month == 11)
    {
         numdays = 30;
         
         }
    else if(month == 2)
    {
         numdays = 28;
        if(year%4 == 0)
        numdays = 29;
      
         }
    cout << endl;
for(int day = 0; day < janday; day++)
{
       cout << " ";
        }
        
for(int day = 1; day <= numdays; day++)
{
       cout << setw(3) <<day;
    if((day + janday)% 7 == 0)
      {
              cout << endl;
                }
                
                }

}
    void PrintHeading ()
    {
    if(month == 1)
     {
     cout << setw(9)<< year << endl;
     cout << endl;
     cout << setw(14)<<"January" << endl;
     cout << " S  M  T  W  Th  F  S" << endl;
     cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  
}
    else if(month == 2)
    {
    cout << setw(15)<<"February" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  
}
    else if(month == 3)
    {
    cout << setw(13)<<"March" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    
}
    else if(month == 4)
    {
    cout << setw(13)<<"April" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    
}
    else if(month == 5)
    {
    cout << setw(12)<<"May" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
   
}
    else if(month == 6)
    {
    cout << setw(13)<<"June" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     
}
    else if(month == 7)
    {
    cout << setw(13)<<"July" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
    else if(month == 8)
    {
    cout << setw(14)<<"August" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     
}
    else if(month == 9)
    {
    cout << setw(16)<< "September" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     
}
    else if(month == 10)
    {
    cout << setw(14)<< "October" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
 
}
    else if(month == 11)
    {
    cout << setw(15)<< "November" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     
}
    else if(month == 12)
    {
    cout << setw(15)<< "December" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to start the next month with a new day! Calendar Program

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1624
  • View blog
  • Posts: 3,082
  • Joined: 30-May 10

Re: How to start the next month with a new day! Calendar Program

Posted 23 January 2011 - 10:58 AM

Make DoAMonth() return the last day printed, so you know where to start for the next month.
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,225
  • Joined: 31-December 10

Re: How to start the next month with a new day! Calendar Program

Posted 23 January 2011 - 11:11 AM

The logic you put in for a leapyear is wrong, if the (year % 4 == 0 AND the year % 100 != 0) OR (year % 400 == 0) then it's also a leapyear. It might be helpful to make that a function that returns a bool. Remember, each function should have one specific task to do. If you break your program into specific tasks and make functions for those tasks, it means that your code is easier to read and understand and also easier to maintain. In the beginning it's hard to think like this, but try to remember that you might have to come back to this code in a year or so and you might not even be able to understand what you were thinking when you wrote it. Now think about how hard it would be for someone else to read and understand it.
Was This Post Helpful? 0
  • +
  • -

#4 Cplusishard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-January 11

Re: How to start the next month with a new day! Calendar Program

Posted 23 January 2011 - 08:40 PM

I've been working on this program for a week and I can't figure out how to start the next month with the day it's suppose to start on other than January. That is the main problem, but I aslo can't figure out how to start the first day on a tuesday,or a wednesday, etc.. If i get that my program will work perfectly :)

My code
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int year;
int firstday;
int month;
void PrintHeading ();
void DoAMonth(int,int,int&);
int numdays;
int janday;


int main(int argc, char *argv[])
{
    cout << "Calendar Program. " << endl;
    cout << "What year do you want a calendar for? " << endl;
    cin >> year;
    cout << "What day of the week does Jan.1 fall on? " << endl;
    cout << "Enter 1 for Sunday, 2 for Monday, etc. " << endl;
    cin >> firstday;
    cout << endl << endl;
    
    while (month >= 0 && month <= 12)
    {
          PrintHeading ();
          DoAMonth (year, month, janday);
          month++;
          }
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
  void DoAMonth(int year, int month, int& day)
    {
   
    if(month == 1 ||month ==  3 ||month == 5 ||month== 7 ||month ==  8 ||month== 10 ||month == 12)
    {
             numdays = 31;
            
            }
            
    else if(month == 4 ||month == 6 ||month == 9 ||month == 11)
    {
         numdays = 30;
         
         }
    else if(month == 2)
    {
         numdays = 28;
        if(year%4 == 0)
        numdays = 29;
         }
         cout << endl;
      
for(int day = 0; day < janday; day++)
{
       cout << " ";
        }
        
for(int day = 1; day <= numdays; day++)
{
       cout << setw(3) <<day;
    if((day + janday)% 7 == 0)
      {
              cout << endl;
                }
                
    
                }
return;
}
    void PrintHeading ()
    {
    if(month == 1)
     {
     cout << setw(9)<< year << endl;
     cout << endl;
     cout << setw(14)<<"January" << endl;
     cout << " S  M  T  W  Th  F  S" << endl;
     cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  
}
    else if(month == 2)
    {
    cout << endl;
    cout << setw(15)<<"February" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  
}
    else if(month == 3)
    {
    cout << endl;
    cout << setw(13)<<"March" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    
}
    else if(month == 4)
    {
    cout << endl;
    cout << setw(13)<<"April" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    
}
    else if(month == 5)
    {
    cout << endl;
    cout << setw(12)<<"May" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
   
}
    else if(month == 6)
    {
    cout << endl;
    cout << setw(13)<<"June" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     
}
    else if(month == 7)
    {
    cout << endl;
    cout << setw(13)<<"July" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
    else if(month == 8)
    {
    cout << endl;
    cout << setw(14)<<"August" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     
}
    else if(month == 9)
    {
    cout << endl;
    cout << setw(16)<< "September" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     
}
    else if(month == 10)
    {
    cout << endl;
    cout << setw(14)<< "October" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
 
}
    else if(month == 11)
    {
    cout << endl;
    cout << setw(15)<< "November" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     
}
    else if(month == 12)
    {
    cout << endl;
    cout << setw(15)<< "December" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    
}
}

Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1307
  • View blog
  • Posts: 4,499
  • Joined: 19-February 09

Re: How to start the next month with a new day! Calendar Program

Posted 23 January 2011 - 09:12 PM

Year 1905 for example,
Jan 1st = Sun = 1
Feb 1st = Wed = 4
Mar 1st = Wed = 4
etc.

if year 1906
You are told Jan 1st = Mon (2), (difference Mon - Sun = 2 - 1 = 1)
Feb 1st = Thu (added 1 day)
Mar 1st = Thu (added 1 day)

If year is leap year you will need to add 1 for that (Mar and so on).

Wondering if this will work for a difference of more than one leap year.

This post has been edited by #define: 23 January 2011 - 09:14 PM

Was This Post Helpful? 0
  • +
  • -

#6 OrganizedChaos  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 153
  • Joined: 29-November 08

Re: How to start the next month with a new day! Calendar Program

Posted 23 January 2011 - 09:14 PM

Hello! I noticed you are not calculating leap years correctly.
Take a look at this to see how they are calculated: http://visualbasic.a...ykleapyr1-1.gif
There are two more checks you need to run to see if it is, in fact, a leap year.

This may be a solution to your starting day problem:
1. Keep track of the number of days skipped at the beginning of January (i.e. If January 1st is a Tuesday, you skipped 2 days: Sunday and Monday)
2. Mod the number of days in the current month by 7, then add the number of days skipped from the previous month to this to get the offset for the next month. (31 (for January) % 7 = 3; 3 + 2 = 5; You need to skip 5 days at the beginning of Feb, so Feb 1st lands on a Friday. If you end up with a total greater than 7, simply subtract 7)
3. Update the daysSkipped variable with the number of days skipped at the beginning of Feb and repeat for March, April, etc.

I know this sounds confusing, but grab a calendar and think through it.

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#7 Cplusishard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-January 11

Re: How to start the next month with a new day! Calendar Program

Posted 23 January 2011 - 10:10 PM

How do I skip 2 days on your first step

This post has been edited by JackOfAllTrades: 24 January 2011 - 05:19 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: How to start the next month with a new day! Calendar Program

Posted 24 January 2011 - 03:45 AM

Duplicate topics merged. Please don't double post.
Was This Post Helpful? 0
  • +
  • -

#9 Cplusishard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-January 11

Re: How to start the next month with a new day! Calendar Program

Posted 24 January 2011 - 06:29 PM

My calendar program is almost done, but there is only 1 problem, the days are not aligned right.
Ex.S M T W Th F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14...
The days other than the first week are perfectly fine
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int year;
int firstday;
int month;
void PrintHeading ();
void DoAMonth(int,int,int&);
int numdays;
int janday;


int main(int argc, char *argv[])
{
    cout << "Calendar Program. " << endl;
    cout << "What year do you want a calendar for? " << endl;
    cin >> year;
    cout << "What day of the week does Jan.1 fall on? " << endl;
    cout << "Enter 1 for Sunday, 2 for Monday, etc. " << endl;
    cin >> firstday;
    cout << endl << endl;
    
    while (month >= 0 && month <= 12)
    {
          PrintHeading ();
          DoAMonth (year, month, janday);
          month++;
          
          }
          
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

  void DoAMonth(int year, int month, int& day)
    {
   
    if(month == 1 ||month ==  3 ||month == 5 ||month== 7 ||month ==  8 ||month== 10 ||month == 12)
    {
             numdays = 31;
            
            }
            
    else if(month == 4 ||month == 6 ||month == 9 ||month == 11)
    {
         numdays = 30;
         
         }
    else if(month == 2)
    {
         numdays = 28;
        if(year%4 == 0)
        numdays = 29;
         }
         cout << endl;
      
for(int day = 0; day < janday; day++)
{
       cout << " ";
        }
        
for(int day = 1; day <= numdays; day++)
{
       cout << setw(3) <<day;
    if((day + janday)% 7 == 0)
      {
              cout << endl;
                }
                }
               
                janday=(janday+(numdays))%7;
                if(janday == 0)
                janday = 7;
}

    void PrintHeading ()
    {
    if(month == 1)
     {
     cout << setw(9)<< year << endl;
     cout << endl;
     cout << setw(14)<<"January" << endl;
     cout << " S  M  T  W  Th  F  S" << endl;
     cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  
}
    else if(month == 2)
    {
    cout << endl;
    cout << setw(15)<<"February" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  
}
    else if(month == 3)
    {
    cout << endl;
    cout << setw(13)<<"March" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    
}
    else if(month == 4)
    {
    cout << endl;
    cout << setw(13)<<"April" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    
}
    else if(month == 5)
    {
    cout << endl;
    cout << setw(12)<<"May" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
   
}
    else if(month == 6)
    {
    cout << endl;
    cout << setw(13)<<"June" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     
}
    else if(month == 7)
    {
    cout << endl;
    cout << setw(13)<<"July" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
    else if(month == 8)
    {
    cout << endl;
    cout << setw(14)<<"August" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     
}
    else if(month == 9)
    {
    cout << endl;
    cout << setw(16)<< "September" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     
}
    else if(month == 10)
    {
    cout << endl;
    cout << setw(14)<< "October" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
 
}
    else if(month == 11)
    {
    cout << endl;
    cout << setw(15)<< "November" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     
}
    else if(month == 12)
    {
    cout << endl;
    cout << setw(15)<< "December" << endl;
    cout << " S  M  T  W  Th  F  S" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    
}
}

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,417
  • Joined: 27-December 08

Re: How to start the next month with a new day! Calendar Program

Posted 24 January 2011 - 06:41 PM

Duplicate threads merged AGAIN. Please STOP opening duplicate threads for the same topic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1