Thread: Calendar Program. What am I doing wrong?

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    7

    Calendar Program. What am I doing wrong?

    Hey everyone. I'm typing up a program which prints out a calendar for any month and year (starting with 1 A D) which the user inputs. I found a general algorithm for calculating the first day of the month but when i cout it in my if statements, something's wrong. It prints out | | 1 no matter what B is equal to. The algorithm is

    1) Subtract 1 from the year and store the result in a variable A.
    2) Multiply A by 365
    3) Find the quotient when you divide the (year - 1) by 4 (ignore remaindr) and add this to A
    4) Find the quotient when you divide the (year - 1) by 100 (ignore the remainder) and subtract this from A.
    5) Find the quotient when you divide the (year - 1) by 400 (ignore the remainder) and add this to A.
    6) Add the days in the year prior to this month (dont worry about leap years here) to A.
    7) Add 1 to A (for first day of month)
    8) If the year is a leap year and the month is after February add 1 to A. Note: A year is a leap year if it is divisible by 4 AND if it is not a centennial OR it is a centennial divisible by 400.
    9) Find the remainder when dividing A by 7

    Here's my code so far:
    Code:
    // Calendar Project
    // Ez Nadar
    
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int day(int year, int month);
    int daysbefore(int month);
    int main()
    {
    	int month = 0, year = 0, d = 0, b = 0;
    
    
    
    	cout << "Enter the year of the calendar" << endl;
    	cin >> year;
    	cout << "Enter the month of the calendar" << endl;
    	cin >> month;
    
    	d = daysbefore(month);
    	b = day(year, month);
    
    	if ( month == 12)
    		cout << "		     " << "Calendar of December " << year << endl;
    
    	if ( month == 11)
    		cout << "		     " << "Calendar of November " << year << endl;
    
    	if ( month == 10)
    		cout << "		     " << "Calendar of October " << year << endl;
    
    	if ( month == 9)
    		cout << "		     " << "Calendar of September " << year << endl;
    
    	if ( month == 8)
    		cout << "		     " << "Calendar of August " << year << endl;
    
    	if ( month == 7)
    		cout << "		     " << "Calendar of July " << year << endl;
    
    	if ( month == 6)
    		cout << "		     " << "Calendar of June " << year << endl;
    
    	if ( month == 5)
    		cout << "		     " << "Calendar of May " << year << endl;
    
    	if ( month == 4)
    		cout << "		     " << "Calendar of April " << year << endl;
    
    	if ( month == 3)
    		cout << "		     " << "Calendar of March " << year << endl;
    
    	if ( month == 2)
    		cout << "	         " << "Calendar of February " << year << endl;
    
    	if ( month == 1)
    		cout << "		     " << "Calendar of January " << year << endl;
    
    	cout << "	    ----------------------------------------- " << endl;
    	cout << "	   | Sun | Mon | Tue | Wed | Thu | Fri | Sat |" << endl;
    	cout << "	    ----------------------------------------- " << endl;
    
    	if ( b = 0)
    
    	cout << "	   |    1|";
    
    	else if ( b = 1)
    
    	cout << "	   |	 |	  1|";
    
    	else if ( b = 2)
    
    	cout << "	   |	 |	   |  1|";
    
    	else if ( b = 3)
    
    	cout << "	   |	 |	   |	 |	  1|";
    
    	else if ( b = 4)
    
    	cout << "	   |	 |	   |	 |	   |    1|";
    	else if ( b = 5)
    
    	cout << "	   |	 |	   |	 |	   |     |	  1|";
    		
    	else if ( b = 6)
    
    	cout << "	   |	 |	   |	 |	   |     |	   |    1|";
    	return 0;
    }
    int  daysbefore(int month) // calculates days in the year before this month
    {
    	int d = 0;
    
    	if ( month == 12)
    	d = 334;
    
    	if ( month == 11)
    	d = 304;
    
    	if ( month == 10)
    	d = 273;
    
    	if ( month == 9)
    	d = 243;
    
    	if ( month == 8)
    	d = 212;
    
    	if ( month == 7)
    	d = 181;
    
    	if ( month == 6)
    	d = 151;
    
    	if ( month == 5)
    	d = 120;
    
    	if ( month == 4)
    	d = 90;
    
    	if ( month == 3)
    	d = 59;
    
    	if ( month == 2)
    	d = 31;
    
    	if ( month == 1)
    	d = 0;
    
    	return d;
    }
    int day(int year, int month) // determines first day of month
    {
    	int a = 0, b = 0;
    a = year - 1;
    a = a * 365;
    a = a + ((year - 1) / 4);
    a = a - ((year - 1) / 100);
    a = a + ((year - 1) / 400);
    a = a + daysbefore(month);
    a = a + 1;
    
    if ( ( year % 100 ) != 0 )
    
    	if ( (year % 4) != 0 )
    	{
    		a = a + 1;
    	}
    
    b = (a%7);
    return b;
    }
    I know the spacing is off in my cout statements
    Last edited by Sektor; 01-17-2004 at 07:34 PM.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    For those if statements involving b, you're using the assignment operator = instead of the comparison operator ==.

    Also, read up on switch. That would help you here.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    7
    ohh, thanks . It's always good to have another pair of eyes . Yeah, ill follow up on the switches. thanks for the tip!

    I just switched the '=' to '==' but now none of the values of b are couting . I dont know why
    Last edited by Sektor; 01-17-2004 at 10:13 PM.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Smile

    You're welcome.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  2. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  3. Yet another Calendar Program.
    By duffmckagan in forum C Programming
    Replies: 9
    Last Post: 07-14-2006, 10:29 PM
  4. calendar program for linux
    By *ClownPimp* in forum Tech Board
    Replies: 1
    Last Post: 10-01-2005, 02:31 AM