// Robert A Simpson

// cs 132

// unit 3

// sept. 22, 1997

// This program will calculate the day of the week

// using an entered date.

#include <iostream.h>

main ()

{

// I had to declare all variables outside the do-while to prevent

// an error message.

int month, day, year, test, leap, counter, counter1, counter2;

long date, week;

char answer;

// This do-while loop is so the user can 'at the end of the program'

// opt. to run the program again. (the while statement is at the very end).

do{

// lable all variables

leap=0,counter=1900,counter1=1,counter2=1,week=0,date=0;

// the next 3 do-while loops are to ask for the date.

do{

cout <<"ENTER THE MONTH: ";

cin >> month;

}while (month < 1 || month >12);

do{

cout <<"ENTER THE DAY OF THE MONTH: ";

cin >> day;

// this will make sure the user enters the correct day for the correct month.

if (month==4 || month==6 || month==9 || month==11)

if (day>30)

day=0;

if (month==2 && day>29)

day=0;

}while (day <1 || day >31);

do{

cout <<"ENTER THE YEAR: ";

cin >> year;

}while (year < 1900 || year > 9999);

// this will add up the total number of days between the base

// year (1900) and the input year(year).

for (; counter < year;counter++){

// This next code will determine if the year is a

// leap year. if it is a leap year it will add 366 to date.

// I not it will add 365 to date.

if (counter%4==0)

leap=1;

if (counter%100==0)

leap=0;

if (counter%400==0)

leap=1;

if (leap==1)

date=date+366;

else date=date+365;

leap=0; // reset variable for next time through the loop.

}

// This for loop will go through all the months from Jan. until

// the month entered.

for(;counter1<month;counter1++) {

// this is a nested loop. It will add the number of days in each month

// to 'date' .

switch (counter1){

case 1 :case 3:case 5:case 7:case 8:case 10:case 12:

date=date+31;break;

case 2 :date=date+28;break;

case 4 :case 6:case 9:case 11:

date=date+30;break;

}

}

// this while loop will add the number of days from the first of the

// month until the day entered.

for (;counter2<=day;counter2++)

date++ ;

cout<<endl;

// This switch statement will find the mounth using the entered number.

switch (month){

case 1 :cout << "JANUARY ";break;

case 2 :cout << "FEBRUARY ";break;

case 3 :cout << "MARCH ";break;

case 4 :cout << "APRIL ";break;

case 5 :cout << "MAY ";break;

case 6 :cout << "JUNE ";break;

case 7 :cout << "JULY ";break;

case 8 :cout << "AUGUST ";break;

case 9 :cout << "SEPTEMBER ";break;

case 10 :cout << "OCTOBER ";break;

case 11 :cout << "NOVEMBER ";break;

case 12 :cout << "DECEMBER ";break;

}

// This will correct a known bug in my program.

// The year entered is not checked for being a leap year

// in the 'if' statment above. so I will check the entered

// year here and add the day to 'date'.

if (year%4==0)

leap=1;

if (year%100==0)

leap=0;

if (year%400==0)

leap=1;

if (leap==1 && month>2)

date++;

// now I will get the remainder of 'date \ 7 ' and will check it in

// the switch statement for the correct day.

week=date%7;

switch (week ){

case 1 :cout <<day <<", "<< year << " is on MONDAY."<<endl;break;

case 2 :cout <<day <<", "<< year << " is on TUESDAY."<<endl;break;

case 3 :cout <<day <<", "<< year << " is on WENDESDAY."<<endl;break;

case 4 :cout <<day <<", "<< year << " is on THURSDAY."<<endl;break;

case 5 :cout <<day <<", "<< year << " is on FRIDAY."<<endl;break;

case 6 :cout <<day <<", "<< year << " is on SATURDAY."<<endl;break;

case 0 :cout <<day <<", "<< year << " is on SUNDAY."<<endl;break;

}

cout <<endl;

cout <<"Do you want to find another day (y/n)? ";

cin >> answer;

cout << endl;

}while (answer=='y' || answer=='Y');

cout << "BYE!";

return 0;

}

back

Home Page