passing date as a parameter to a function

I want to search a file in which some dates exist.So how can I pass two parameters like FromDate and ToDate to my function.
The date format is yyyy/mm/dd  
ardneyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Answers2000Connect With a Mentor Commented:
You need a date class.  If you don't already have one (see below) the best thing to do would be to make a date class along the lines of

class MyDate
{
private:
   int nYear ;
   int nMonth ;
   int nDay ;

public:
   MyDate() // default constructor
   {
        nYear = 0 ;
        nMonth = 0 ;
        nDay = 0 ;
   }  

   MyDate( int year, int month, int day ) // another constructor
   {
        nYear = year ;
        nMonth = month ;
        nDay = day ;
   }  

   MyDate( const MyDate& rDate ) // copy constructor
   {
        nYear = rDate.nYear ;
        nMonth = rDate.nMonth ;
        nDay = rDate.nDay ;
   }

   void SetYear( int year )
   {
       nYear = year ;  
   }

   int GetYear() const
   {
       return nYear ;
   }

   void SetMonth( int month )
   {
       if ( ( month >= 1 ) && ( month <= 12 ) )
       {
            nMonth = month ;
       }
   }

   // etc add more functions as necessary for example to
   // convert dates to & from strings, fill in the date with
   // today's date etc., operator=, >, <, <=, >=, != and so
   // forth.  For example operator= would look like this
   MyDate& operator=( const MyDate& rDate )
   {
       if ( (&rDate) != this )
       {
           nYear = rDate.nYear ;
           nMonth = rDate.nMonth ;
           nDay = rDate.nDay ;
       }
       return *this ;
   }  

   // and for you I'll include an output function
   void OutputDate()
   {
        cout << nYear << "/" << nMonth << "/" << nDay ;
        cout << " " ; // add a space for the heck of it
   }
   
} ;

If you don't want to create your own date class, consider using the one that came with the compiler library (depends on which compiler, for example Microsoft Foundation Classes has the excellent COleDateTime class)

To pass these to functions you can pass them into parameters several ways :-

1. Pass entire object (you need to include class members to make the class member copy-able)

void SomeFunction( MyDate datefrom, MyDate dateto )
{
    datefrom.Output() ;
    dateto.Output() ;
}

sample calling code :-

MyDate today( 1998, 8, 17 ) ;
MyDate xmas( 1998, 12, 25 ) ;
SomeFunction( today, xmas ) ;

2. Pass by reference, use const reference if the function is not supposed to change the value of variable.  For example I'll make datefrom a const reference parameter, and dateto a non-const parameter

void SomeFunction( const MyDate& datefrom, MyDate& dateto )
{
    datefrom.Output() ;
    dateto.Output() ;

    // dateto can be changed in this function and will affect
    // the caller's value.  datefrom can not be changed (will
    // not compile if you try), for example
    dateto.SetDay( 31 ) ;
}

sample calling code :-

MyDate today( 1998, 8, 17 ) ;
MyDate xmas( 1998, 12, 25 ) ;
SomeFunction( today, xmas ) ; // modifies xmas
xmas.Output() ; // will contain 31 December 1998


3. Pass by pointer, use const pointers if the function is not supposed to change the value of variable.  For example I'll make datefrom a const reference parameter, and dateto a non-const parameter.  In a lot of ways this is very similar to #2 but syntax is a bit different.

void SomeFunction( const MyDate * datefrom, MyDate * dateto )
{
    datefrom->Output() ;
    dateto->Output() ;

    // dateto can be changed in this function and will affect
    // the caller's value.  datefrom can not be changed (will
    // not compile if you try), for example
    dateto->SetDay( 31 ) ;
}

sample calling code :-

MyDate today( 1998, 8, 17 ) ;
MyDate xmas( 1998, 12, 25 ) ;
SomeFunction( &today, &xmas ) ; // modifies xmas
xmas.Output() ; // will contain 31 December 1998


One final note: I'd recommend #2 unless there are good reasons not to use this method.  This is because the syntax is simplest.

#1 wastes a lot of allocation time/effort as you end up making temporary copies of the input parameters

#2 Syntax is very similar to #1 so easy to cut'n'paste code around for example between body of a function and the code that calls it.

#3 syntax is more complex.  You will note syntax when operating with a pointer (inside function) is significantly different to when operating without pointer (sample calling code) - which makes it more difficult to move code around.  Only use if you really need to use pointers (which is a big topic in itself).
0
 
NorbertCommented:
Your question is not clear for me.
What do you wish?
if The Date format is absolutely fix a

char Begin[11];
char End[11];
strcpy(Begin,"1998/01/01");
strcpy(End,"1998/08/17");
would be enough to pass it to a Function like
Search(char* FromDate,char* ToDate)
{



}
the call to pass it would be
Serach(Begin,End);
but I bet that is not the problem you have.
0
 
ardneyAuthor Commented:
What I require is the user will be entering From Date in a text Box in the user interface and similarly a ToDate in the other TextBox and says OK. I ahve to search a file in which many lines like
1,FromDate,ToDate\n
1,FromDate,ToDate\n
2,FromDate,ToDate\n
3,FromDate,ToDate\n
4,FromDate,ToDate\n
7,FromDate,ToDate\n
etc.,
I have to count all the entries between those dates.
For a file with above lines I have to return 6 as the number of entries between those dates is 6
So That is what I want

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
NorbertCommented:
And now all is clear because you have accepted the answer ?
0
 
ardneyAuthor Commented:
No,
I have not accepted the answer. Please see the comment by me above and answer it. I could not get what u told.
0
 
NorbertCommented:
I guessed that you did not want accept the answer but you have  !!! May be you can ask linda to give you back the points because your accept was an error. And than ask the question again.
But back to your question: It is abolutely not clear to me what you  need: do you need the function to read the file?
Do you need a function to build the date from the user input?
In your question you asked how to pass 2 parameters and both My comment and Answer2000's answer are correct but I guess that you not need to know how to pass - the simplest is as string because in your file you have it also as string - I belive you need to know how to search for the dates inside the file but again it is not clear to me what you need
0
 
ardneyAuthor Commented:
I am posting another question. please see it.
bye and thanx
ardney
0
 
NorbertCommented:
I am looking for the new question but until now I cant find it
0
 
ardneyAuthor Commented:
I justed posted the question

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.