Solved

passing date as a parameter to a function

Posted on 1998-08-17
9
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 3

Expert Comment

by:Norbert
ID: 1170465
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
ID: 1170466
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
ID: 1170467
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Norbert
ID: 1170468
And now all is clear because you have accepted the answer ?
0
 

Author Comment

by:ardney
ID: 1170469
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
ID: 1170470
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
ID: 1170471
I am posting another question. please see it.
bye and thanx
ardney
0
 
LVL 3

Expert Comment

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

Author Comment

by:ardney
ID: 1170473
I justed posted the question

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

729 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