Solved

passing date as a parameter to a function

Posted on 1998-08-17
9
221 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ to C# code conversion issue 4 127
sorting efficency of sorting algorithm 30 147
Which Linux flavors will this run on? 6 112
maximize the sum of fractions 33 71
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 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