C++ calendar problem?

This is a discussion on C++ calendar problem? within the C++ Programming forums, part of the General Programming Boards category; Hey everyone So I need to make a calendar that displays 12 months for a fixed year. So far I ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    6

    Red face C++ calendar problem?

    Hey everyone So I need to make a calendar that displays 12 months for a fixed year. So far I can make it display all the month names, he right amount of days, and its formatted nice. Thing is, I cant get the first day of the month to work... so they all start on Sunday. Heres the code I have now:

    Code:
    #include <iostream> 
    #include <string>
    #include <iomanip>
    using namespace std;
    
    int MonthDays[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
    string MonthNames [13]={"Null","January","February","March…
    void printmonth( const int month, const int startday, const bool leapyear); 
    
    int main(void)
    {
    printmonth(1,0,false); //Print January, 1st on Sunday
    printmonth(2,1,true); //Print February, 1st on Monday, Leap year
    printmonth(3,2,false); //Print March, 1st on Tuesday
    printmonth(4,3,false); //Print April, 1st on Wednesday
    printmonth(5,5,false); //Print May, 1st on Friday 
    printmonth(6,6,false); //Print June, 1st on Saturday
    printmonth(7,0,false); //Print July, 1st on Sunday
    printmonth(8,1,false); //Print August, 1st on Monday
    printmonth(9,2,false); //Print September, 1st on Tuesday
    printmonth(10,3,false); //Print October, 1st on Wednesday
    printmonth(11,4,false); //Print November, 1st on Thursday
    printmonth(12,5,false); //Print December, 1st on Friday
    system ("pause");
    return 0;
    }
    
    
    void printmonth( const int month, const int startday, const bool leapyear)
    {
    int daysinmonth = MonthDays[month];
    int daynumber=1;
    
    cout << " " << MonthNames[month] << endl;
    cout << endl;
    cout <<" Sun"<<" "<<"Mon"<<" "<<"Tue"<<" "<<"Wed" <<" "<<"Thu"<<" "<<"Fri"<<" "<<"Sat"<<endl;
    cout << endl;
    
    void wrapper(int daynumber, int MonthDays);
    {
    int colCount=1;
    daynumber=1;
    
    while (daynumber <= daysinmonth) 
    { 
    colCount+1;
    if (colCount == 8) 
    {
    cout << endl;
    colCount = 1; 
    }
    
    cout << setw(4); 
    cout << daynumber; 
    colCount++; 
    daynumber++;
    }
    colCount = 1;
    cout << endl << endl; 
    }
    
    }
    So what should I do? I was thinking a switch(startday) statement but it wasn't working... Thanks so much guys!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    You could start with indenting the code -> SourceForge.net: Indentation - cpwiki
    It makes it so much easier to read.

    For example, I've no idea what line 39 is trying to achieve.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    ^^I know it does but it lost indentation when I pasted it. Line 39 is my void 'wrapper' function to format the months correctly. Is there something wrong with it?

  4. #4
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    You have a function in a function. Put wrapper() into printMonth() and make it one function.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    Alright I'll try and merge them, any idea how to solve the OP?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Re-post your indented code first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    Code:
    #include <iostream> 
    #include <string>
    #include <iomanip>
    using namespace std;
    
    
    int MonthDays[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
    string MonthNames [13]={"Null","January","February","March","April","May","June","July","August","September","October","November","December"};
    void printmonth( const int month, const int startday, const bool leapyear); 
    
    
    int main(void)
    	{
    		printmonth(1,0,false); //Print January, 1st on Sunday
    		printmonth(2,1,true); //Print February, 1st on Monday, Leap year
    		printmonth(3,2,false); //Print March, 1st on Tuesday
    		printmonth(4,3,false); //Print April, 1st on Wednesday
    		printmonth(5,5,false); //Print May, 1st on Friday 
    		printmonth(6,6,false); //Print June, 1st on Saturday
    		printmonth(7,0,false); //Print July, 1st on Sunday
    		printmonth(8,1,false); //Print August, 1st on Monday
    		printmonth(9,2,false); //Print September, 1st on Tuesday
    		printmonth(10,3,false); //Print October, 1st on Wednesday
    		printmonth(11,4,false); //Print November, 1st on Thursday
    		printmonth(12,5,false);  //Print December, 1st on Friday
    		system ("pause");
    		return 0;
    	}
    
    
    
    
    void printmonth( const int month, const int startday, const bool leapyear)
    	{
    		int daysinmonth = MonthDays[month];
    		int daynumber=1;
    		int colCount=1;
    
    
    		cout << "           " << MonthNames[month] << endl;
    		cout << endl;
    		cout <<"  Sun"<<" "<<"Mon"<<" "<<"Tue"<<" "<<"Wed" <<" "<<"Thu"<<" "<<"Fri"<<" "<<"Sat"<<endl;
    		cout << endl;
    
    
    		   while (daynumber <= daysinmonth) 
    		   { 
    				colCount+1;
    		     if (colCount == 8) 
    					 {
    					    cout << endl;
    					    colCount = 1; 
    					 }
    
    
    		      cout << setw(4); 
    		      cout << daynumber; 
    		      colCount++; 
    		      daynumber++;
    		  }
                colCount = 1;
        cout << endl << endl; 
    	}
    Alright there it is; functions merged and indented

  8. #8
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    There are a few ways to go about this. The way I was thinking is keep a running total of all the days and pass by reference to print month(you'll have to create a new int for that). Then take that number and mod it by 7 and add 1 to give you the starting point at each month. The very first month should start at 0 (on sunday). The next month would be 0+31, so (31%7)+1=4, so it will start 4 days over and so on. Next would be 0+31+28=(59%7)+1=4, so it would again start 4 days over. You'll have to do something similar to your colCount.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Consider these two changes.
    Code:
            int colCount=startday;
    
    
            cout << "           " << MonthNames[month] << endl;
            cout << endl;
            cout <<"  Sun"<<" "<<"Mon"<<" "<<"Tue"<<" "<<"Wed" <<" "<<"Thu"<<" "<<"Fri"<<" "<<"Sat"<<endl;
            cout << endl;
            cout << setw(colCount*3) << "";
    It's not quite the right answer, but it should get you thinking in the right direction.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    ^YES! Thanks you Salem I integrated it, made a couple adjustments, and now I'm really close! All the days start correctly now I'm just trying to figure out how to get it to print to the next line at the right time... its printing 1 date outside of the column. Heres the code revised:

    Code:
    #include <iostream> 
    #include <string>
    #include <iomanip>
    using namespace std;
    
    
    int MonthDays[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
    string MonthNames [13]={"Null","January","February","March","April","May","June","July","August","September","October","November","December"};
    void printmonth( const int month, const int startday, const bool leapyear); 
    
    
    int main(void)
    	{
    		printmonth(1,0,false); //Print January, 1st on Sunday
    		printmonth(2,1,true); //Print February, 1st on Monday, Leap year
    		printmonth(3,2,false); //Print March, 1st on Tuesday
    		printmonth(4,3,false); //Print April, 1st on Wednesday
    		printmonth(5,5,false); //Print May, 1st on Friday 
    		printmonth(6,6,false); //Print June, 1st on Saturday
    		printmonth(7,0,false); //Print July, 1st on Sunday
    		printmonth(8,1,false); //Print August, 1st on Monday
    		printmonth(9,2,false); //Print September, 1st on Tuesday
    		printmonth(10,3,false); //Print October, 1st on Wednesday
    		printmonth(11,4,false); //Print November, 1st on Thursday
    		printmonth(12,5,false);  //Print December, 1st on Friday
    		system ("pause");
    		return 0;
    	}
    
    
    
    
    void printmonth( const int month, const int startday, const bool leapyear)
    	{
    		int daysinmonth = MonthDays[month];
    		int daynumber=1;
    		int colCount=startday;
    
    
    		cout << "           " << MonthNames[month] << endl;
    		cout << endl;
    		cout <<"  Sun"<<" "<<"Mon"<<" "<<"Tue"<<" "<<"Wed" <<" "<<"Thu"<<" "<<"Fri"<<" "<<"Sat"<<endl;
    		cout << endl;
    		cout << setw((colCount*3)+startday) << "";
    	
    
    
    		   while (daynumber <= daysinmonth) 
    		   { 
    		     if (colCount == 8) 
    					 {
    					    cout << endl;
    					    colCount = 1; 
    					 }
    
    
    		      cout << setw(4); 
    		      cout << daynumber; 
    		      colCount++; 
    		      daynumber++;
    		  }
                colCount = 1;
        cout << endl << endl; 
    	}
    What do you guys think? I've ben trying to add 1 to the colCount but everywhere I do it it seems to make no difference...

  11. #11
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    What is the startday? Is that what day of the week the 1st of the month is?

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    ^^ yep like a startday of 0 is January, starting on Sunday. 1 is Mon... so on

  13. #13
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    OK, I think you need to fix your start days. Jan 1st will be 0, but Feb 1st will be on Weds...so 3 would be the start day. March start day would be 3 also.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calendar in C
    By Prolapsis in forum C Programming
    Replies: 9
    Last Post: 06-17-2011, 11:56 AM
  2. Calendar
    By Deerman in forum C++ Programming
    Replies: 6
    Last Post: 08-08-2007, 12:18 PM
  3. how to convert from solar calendar to lunar calendar??
    By zaracattle in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2006, 07:17 AM
  4. C++ calendar HELP!!!
    By chocha7 in forum C++ Programming
    Replies: 7
    Last Post: 11-14-2004, 08:06 AM
  5. Calendar Problem
    By wordup in forum C Programming
    Replies: 7
    Last Post: 10-29-2002, 03:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21