Jump to content

need help to troubleshoot

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
debug

debug

    Newbie

  • Members
  • PipPip
  • 17 posts
hi below is the code which i have come up with. when i enter a date in option a, the set function of the class should change the value of variable day to my input value. however, when i enter option b to return the date, the date remain the same as in the default constructor. can any one help me to check where the problem lies? thks
#include<iostream>

#include<fstream>


using namespace std;


class C

{

      public:

             C();

             void setDate(int&);

             int getDate();

             

             

      private:

              int day;

};


// Default constructor

C::C()

{

     // Initialise the variables

     day = 0;

}


void C::setDate(int& newDay)

{

     day = newDay;

}


int C::getDate()

{

     return day;

}



const void printMenu();

const void newDate(C&);

const void getDate(C);


const void dateMenu();


int main()

{

    printMenu();

        

    cout << endl;

    system("pause");

    return 0;

}



const void printMenu()

{

     cout << "********* My Date Utility Menu *********" << endl << endl;

     cout << "a) Set date" << endl;

     cout << "b) Return date" << endl;

     cout << "q) Quit" << endl;

     

     dateMenu();

}



const void dateMenu()

{

     char confirm, select;

     C date;

     

     cout << endl<< "Enter option: ";

     cin >> select;

           

     while(1)

     {            

           switch (tolower(select))

           {

                   case 'a': newDate(date);

                             break;

                             

                   case 'b': getDate(date);

                             break;

                             

                   case 'q': cout << endl << "Confirm to quit? (y = Yes, n = No): ";

                             cin >> confirm;

                                                                  

                             if (tolower(confirm) == 'y')

                             { 

                                  exit(0);

                             }

                      

                             else if (tolower(confirm) == 'n')

                             {

                                  cout << endl;

                                  printMenu();

                             }

                                     

                             else

                             {

                                  cout << endl << "Invalid input!!" << endl <<endl;

                                  system("pause");

                                  cout << endl;

                                  printMenu();

                             }

                             

                             break;

                             

                   default : cout << endl << "Invalid input!!" << endl << endl;

                             system("pause");

                             exit(0);

                                                        

            }

     }

}  

const void newDate(C& date)

{ 

    int d=0;

    int toYear = 0;

    //string y = date.getYear();

    string m;

    string y;

    

    cout << endl << "Enter day (between 1 & 31): ";

    cin >> d;

    

    date.setDate(d); 

                   

    printMenu();

              

    system("pause");

}



const void getDate(C date)

{

    cout << date.getDate();

    system("pause");

    cout << endl;

    printMenu();

} 



#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
You're doing pass-by-value with your functions.

#3
debug

debug

    Newbie

  • Members
  • PipPip
  • 17 posts
u mean the class function or the function for the main?