C++ Question

This is a discussion on C++ Question within the C++ Programming forums, part of the General Programming Boards category; Can anyone give pointers on how to write a funtion that would return the later of any two dates passed ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    12

    Question C++ Question

    Can anyone give pointers on how to write a funtion that would return the later of any two dates passed to it?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    First compare the years. If they're equal, then compare the months. If those are equal, then compare the days. Exactly how you do this depends on the date format.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    what is a 'date' specificially? what have you tried? where are you stuck? please post code. if you havent attempted this then post as much C++ code mixed with pseudo-code (english version of programming code and syntax) as you can and then we have a place to start.

    good luck
    Last edited by nadroj; 10-19-2006 at 11:37 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    12
    Code:
    going to just show you what I have so far...
    
    struct 
    {
    int month;
    int day;
    int year;
    } Date1, Date2;
    
    int larger(Date1[], Date2[]);  //function name
    
    int main()
    {
     Date1 = {10,9,2005};
     Date2 = {11,3,2005};
     
        Date.year*10000
        Date.month*100 
        Date.day
    
    I really just have no clue where to put what...}

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    can you even do this? havent _used_ C++ indepth in a while but that doesnt look like it would even compile (of course its likely im wrong).

    id change this code to:

    Code:
    struct Date
    {
    int month;
    int day;
    int year;
    };
    
    Date& larger(Date date1, Date date2);  //function name
    
    int main()
    {
    Date date1;
    Date date2;
    
    //use the . operator to access the int variables of date1 and date2 to set the values, ie: variableName.member = "just an example";
    
    // what are you trying to do here?
        Date.year*10000
        Date.month*100 
        Date.day
    ...
    now, as robatino said, implement your larger function with the new signature as i have layed out (if you want) and use the logic he said. post back code and errors if need be.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    12
    I was told to do this to find the "later" date that was input

    // what are you trying to do here?
    Date.year*10000
    Date.month*100
    Date.day

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by scipitam
    I was told to do this to find the "later" date that was input

    // what are you trying to do here?
    Date.year*10000
    Date.month*100
    Date.day
    These "statements" don't do anything, thus the question, "what are you trying to do here?" Are you trying to add the results together into some kind of sum? ...
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    12
    ok, well let me just scrap that...I have to say, I am totally new to C++ and am really finding this overwhelming. I am trying to return the later of any 2 dates passed to a function named larger()

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok, we are aware of this.

    lets try and continue with the function prototype i have shown (or someone else share if theres a better one): Date& larger(Date date1, Date date2);

    so now we must implement this function and add code in its body so it will do what you want. as i have said in an earlier post, write pseudocode (simply english sentences, lets say), to model what you are trying to do. ill give an example.

    Code:
    function StartCar()
    {
       -put key in ignition
       -turn key
       -if (theres enough gas and there are no mechanical faults)
             engine will start
    }
    now lets transform this trivial example from pseudocode to real code:
    Code:
    void startCar()
    {
         ignition.insertKey();
         ignition.turnKey();
         if (gasLevel > 0 && engine.isGood() )
               engine.start();
    }
    i hope i get somewhere with that, sorry if its a dumb example which doesnt make sense.

    now, having your code:
    Code:
    Date& larger(Date date1, Date date2)
    {
          // ....
    }
    simply write english terms and phrases to layout what you want to do where the // .... is. tip: see post 2. if you dont know how to work with structures search somewhere for a c++ tutorial on it, they really arent hard.

    good luck

    edit: heres a quick link: http://www.cplusplus.com/doc/tutorial/structures.html
    Last edited by nadroj; 10-20-2006 at 06:50 PM.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Date.year*10000
    Date.month*100
    Date.day
    I can see some idea behind this. It looks like an attempt to add up year, month and day, so you get a comparable value.

    If I had to come up with something like that just to compare if one date is larger than the other (but don't need to know by how much), I'd try something like that: x = day+month*31+year*31*12
    This of course assumes all months are 31 days for simplicity, making each year 372 days long. This makes the dates comparable, but if I needed something finer, I'd probably study date and time functions.

    (If the world didn't end before, eventually at some point a year would be 372 days long. I wish it would happen sooner

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    12
    Why would I be getting the error "expected primary-expression before '{' token" ?

    Code:
    struct Date
    {
     int month;
     int day;
     int year;
     };
     
     int larger (Date);
     Date num1, num2;
     
     int main()
     {
     
     num1 = {10,9,2005};  //this is where the error is coming up
     num2 = {11,3,2005};  //here too
    
     
     if ((num1.year * 10000 + num1.month * 100 + num1.day) > 
         (num2.year * 10000 + num2.month * 100 + num2.day))
    	 cout<< "num1 is bigger " << endl;
    	 
       else
       cout << "num2 is bigger "<< endl;	 
     
    
    return 0;
    
     }

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by scipitam
    Why would I be getting the error "expected primary-expression before '{' token" ?

    Code:
     num1 = {10,9,2005};  //this is where the error is coming up
     num2 = {11,3,2005};  //here too
    Because the syntax is not valid. Initialization and assignment are not the same.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    please see post #9 for the link, and look at the examples.

    we take the time to post tips and links to help you. take the time to read it.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    12
    Thanks for your help..

  15. #15
    MB1
    MB1 is offline
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Date.year*10000
    Date.month*100
    Date.day
    You've got the right idea here. You have to change the gregorian date format of mm/dd/yyyy to an integer (julian day) so that you can do your calculations. Once you get your answer, you have to change it back to the gregorian date format. The formula will take into account leap years and various days in a month. I would think that you're instructor would give you the algorithm or code for this. You want Volume 6, No. 8, p. 444 I don't know if they charge for a student membership. Once you get the format changed, the rest is much easier.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21