#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?

Recommended Answers

All 4 Replies

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.

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:

void setDateMenu(Date& myDate);

Common error...
I hope now you know that argument passed by value in C and C++ ;)...

Thanks for the help. i made a reference variable to myDate and it worked.

Yes, rather! It would be surprising if it didn't work ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.