| | |
Problem with constructor
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 16
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string.h> using namespace std; class Date { public : Date(); void setDate(int,string,int); void printDate(); private : int day; string mth; int year; };//end class //date constructor Date::Date() { day = 01; mth = "Jan"; year = 1990; } //setDate void Date::setDate(int inDay,string inMth, int inYear) { day = inDay; mth = inMth; year = inYear; cout<<"Date set to "<<day<<"-"<<mth<<"-"<<year<<endl; }//end setDate //printDate void Date::printDate() { cout<<day<<"-"<<mth<<"-"<<year<<endl; }//end print date //functions declaration void menu(); void setDateMenu(Date myDate); int main() { char choice; //show menu menu(); //new a date object Date myDate; //read in the choice cin>>choice; //change it to a lower case. choice = tolower(choice); while (1) { switch (choice) { case 'a' : setDateMenu(myDate); //myDate.setDate(10,"Jan",2007); system("pause"); system("CLS"); break; case 'b' : myDate.printDate(); system("pause"); system("CLS"); break; case 'q' : cout<<"Exiting system. Thank you for using calendar system."<<endl; system("pause"); exit(0); break; default : cout<<"Please enter A to H only. Q to quit."<<endl; system("pause"); system("CLS"); break; }//end switch cout << endl; menu(); cin >> choice; choice = tolower(choice); }//end while system("pause"); }//end main void menu() { cout<<"A)\tSet Date"<<endl; cout<<"B)\tReturn Date"<<endl; cout<<"C)\tReturn number of days in month"<<endl; cout<<"D)\tReturn number of days passed in a year"<<endl; cout<<"E)\tCheck if leap year"<<endl; cout<<"F)\tReturn which day of week"<<endl; cout<<"G)\tAdd days to date"<<endl; cout<<"H)\tPrint month calendar"<<endl; cout<<"Q)\tQuit"<<endl; cout<<endl; cout<<"Please enter choice: "<<endl; }//end menu //setDate void setDateMenu(Date myDate) { //bool year = false; int inDay; string inMth; int inYear; cout<<"Enter Day: "<<endl; cin>>inDay; cout<<"Enter Month: "<<endl; cin>>inMth; cout<<"Enter Year: "<<endl; cin>>inYear; for(int i=0;i<inMth.length();i++) {//convert all the input for month to lower case tolower(inMth[i]); }//end for loop myDate.setDate(inDay,inMth,inYear); }//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.
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.
C++ Syntax (Toggle Plain Text)
void setDateMenu(Date myDate);
Pass by reference:
C++ Syntax (Toggle Plain Text)
void setDateMenu(Date& myDate);
I hope now you know that argument passed by value in C and C++
... ![]() |
Similar Threads
- Constructor overloading question (C++)
- Class Template Problem - Constructor Issues - Please help (C++)
- what is the wrong with Byte constructor which takes int value (Java)
- Problem updating a JLabel?? (Java)
- problem in constructing two dimensional array in c++ using classes (C++)
- Calling SocketServer's constructor (C++)
- Constructor overloading problem (VB.NET)
- Problem converting Selection Sort to Class (C++)
- copy constructor problem (C++)
Other Threads in the C++ Forum
- Previous Thread: double data type
- Next Thread: c++ tutorials
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






