943,682 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 469
  • C++ RSS
Nov 9th, 2008
0

Problem with constructor

Expand Post »
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 6
Solved Threads: 0
Light Poster
AcidG3rm5 is offline Offline
25 posts
since Oct 2008
Nov 9th, 2008
0

Re: Problem with constructor

Your program does not include <string> for std::string class, so how in the world are you getting it to compile????? Don't attempt to execute a program that contains compiler errors or warnings. Yes, warnings in your code are usually errors too.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,949 posts
since Aug 2005
Nov 9th, 2008
0

Re: Problem with constructor

C++ Syntax (Toggle Plain Text)
  1. void setDateMenu(Date myDate);
You pass Date by value so set a new date to Date copy. The original object is intact.
Pass by reference:
C++ Syntax (Toggle Plain Text)
  1. void setDateMenu(Date& myDate);
Common error...
I hope now you know that argument passed by value in C and C++ ...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 9th, 2008
0

Re: Problem with constructor

Thanks for the help. i made a reference variable to myDate and it worked.
Reputation Points: 6
Solved Threads: 0
Light Poster
AcidG3rm5 is offline Offline
25 posts
since Oct 2008
Nov 9th, 2008
0

Re: Problem with constructor

Yes, rather! It would be surprising if it didn't work
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: double data type
Next Thread in C++ Forum Timeline: c++ tutorials





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC