[nfbcs] Help with Calendar Project.

Lanie Molinar laniemolinar91 at gmail.com
Sun Oct 29 09:07:34 UTC 2017


Hi, everyone. I'm sending this again with a modified PuTTY log since I 
got a message saying my email was too large to send to program-l. If you 
don't mind, I need your help again. I'm working on a major C++ project 
that puts a lot of what I've learned over the past few weeks together. 
It asks the user for the month and year and then displays a calendar, 
kind of like the assignment I did the other day. I think I almost have 
it, but for some reason, some of the tests my school's testbed system 
does are showing a bunch of blank lines instead of the expected output, 
and I have no idea why. Other tests work fine, but then there are a few 
that don't. I know there are a few other things that I still need to 
correct, but I'm trying to fix this issue first. Can you please take a 
look at my code and my log from PuTTY and help me understand what I 
could be doing wrong? Thanks.

-------------- next part --------------
/***********************************************************************
* Program:
*    Project 07, Calendar
*    Sister Unsicker, CS124
* Author:
*    Lanie Molinar
* Summary: 
*    This program gets the month and year from the user and then uses that 
*    data to display a calendar.
*
*    Estimated:  5.0 hrs   
*    Actual:     6.0 hrs
*      At first, I tried to creat the displayTable functionby using my 
*      pseudocode from project 6, without using or looking at the table I 
*      creatd in assignment 25. I ended up having to rethink that strategy 
*      when i got bad results from doing that, I plugged in the function from
*      assignment 25 and everything worked perfectly.
************************************************************************/

#include <iostream>
#include <iomanip>
using namespace std;

/***********************************************************************
* This function prompts the user for the month, making sure it's between 1 and 
* 12,  and then returns it to main().
***********************************************************************/
int getMonth()
{
   int month;
   do
   {
      cout << "Enter a month number: ";
      cin >> month;
   }
   while (month < 1 || month > 12);
   return month;
}

/***********************************************************************
* This function prompts the user for the year, making sure it's within an 
* acceptable range, and then returns it to main().
***********************************************************************/
int getYear()
{
   int year;
   do
   {
      cout << "Enter year: ";
      cin >> year;
      cout << endl;
   }
   while (year < 1753);
   return year;
}

/***********************************************************************
* This function determines whether the year entered by the user is a leap 
* year.
***********************************************************************/
bool isLeapYear(int year)
{
   bool isLeap;
   if ((year % 4 == 0) && (year % 400 != 0))
      isLeap = true;
   else
      isLeap = false;
   return isLeap;
}

/***********************************************************************
* This function determines how many days are in a given year, calling 
* isLeapYear() to help it do so.
***********************************************************************/
int numDaysInYear(int year)
{
   int numDays;
   bool isLeap = isLeapYear(year);
   if (isLeap == true)
      numDays = 366;
   else
      numDays = 365;
   return numDays;
}

/***********************************************************************
* This function determines the number of days in a given month, calling 
* isLeapYear() to help it do so.
***********************************************************************/
int numDaysInMonth(int month, int year)
{
   int numDays;
   if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || 
   month == 10 || month == 12)
      numDays = 31;
   else if (month == 2)
      numDays = 28 + isLeapYear(year);
   else if (month == 4 || month == 6 || month == 9 || month == 11)
      numDays = 30;
   return numDays;
}

/***********************************************************************
*This function computes the offset from Monday for the given month.
***********************************************************************/
int computeOffset(int month, int year)
{
   int numDays = numDaysInYear(year);
   for (int startMonth = 1; startMonth < month; startMonth++)
      numDays += numDaysInMonth(startMonth, year);
   for (int startYear = 1753; startYear < year; startYear++)
      numDays += numDaysInYear(startYear);
   int offset = (numDays % 7);
   if ((month <= 2) && isLeapYear(year))
      offset--;
   return offset;
}

/***********************************************************************
* This function displays the header for the calendar table.
***********************************************************************/
void displayHeader(int month, int year)
{
   if (month == 1)
      cout << "January, ";
   else if (month == 2)
      cout << "February, ";
   else if (month == 3)
      cout << "March, ";
   else if (month == 4)
      cout << "April, ";
   else if (month == 5)
      cout << "May, ";
   else if (month == 6)
      cout << "June, ";
   else if (month == 7)
      cout << "July, ";
   else if (month == 8)
      cout << "August, ";
   else if (month == 9)
      cout << "September, ";
   else if (month == 10)
      cout << "October, ";
   else if (month == 11)
      cout << "November, ";
   else
      cout << "December, ";
   cout << year << endl;
   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";
}

/***********************************************************************
* This function displays the calendar table.
***********************************************************************/
void displayTable(int offset, int numDays)
{
   int count = 0;
   for (int day = 1; day <= numDays; )
   {
      if (offset > 0)
      {
         cout << "    ";
         offset--;
      }
      else if (offset == 0)
      {
         cout << setw(4) << day;
         day++;
      }
      count++;
      if ((count % 7 == 0) && (day != (numDays + 1)))
         cout << endl;
      if (day > numDays)
         cout << endl;
   }
}

/***********************************************************************
* This function calls the functions that display the calendar header an table.
***********************************************************************/
void display(int month, int year, int offset)
{
   displayHeader(month, year);
   int numDays = numDaysInMonth(month, year);
   displayTable(offset, numDays);
}

/**********************************************************************
*    The main function calls many of the other functions in this program.
***********************************************************************/
int main()
{
   int month =getMonth();
   int year = getYear();
   int offset = computeOffset(month, year);
   display(month, year, offset);
   return 0;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: putty.log
Type: application/octet-stream
Size: 76289 bytes
Desc: not available
URL: <http://nfbnet.org/pipermail/nfbcs_nfbnet.org/attachments/20171029/af0c4763/attachment.obj>


More information about the NFBCS mailing list