hi... im a bit new to this forum. i saw it on google and decided to join up...

im working on a calender like program for my computer science 102 course. the parameters are as follows:
Make a program that, when given the mm/dd/yyyy, can decide what DAY the user is referring to. so for example if i input 10/31/2004 the output would look like this:
"That day is a Sunday!"
and so forth.
right now im stuck at leap years.... i need help on writing an if statement on how to determine leap years.... my program compiles and runs fine until i try some leap year.... i asked my teacher for help but the only thing she said was that "a leap year is NOT NECESSARILY EVERY 4 YEARS". any suggestions? ill post my code so you can see what im working with. oh im using dev c++ as my editor if that matters.

/* This program does XXXX */

#include <iostream>
using namespace std;

void getData(int&, int&, int&);
int findYear(int);
void calculate(void);
int calculateMonthDays(int);
void calculateBornDay(int, int, int);


int main ()
{
    calculate();    
    
    system("pause");
    return 0;
}    

void calculate ()
{
    int month;
    int day;
    int year;
    int lastDayDec;
    int monthDays;
    

    //cout << " in main\n";
    getData(month, day, year);
    lastDayDec = findYear(year);
    monthDays = calculateMonthDays(month);
    calculateBornDay(lastDayDec, monthDays, day);
    
    //cout << month << "/" << day << "/" << year << "\n";
    //cout << "***   ***" << lastDayDec << "***  ***" << monthDays;
    //cout << "***  ***" << monthDays;
    //calculate();
    
    return;
}

void getData(int& month, int& day, int& year)
{
    cout << "Enter the month in which you were born: ";
    cin >> month;
    cout << "\nDay: ";
    cin >> day;
    cout << "\nYear: ";
    cin >> year;
    
    return;
}

int calculateMonthDays(int month)
{
    int days = 0;

    month = month - 1;
    

    switch (month)
{
    case 12 : days = days + 31;

    case 11 : days = days + 30;

    case 10 : days = days + 31;

    case 9 : days =  days + 30;

    case 8 : days = days + 31;

    case 7 : days = days + 31;

    case 6 : days = days + 30;

    case 5 : days = days + 31;

    case 4 : days = days + 30;

    case 3 : days = days + 31;

    case 2 : days = days + 28;

    case 1 : days = days + 31;

    default : days = days + 0;

}
    //cout << "\n " << days << "\n";
    return days;
}


int findYear(int year)
{
    int temp1, temp2, temp3, temp4, temp5, temp6;
    int answer;

    temp1 = (year - 1);
    temp2 = temp1 * 365;
    temp3 = year - 1;
    temp4 = temp3 / 4;
    temp5 = temp3 / 100;
    temp6 = temp3 / 400;
    answer = (temp2 + temp4 - temp5 + temp6) % 7;

    //cout << "\n*********" << answer << "\n";

    return answer;
}

void calculateBornDay(int lastDayDec, int monthDays, int day)
{
    
    int bornDay;
    
    bornDay = (lastDayDec + monthDays + day) % 7;
    
    if (bornDay == 0)
        cout << "You were born on a Sunday.\n";
    else if (bornDay == 1)
        cout << "You were born on a Monday.\n";
    else if (bornDay == 2)
        cout << "You were born on a Tuesday.\n";
    else if (bornDay == 3)
        cout << "You were born on a Wednesday.\n";
    else if (bornDay == 4)
        cout << "You were born on a Thursday.\n";
    else if (bornDay == 5)
        cout << "You were born on a Friday.\n";
    else if (bornDay == 6)
        cout << "You were born on a Saturday.\n";
}

so basically i just need a formula to figure out leap years... and leap years are NOT necessarily every 4 years... help plz?

Recommended Answers

All 6 Replies

>so basically i just need a formula to figure out leap years
If the following is true, the year is a leap year: year % 4 == 0 && year % 100 != 0 || year % 400 == 0. The test for modulo 4000 really isn't necessary because we don't need to worry about it for about 2000 years. ;)

ahhh thx a lot

hi !!!

A leap year is

year divisible by 4 and not by 100 or divisible by 400

if (((yr % 4==0)&&(yr % 100 != 0))||(yr % 400 ==0))

cout<<"Leap Year";

hope it will help

anukampa

hi... im a bit new to this forum. i saw it on google and decided to join up...

im working on a calender like program for my computer science 102 course. the parameters are as follows:
Make a program that, when given the mm/dd/yyyy, can decide what DAY the user is referring to. so for example if i input 10/31/2004 the output would look like this:
"That day is a Sunday!"
and so forth.
right now im stuck at leap years.... i need help on writing an if statement on how to determine leap years.... my program compiles and runs fine until i try some leap year.... i asked my teacher for help but the only thing she said was that "a leap year is NOT NECESSARILY EVERY 4 YEARS". any suggestions? ill post my code so you can see what im working with. oh im using dev c++ as my editor if that matters.

/* This program does XXXX */

#include <iostream>
using namespace std;

void getData(int&, int&, int&);
int findYear(int);
void calculate(void);
int calculateMonthDays(int);
void calculateBornDay(int, int, int);


int main ()
{
    calculate();    
    
    system("pause");
    return 0;
}    

void calculate ()
{
    int month;
    int day;
    int year;
    int lastDayDec;
    int monthDays;
    

    //cout << " in main\n";
    getData(month, day, year);
    lastDayDec = findYear(year);
    monthDays = calculateMonthDays(month);
    calculateBornDay(lastDayDec, monthDays, day);
    
    //cout << month << "/" << day << "/" << year << "\n";
    //cout << "***   ***" << lastDayDec << "***  ***" << monthDays;
    //cout << "***  ***" << monthDays;
    //calculate();
    
    return;
}

void getData(int& month, int& day, int& year)
{
    cout << "Enter the month in which you were born: ";
    cin >> month;
    cout << "\nDay: ";
    cin >> day;
    cout << "\nYear: ";
    cin >> year;
    
    return;
}

int calculateMonthDays(int month)
{
    int days = 0;

    month = month - 1;
    

    switch (month)
{
    case 12 : days = days + 31;

    case 11 : days = days + 30;

    case 10 : days = days + 31;

    case 9 : days =  days + 30;

    case 8 : days = days + 31;

    case 7 : days = days + 31;

    case 6 : days = days + 30;

    case 5 : days = days + 31;

    case 4 : days = days + 30;

    case 3 : days = days + 31;

    case 2 : days = days + 28;

    case 1 : days = days + 31;

    default : days = days + 0;

}
    //cout << "\n " << days << "\n";
    return days;
}


int findYear(int year)
{
    int temp1, temp2, temp3, temp4, temp5, temp6;
    int answer;

    temp1 = (year - 1);
    temp2 = temp1 * 365;
    temp3 = year - 1;
    temp4 = temp3 / 4;
    temp5 = temp3 / 100;
    temp6 = temp3 / 400;
    answer = (temp2 + temp4 - temp5 + temp6) % 7;

    //cout << "\n*********" << answer << "\n";

    return answer;
}

void calculateBornDay(int lastDayDec, int monthDays, int day)
{
    
    int bornDay;
    
    bornDay = (lastDayDec + monthDays + day) % 7;
    
    if (bornDay == 0)
        cout << "You were born on a Sunday.\n";
    else if (bornDay == 1)
        cout << "You were born on a Monday.\n";
    else if (bornDay == 2)
        cout << "You were born on a Tuesday.\n";
    else if (bornDay == 3)
        cout << "You were born on a Wednesday.\n";
    else if (bornDay == 4)
        cout << "You were born on a Thursday.\n";
    else if (bornDay == 5)
        cout << "You were born on a Friday.\n";
    else if (bornDay == 6)
        cout << "You were born on a Saturday.\n";
}

so basically i just need a formula to figure out leap years... and leap years are NOT necessarily every 4 years... help plz?

Y

There is a C code snippet in this forum under "Day of week given a date" that can give you an idea.

Dear you now that a leap year comes after 4 years, it means if we devide a year by 4 and checks its reminder is zero, if it is yes than it is understood that year is leap. so here is short programming condition, i hope it will work for you

if( (YEAR%4) == 0)
{
cout<<endl<<"Leap Year";
}

BYE!
Rizwan Yasin Khan

>i hope it will work for you
It won't work because it will give false positives. I wonder why one person felt the need to give the same answer as I did a day after I did and another did the same thing, but screwed it up!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.