View Single Post
Join Date: Oct 2008
Posts: 16
Reputation: AcidG3rm5 has a little shameless behaviour in the past 
Solved Threads: 0
AcidG3rm5 AcidG3rm5 is offline Offline
Newbie Poster

Problem with constructor

 
0
  #1
Nov 9th, 2008
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. class Date
  6. {
  7. public : Date();
  8. void setDate(int,string,int);
  9. void printDate();
  10.  
  11. private : int day;
  12. string mth;
  13. int year;
  14.  
  15. };//end class
  16.  
  17.  
  18. //date constructor
  19. Date::Date()
  20. {
  21. day = 01;
  22. mth = "Jan";
  23. year = 1990;
  24. }
  25.  
  26. //setDate
  27. void Date::setDate(int inDay,string inMth, int inYear)
  28. {
  29. day = inDay;
  30. mth = inMth;
  31. year = inYear;
  32. cout<<"Date set to "<<day<<"-"<<mth<<"-"<<year<<endl;
  33. }//end setDate
  34.  
  35. //printDate
  36. void Date::printDate()
  37. {
  38. cout<<day<<"-"<<mth<<"-"<<year<<endl;
  39. }//end print date
  40.  
  41. //functions declaration
  42. void menu();
  43. void setDateMenu(Date myDate);
  44.  
  45. int main()
  46. {
  47. char choice;
  48. //show menu
  49. menu();
  50. //new a date object
  51. Date myDate;
  52.  
  53. //read in the choice
  54. cin>>choice;
  55. //change it to a lower case.
  56. choice = tolower(choice);
  57.  
  58. while (1)
  59. {
  60. switch (choice)
  61. {
  62. case 'a' : setDateMenu(myDate);
  63. //myDate.setDate(10,"Jan",2007);
  64. system("pause");
  65. system("CLS");
  66. break;
  67.  
  68. case 'b' : myDate.printDate();
  69. system("pause");
  70. system("CLS");
  71. break;
  72.  
  73. case 'q' : cout<<"Exiting system. Thank you for using calendar system."<<endl;
  74. system("pause");
  75. exit(0);
  76. break;
  77.  
  78. default : cout<<"Please enter A to H only. Q to quit."<<endl;
  79. system("pause");
  80. system("CLS");
  81. break;
  82. }//end switch
  83. cout << endl;
  84. menu();
  85. cin >> choice;
  86. choice = tolower(choice);
  87. }//end while
  88.  
  89.  
  90. system("pause");
  91. }//end main
  92.  
  93. void menu()
  94. {
  95. cout<<"A)\tSet Date"<<endl;
  96. cout<<"B)\tReturn Date"<<endl;
  97. cout<<"C)\tReturn number of days in month"<<endl;
  98. cout<<"D)\tReturn number of days passed in a year"<<endl;
  99. cout<<"E)\tCheck if leap year"<<endl;
  100. cout<<"F)\tReturn which day of week"<<endl;
  101. cout<<"G)\tAdd days to date"<<endl;
  102. cout<<"H)\tPrint month calendar"<<endl;
  103. cout<<"Q)\tQuit"<<endl;
  104. cout<<endl;
  105. cout<<"Please enter choice: "<<endl;
  106. }//end menu
  107.  
  108. //setDate
  109. void setDateMenu(Date myDate)
  110. {
  111. //bool year = false;
  112. int inDay;
  113. string inMth;
  114. int inYear;
  115.  
  116. cout<<"Enter Day: "<<endl;
  117. cin>>inDay;
  118.  
  119. cout<<"Enter Month: "<<endl;
  120. cin>>inMth;
  121.  
  122. cout<<"Enter Year: "<<endl;
  123. cin>>inYear;
  124.  
  125. for(int i=0;i<inMth.length();i++)
  126. {//convert all the input for month to lower case
  127. tolower(inMth[i]);
  128. }//end for loop
  129.  
  130. myDate.setDate(inDay,inMth,inYear);
  131. }//end setDateMenu

I have problem doing a set date for the above codes. every time i invoked the setDateMenu(Date myDate) to set a new date to my object myDate, it manages to take in the correct day,mth and year (I did a cout at void Date::setDate(int inDay,string inMth, int inYear) It shows the new day,mth and year set.

However, when i did a print date (option B), the new date values are still not saved into the dates and it still prints the default date value which is 01-Jan-1990.

Anyone got any idea where the codes went wrong and why the new values of the day,mth and year aren't passed into the object?
Last edited by AcidG3rm5; Nov 9th, 2008 at 12:26 pm.
Reply With Quote