Solved

passing date as a parameter to a function

Posted on 1998-08-17
9
216 Views
Last Modified: 2010-04-01
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  
0
Comment
Question by:ardney
  • 4
  • 4
9 Comments
 
LVL 3

Expert Comment

by:Norbert
Comment Utility
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
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
Comment Utility
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
 

Author Comment

by:ardney
Comment Utility
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
 
LVL 3

Expert Comment

by:Norbert
Comment Utility
And now all is clear because you have accepted the answer ?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:ardney
Comment Utility
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
 
LVL 3

Expert Comment

by:Norbert
Comment Utility
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
 

Author Comment

by:ardney
Comment Utility
I am posting another question. please see it.
bye and thanx
ardney
0
 
LVL 3

Expert Comment

by:Norbert
Comment Utility
I am looking for the new question but until now I cant find it
0
 

Author Comment

by:ardney
Comment Utility
I justed posted the question

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now