Can anyone give pointers on how to write a funtion that would return the later of any two dates passed to it?
Can anyone give pointers on how to write a funtion that would return the later of any two dates passed to it?
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.
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.
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...}
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:
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.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 ...
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? ...Originally Posted by scipitam
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.*
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()
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.
now lets transform this trivial example from pseudocode to real code:Code:function StartCar() { -put key in ignition -turn key -if (theres enough gas and there are no mechanical faults) engine will start }
i hope i get somewhere with that, sorry if its a dumb example which doesnt make sense.Code:void startCar() { ignition.insertKey(); ignition.turnKey(); if (gasLevel > 0 && engine.isGood() ) engine.start(); }
now, having your code: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.Code:Date& larger(Date date1, Date date2) { // .... }
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.
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.Date.year*10000
Date.month*100
Date.day
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
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; }
Because the syntax is not valid. Initialization and assignment are not the same.Originally Posted by scipitam
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.*
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.
Thanks for your help..
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.Date.year*10000
Date.month*100
Date.day