ADT Calendar

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

ADT Calendar

 
0
  #1
Feb 10th, 2008
Got most of this done. I just can't see why my advance_date() method is only partially working...I think it's something with all my bools....but I don't know how else to get this condition met. Project is to Design, implement, and test an ADT that represents a calendar date..Integers are fine...include an operation that will advance the date 'one day.' and display with words or numbers. As an 'enhancement," include the name of the day. Well, I'd like to do that as well but can't figure out the day_of_function; which is why it's commented out. Once again, the first part of my advance_date() is working, February 28 goes to 29 and 29 goes to March 1. But all the rest jump to the next month instead of incrementing by one...Just can't see anymore...Thank you:

Header:
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Date
  6. {
  7. public:
  8.  
  9. //day_of_week();
  10. void advance_date();
  11. // increments the date
  12. void display_date();
  13. // outputs the date
  14. void change_date(int month1, int day1, int year1);
  15. // Reads in new values for the arguments day, month, and year of Date
  16. void name_date();
  17. // Assigns a numeric value which represents a determined date
  18. Date();
  19. // Default Constructor
  20. Date(int month1, int day1, int year1);
  21. // Constructor with neccessary arguments to produce date output
  22.  
  23. private:
  24.  
  25. int day, month, year;
  26. };
  27. //End Date


Implementation:

  1. #include "DATE.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. Date::Date():month(01),day(01),year(2008)
  7. {
  8. }
  9.  
  10. Date::Date(int month1, int day1, int year1)
  11. {
  12.  
  13. month = month1;
  14. day = day1;
  15. year = year1;
  16. }
  17.  
  18. void Date::advance_date()
  19. {
  20.  
  21. if(month == 2 && day > 1 && day < 29)
  22. {
  23. day += 1;
  24. //month = month;
  25. }
  26.  
  27. else if(day == 29)
  28. {
  29. day = 1;
  30. month += 1;
  31. }
  32. //day = day + 1;
  33. else if((month == 4 || month == 6 || month == 9 || month == 11) && (day > 1 && day < 30))
  34. {
  35. day += 1;
  36. //month += 1;
  37. }
  38.  
  39. else if(day == 30)
  40. {
  41. day == 1;
  42. month += 1;
  43. }
  44.  
  45. else if((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day > 1 && day < 31))
  46. {
  47. day += 1;
  48. //month = month;
  49. }
  50.  
  51. else if(day > 31)
  52. {
  53. day == 1;
  54. month += 1;
  55. }
  56.  
  57. if(month > 12)
  58. {
  59. month == 1;
  60. }
  61. }
  62.  
  63.  
  64. void Date::display_date()
  65. {
  66. cout << month << "-" << day << "-" << year << endl;
  67. }
  68.  
  69. void Date::name_date()
  70. {
  71.  
  72. if(month == 1)
  73. {
  74. cout << "January " << day << ", " << year << endl;
  75. }
  76. else if(month == 2)
  77. {
  78. cout << "February " << day << ", " << year << endl;
  79. }
  80. else if (month == 3)
  81. {
  82. cout << "March " << day << ", " << year << endl;
  83. }
  84. else if (month == 4)
  85. {
  86. cout << "April " << day << ", " << year << endl;
  87. }
  88. else if (month == 5)
  89. {
  90. cout << "May " << day << ", " << year << endl;
  91. }
  92. else if (month == 6)
  93. {
  94. cout <<"JUNE " << day << ", " << year << endl;
  95. }
  96. else if (month == 7)
  97. {
  98. cout << "July " << day << ", " << year << endl;
  99. }
  100. else if (month == 8)
  101. {
  102. cout << "August " << day << ", " << year << endl;
  103. }
  104. else if (month == 9)
  105. {
  106. cout << "September " << day << ", " << year << endl;
  107. }
  108. else if (month == 10)
  109. {
  110. cout << "October " << day << ", " << year << endl;
  111. }
  112. else if (month == 11)
  113. {
  114. cout << "November " << day << ", " << year << endl;
  115. }
  116. else if(month == 12)
  117. {
  118. cout << "December " << day << ", "<< year<< endl;
  119. }
  120. }
  121.  
  122.  
  123.  
  124. void Date::change_date(int month1, int day1, int year1)
  125. {
  126. day = day1;
  127. month = month1;
  128. year = year1;
  129. }

Test File:

  1. #include <iostream>
  2. #include "DATE.h"
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. Date make_a_date;
  8. make_a_date.change_date(04,29,2008);
  9. make_a_date.display_date();
  10. make_a_date.name_date();
  11. make_a_date.advance_date();
  12. cout << endl;
  13. cout << "The advanced date is: " << endl;
  14. make_a_date.display_date();
  15. system("pause");
  16.  
  17. return 0;
  18. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: ADT Calendar

 
0
  #2
Feb 10th, 2008
You should really consider using arrays for some of your data. For example, you could create an array that contains the number of days in each month. Your advance_date() function would be considerably simpler:
  1. day++;
  2.  
  3. // check to see if this is the last day of the month
  4. if (day > daysInMonth[month])
  5. {
  6. day = 1;
  7. month++;
  8.  
  9. // check to see if we're at the end of the year
  10. if (month > 12)
  11. {
  12. month = 1;
  13. year++;
  14. }
  15. }

You can apply a similar technique when printing out the names of days and months.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: ADT Calendar

 
0
  #3
Feb 10th, 2008
Your tests and adjustments for month rollover should be inside the month group if's, not as separate else if's.

You testing for day ranges should be day >=1, not strictly >1 (what happens when you try to advance from first of a month?)

Do you really need to test day value? If it's January, add a day, then test for month change.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

Re: ADT Calendar

 
0
  #4
Feb 10th, 2008
Thank you. I may try to do that.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

Re: ADT Calendar

 
0
  #5
Feb 10th, 2008
Originally Posted by vmanes View Post
Your tests and adjustments for month rollover should be inside the month group if's, not as separate else if's.

You testing for day ranges should be day >=1, not strictly >1 (what happens when you try to advance from first of a month?)

Do you really need to test day value? If it's January, add a day, then test for month change.
Well it's just that the other months aren't advancing properly...I'll try setting >=1; I should've seen that one...Regarding the rollovers being part of the ifs; I don't know how I can do that and keep the conditions? And I still don't get why the January works properly but the others don't
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 14
Reputation: TimeFractal is an unknown quantity at this point 
Solved Threads: 2
TimeFractal TimeFractal is offline Offline
Newbie Poster

Re: ADT Calendar

 
0
  #6
Feb 11th, 2008
  1. // Read the comments embedded in the code.
  2. // Some Advice. Read this carefully and understand it. Then and only then read the rest of the code.
  3. int primer_on_else_if()
  4. {
  5. if( day == 1 )
  6. // do this when day == 1
  7. do_something();
  8. else
  9. // do then when day != 1
  10. do_something();
  11.  
  12. // nested else if
  13. if( day == 1 )
  14. // do this when day == 1
  15. do_something();
  16. else if( day < 15 )
  17. // do this when day != 1 && day < 15
  18. do_something();
  19. else if( day == 29 )
  20. // do this when day != 1 && day >= 15 && day == 29
  21. do_something();
  22. else
  23. // do this when day != 1 && day >= 15 && day != 29
  24. do_something();
  25.  
  26. // see the pattern. for control to reach an 'if' attached to an 'else', the 'else' clause must first execute
  27.  
  28. // another way of writing the above would be
  29. if( day == 1 )
  30. // do this when day == 1
  31. do_something();
  32. else
  33. {
  34. if( day < 15 )
  35. // do this when day != 1 && day < 15
  36. do_something();
  37. else
  38. {
  39. if( day == 29 )
  40. // do this when day != 1 && day >= 15 && day == 29
  41. do_something();
  42. else
  43. {
  44. // do this when day != 1 && day >= 15 && day != 29
  45. do_something();
  46. } // else( day == 29 )
  47. } // else( day < 15 )
  48. } // else( day == 1 )
  49. }
  50.  
  51. #include "DATE.h"
  52. #include <iostream>
  53. using namespace std;
  54.  
  55. Date::Date():month(01),day(01),year(2008)
  56. {}
  57.  
  58. //---> instead of this
  59. #if 0
  60. Date::Date(int month1, int day1, int year1)
  61. {
  62. month = month1;
  63. day = day1;
  64. year = year1;
  65. }
  66. #endif
  67. //---> do this
  68. Date::Date(int m, int d, int y): month(m),day(d),year(y)
  69. {}
  70.  
  71. void Date::advance_date()
  72. {
  73. /*
  74.   * Year 2008 is the next leap year, with 29 days in February. A leap
  75.   * year is a year with one extra day inserted into February, the
  76.   * leap year is 366 days with 29 days in February as opposed to the
  77.   * normal 28 days.
  78.   */
  79. // on feb 29, 2008 advance_date() won't work
  80. // i suggest keeping things simple and ignoring leap years entirely
  81. if(month == 2 && day > 1 && day < 29)
  82. {
  83. day += 1;
  84. //month = month;
  85. }
  86. // this 'else if' does not do what you think it does
  87. // what happens when month = 1, day = 29, whoops....
  88. else if(day == 29)
  89. {
  90. day = 1;
  91. month += 1;
  92. }
  93. // ok yea, i'm writing an else_if tutorial, read that first
  94. else if((month == 4 || month == 6 || month == 9 || month == 11) && (day > 1 && day < 30))
  95. {
  96. day += 1;
  97. //month += 1;
  98. }
  99. // ok, snip rest of code... instead of what you have, please do this
  100.  
  101. // identify the number of days in this month
  102. int days_in_month;
  103.  
  104. // use branching if/else, or as joeprogrammer suggested use a data table
  105.  
  106. // increment day
  107.  
  108. // increment month if days overflow the number of days in month
  109.  
  110. // don't do this
  111. if( day > days_in_month )
  112. {
  113. day == 1;
  114. month += 1;
  115. }
  116. /*
  117.   * why? b/c you may need to add dates some day. for instance
  118.   * date a(1,1,2008);
  119.   * date b(2,1,2008);
  120.   * date c = a;
  121.   * c.add_days( days_between( a, b ) );
  122.   */
  123. // ok, on second thought add_days() requires a recursive call. we
  124. // don't need that complexity right now so lets keep to the KISS
  125. // principle and do this
  126. if( day > days_in_month )
  127. {
  128. day == 1;
  129. month += 1;
  130. }
  131.  
  132. }
  133.  
  134.  
  135. void Date::display_date()
  136. {
  137. cout << month << "-" << day << "-" << year << endl;
  138. }
  139.  
  140. void Date::name_date()
  141. {
  142. // read joe programmers suggestion. i would suggest a const char array
  143. char const * const [] month_name = { "jan", "feb", "mar", ... };
  144. // remember that c/c++ indexes arrays at base 0, so to access
  145. // month_name[ month-1 ] is "jan"
  146.  
  147. // yay!! if/else if used correctly here
  148. if(month == 1)
  149. {
  150. cout << "January " << day << ", " << year << endl;
  151. }
  152. else if(month == 2)
  153. {
  154. cout << "February " << day << ", " << year << endl;
  155. }
  156. // snip, good stuff, long, but correct
  157. }
  158.  
  159. // you don't need change_date(), why? copy constructor, like so
  160. // date a(1,1,2008);
  161. // now to change
  162. // a = Date(2,10,2009);
  163.  
  164. void Date::change_date(int month1, int day1, int year1)
  165. {
  166. day = day1;
  167. month = month1;
  168. year = year1;
  169. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: ADT Calendar

 
0
  #7
Feb 11th, 2008
Keeping it simple (no leap year handling)
  1. void Date::advance_date()
  2. {
  3.  
  4. day += 1;
  5. if(month == 2 && day > 28)
  6. {
  7. day = 1;
  8. month++;
  9. }
  10. else if((month == 4 || month == 6 || month == 9 || month == 11) && (day > 30))
  11. {
  12. day = 1;
  13. month++;
  14. }
  15. else if ( day > 31 ) //all other months
  16. {
  17. day = 1;
  18. month++;
  19. }
  20.  
  21. if( month > 12 )
  22. {
  23. month = 1;
  24. year++;
  25. }
  26.  
  27. }
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

Re: ADT Calendar

 
0
  #8
Feb 13th, 2008
Thank you for this lesson!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

Re: ADT Calendar

 
0
  #9
Feb 13th, 2008
Thank you for this intensive lesson!!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1038 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC