Problem with constructor

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

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 Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem with constructor

 
0
  #2
Nov 9th, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem with constructor

 
0
  #3
Nov 9th, 2008
  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:
  1. void setDateMenu(Date& myDate);
Common error...
I hope now you know that argument passed by value in C and C++ ...
Reply With Quote Quick reply to this message  
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

Re: Problem with constructor

 
0
  #4
Nov 9th, 2008
Thanks for the help. i made a reference variable to myDate and it worked.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem with constructor

 
0
  #5
Nov 9th, 2008
Yes, rather! It would be surprising if it didn't work
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC