please help!!!!!

[b][red]This message was edited by skapoor111 at 2004-10-31 23:7:12[/red][/b][hr]
I have written some code for a program in c++ that displays a calendar for a year taking into consideration leap years...what i need help on is i need to modify this so that it asks the user to input a year and a month and then display a calendar for only that month. please help ! thanks. here is my code so far:


#include
#include
#include
using std::cin;
using std::cout;
using std::cerr;

#include
using std::setw;



int daysInMonth(const int month, const bool leap)
{
assert(1<=month && month<=12);

switch(month) {
case 1: return(31);
case 2: return(leap ? 29 : 28);
case 3: return(31);
case 4: return(30);
case 5: return(31);
case 6: return(30);
case 7: return(31);
case 8: return(31);
case 9: return(30);
case 10: return(31);
case 11: return(30);
case 12: return(31);
}

return(0);
}


void printMonthName(const int month)
{


switch(month) {
case 1: cout << "Jan."; break;
case 2: cout << "Feb."; break;
case 3: cout << "Mar."; break;
case 4: cout << "Apr."; break;
case 5: cout << "May "; break;
case 6: cout << "Jun."; break;
case 7: cout << "Jul."; break;
case 8: cout << "Aug."; break;
case 9: cout << "Sep."; break;
case 10: cout << "Oct."; break;
case 11: cout << "Nov."; break;
case 12: cout << "Dec."; break;
default: cout << " "; break;
}
}


void printCalendarForMonth(const int month, const bool leap, const int dow1st)
{
assert(1<=month && month<=12);
assert(0<=dow1st && dow1st<=6);

const int lastDay = daysInMonth(month, leap);
assert(28<=lastDay && lastDay<=31);

printMonthName(month);
cout << " Su Mo Tu We Th Fr Sa
";

for(int dayWeekBegins=1-dow1st; dayWeekBegins<=lastDay; dayWeekBegins+=7) {
printMonthName(0);
cout << ' ';

for(int day=dayWeekBegins; day<dayWeekBegins+7; day++)
if(day > lastDay) break;
else if(day < 1) cout << " ";
else cout << setw(3) << day;

cout << '
';
}
}


bool isLeapYear(const int year)
{

const bool isMultiple4 = ((year % 4) == 0);
const bool isMultiple100 = ((year % 100) == 0);
const bool isMultiple400 = ((year % 400) == 0);

return((isMultiple4 && !isMultiple100) || isMultiple400);
}


int dowJan1ofYear(const int year)
{

const int leaps = (year>0) ? 1 + (year-1)/4 - (year-1)/100 + (year-1)/400
: - (-year)/4 + (-year)/100 - (-year)/400;

const int dow = (6 + (year % 7) + (leaps % 7)) % 7;
assert((-6)<=dow && dow<=6);
return((dow<0) ? dow+7 : dow);
}


void printCalendarForYear(const int year)
{
const bool leap = isLeapYear(year);

int dow = dowJan1ofYear(year);
assert(0<=dow && dow<=6);

printMonthName(0);
cout << " ==== YEAR: " << year << " ====
";

for(int month=1; month<=12; month++) {
cout << '
';
printCalendarForMonth(month, leap, dow);
dow = (dow+daysInMonth(month, leap)) % 7;
}
}


int main(void)
{
cout << "This program uses the proleptic "
"Gregorian calendar with a Year 0.";

while(true) {
cout << "
Print a calendar for what year? ";
int year; cin >> year;

if(!cin) break;
else cout << '
';

printCalendarForYear(year);
}

if(!cin.eof()) {
cerr << "Error reading year; exiting.
";
return(EXIT_FAILURE);
}

cout << "
Ok, I hope you enjoyed your calendar(s)!
";
return(EXIT_SUCCESS);
}


Comments

  • Add the appropriate logic to get the desired month from the user in main and convert the value to the appropriate integer if needed.

    Then it looks like you could add a second input value to printCalendarForYear for the month desired:
    printCalendarForYear(const int year, const int inputMonth = 0)
    If it is 0, then print the entire year by going through the loop as you have it already, otherwise loop to desired month to calculate dow and call printCalendarForMonth with the input month value instead of looping for all the months of the year.
    [code]
    if (inputMonth <= 0)
    {
    for(int month=1; month<=12; month++)
    {
    cout << '
    ';
    printCalendarForMonth(month, leap, dow);
    dow = (dow+daysInMonth(month, leap)) % 7;
    }
    }
    else
    {
    for (int x = 1; x < inputMonth; x++)
    dow = (dow + daysInMonth(month, leap)) % 7;
    printCalendarForMonth(inputMonth, leap, dow);
    }
    [/code]


    : [b][red]This message was edited by skapoor111 at 2004-10-31 23:7:12[/red][/b][hr]
    : I have written some code for a program in c++ that displays a calendar for a year taking into consideration leap years...what i need help on is i need to modify this so that it asks the user to input a year and a month and then display a calendar for only that month. please help ! thanks. here is my code so far:
    : [code]
    :
    : #include <cassert>
    : #include
    : #include
    : using std::cin;
    : using std::cout;
    : using std::cerr;
    :
    : #include
    : using std::setw;
    :
    :
    :
    : int daysInMonth(const int month, const bool leap)
    : {
    : assert(1<=month && month<=12);
    :
    : switch(month) {
    : case 1: return(31);
    : case 2: return(leap ? 29 : 28);
    : case 3: return(31);
    : case 4: return(30);
    : case 5: return(31);
    : case 6: return(30);
    : case 7: return(31);
    : case 8: return(31);
    : case 9: return(30);
    : case 10: return(31);
    : case 11: return(30);
    : case 12: return(31);
    : }
    :
    : return(0);
    : }
    :
    :
    : void printMonthName(const int month)
    : {
    :
    :
    : switch(month) {
    : case 1: cout << "Jan."; break;
    : case 2: cout << "Feb."; break;
    : case 3: cout << "Mar."; break;
    : case 4: cout << "Apr."; break;
    : case 5: cout << "May "; break;
    : case 6: cout << "Jun."; break;
    : case 7: cout << "Jul."; break;
    : case 8: cout << "Aug."; break;
    : case 9: cout << "Sep."; break;
    : case 10: cout << "Oct."; break;
    : case 11: cout << "Nov."; break;
    : case 12: cout << "Dec."; break;
    : default: cout << " "; break;
    : }
    : }
    :
    :
    : void printCalendarForMonth(const int month, const bool leap, const int dow1st)
    : {
    : assert(1<=month && month<=12);
    : assert(0<=dow1st && dow1st<=6);
    :
    : const int lastDay = daysInMonth(month, leap);
    : assert(28<=lastDay && lastDay<=31);
    :
    : printMonthName(month);
    : cout << " Su Mo Tu We Th Fr Sa
    ";
    :
    : for(int dayWeekBegins=1-dow1st; dayWeekBegins<=lastDay; dayWeekBegins+=7) {
    : printMonthName(0);
    : cout << ' ';
    :
    : for(int day=dayWeekBegins; day<dayWeekBegins+7; day++)
    : if(day > lastDay) break;
    : else if(day < 1) cout << " ";
    : else cout << setw(3) << day;
    :
    : cout << '
    ';
    : }
    : }
    :
    :
    : bool isLeapYear(const int year)
    : {
    :
    : const bool isMultiple4 = ((year % 4) == 0);
    : const bool isMultiple100 = ((year % 100) == 0);
    : const bool isMultiple400 = ((year % 400) == 0);
    :
    : return((isMultiple4 && !isMultiple100) || isMultiple400);
    : }
    :
    :
    : int dowJan1ofYear(const int year)
    : {
    :
    : const int leaps = (year>0) ? 1 + (year-1)/4 - (year-1)/100 + (year-1)/400
    : : - (-year)/4 + (-year)/100 - (-year)/400;
    :
    : const int dow = (6 + (year % 7) + (leaps % 7)) % 7;
    : assert((-6)<=dow && dow<=6);
    : return((dow<0) ? dow+7 : dow);
    : }
    :
    :
    : void printCalendarForYear(const int year)
    : {
    : const bool leap = isLeapYear(year);
    :
    : int dow = dowJan1ofYear(year);
    : assert(0<=dow && dow<=6);
    :
    : printMonthName(0);
    : cout << " ==== YEAR: " << year << " ====
    ";
    :
    : for(int month=1; month<=12; month++) {
    : cout << '
    ';
    : printCalendarForMonth(month, leap, dow);
    : dow = (dow+daysInMonth(month, leap)) % 7;
    : }
    : }
    :
    :
    : int main(void)
    : {
    : cout << "This program uses the proleptic "
    : "Gregorian calendar with a Year 0.";
    :
    : while(true) {
    : cout << "
    Print a calendar for what year? ";
    : int year; cin >> year;
    :
    : if(!cin) break;
    : else cout << '
    ';
    :
    : printCalendarForYear(year);
    : }
    :
    : if(!cin.eof()) {
    : cerr << "Error reading year; exiting.
    ";
    : return(EXIT_FAILURE);
    : }
    :
    : cout << "
    Ok, I hope you enjoyed your calendar(s)!
    ";
    : return(EXIT_SUCCESS);
    : }[/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion