RE: A possible constructor bug in my program

  • From: "Joseph Lee" <joseph.lee22590@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Fri, 30 Jul 2010 08:24:18 -0700

Hi Dave,
Thanks for your help. I've fixed it; turns out it was an assignment logic
error (I should have assigned the input argument (d) to the correct value
before assigning it to the day data).
Cheers,
Joseph

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Dave
Sent: Friday, July 30, 2010 8:09 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: A possible constructor bug in my program

...
if (d < 1)
this->day = 1;
else if (d > days_per_month(month, y))
this->day = days_per_month(month, y);
else day_error = false;
this->day = d;
// do you want to do this even when the day is invalid (i.e. set the
instance day to the passed in day)? In this case, you should probably
use braces to clarify the logic here; he else above only executes the
single statement after itthis->year = y;
...

On 7/30/10, Joseph Lee <joseph.lee22590@xxxxxxxxx> wrote:
> Hi guys,
> I'm working on a Date class to store month, day and year and print out the
> date in question. I've defined three constructors - a default one (Jan 1,
> 1900), a constructor to take month as number (int m, int d, int y) and
> another for taking month name as string (string m_name, int d, int y). To
> help me set up the object correctly, I've implemented several helper
> functions such as leap year test and an accessor to return days per month
> for that particular month.
> Here's the scenario with my code (in C++):
> * Class name: Date.
> * Encapsulated data: three ints (month, day, year) and a string
> (month_name).
> * Member functions: Three constructors (default, two parameterized ones),
> two print functions (numeric date output and alpha date output).
> * Private helpers: leap year test (bool), return month name for a month
> number (string), return month number for a month name (int), days per
month
> (int).
> // Code:
>
> // Second parameterized constructor: month as name.
> // A bit more complicated than last time, but a bit easier at first
glance.
> // Use the helper function to get the month number based on a month name
> e.g. July for 7.
> // In case of wrong input, which I need to check regularly, send out an
> error message and set the correct date.
> // For instance, if one types one/1/2010, I should set it to
> current_month/1/2010 e.g. 7/1/2010.
> // It'll be better to leave the other guys alone and just put the current
> month name to the screen. However, becauswe of the framework rule, I need
to
> put in 1/1/1900.
> Date::Date(string m_name, int d, int y)
>       {
> month  = number(m_name);
>       month_name = name(month);
>       bool day_error  = true; // To check for date entry.
> if (d < 1)
> this->day = 1;
> else if (d > days_per_month(month, y))
> this->day = days_per_month(month, y);
> else day_error = false;
> this->day = d;
> this->year = y;
> // After our checking code...
> if (day_error)
>       {cout << "Error: invalid day entered: " << d << endl;
> cout << "Day reset to: " << day << endl;
> this->print_numeric();
> }
>       }
> // End of constructors.
>
>       
> // Other needy ones...
>
> // Print date in numeric form e.g. 7/1/2010.
> void Date::print_numeric() const
>       {
> cout << month << "/" << day << "/" << year << endl;
>       }
>
> // Print date in alpha form e.g. July 1, 2010.
> void Date::print_alpha() const
>       {
> cout << month_name << " " << day << ", " << year << endl;
>       }
>
>
> // The following are helper functions - just to help out our Date object.
>
> // Leap year test: A leap year is ones divisible by 4 and if they are
> divisible by 400.
> // For instance, 1700, 1800, 1900 and 2100 are regular years and 1996,
2000
> and 2012 are leap years.
> // This function will check the year (the int argument) to see if the
> remainder of modulus 4 is zero or not - useful for putting February 29th.
> bool Date::is_leap(int y) const
>       {
> return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));
>       }
>
> // Days per month: 30 for April, June, September and November, 31 for
others
> except Feb.
> // In case of February, use is_leap to help determine whether it should be
> 28 (false) or 29 (true).
> int Date::days_per_month(int m, int y) const
>       {
> if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
> return 31;
> if (m == 4 || m == 6 || m == 9 || m == 11) return 30;
> if (m == 2)
>         {
> if    (is_leap(y)) return 29;
> else return 28;
>       } return 0; // can never reach this point
>       }
>
> So, for example, if I type "Jan 32, 2010" into the console (all three
> variables using separate output), I'm supposed to get an error message
about
> invalid input, then the day should be reset to days per month, which is
31.
> In my case, it does not happen - the "day" in the constructor is set to
the
> date I've entered no matter what e.g. it'll print 1/32/2010 via
> print_numeric function. After extensive testing, I found that it must be a
> constructor error (my instructor was a bit puzzled about this code as
well).
> Thanks for your considerations on fixing this problem.
> Cheers,
> Joseph
>
>
> __________
> View the list's information and change your settings at
> //www.freelists.org/list/programmingblind
>
>
__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

No virus found in this incoming message.
Checked by AVG - www.avg.com 
Version: 8.5.441 / Virus Database: 271.1.1/3036 - Release Date: 07/29/10
18:34:00

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: