943,981 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 16893
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 26th, 2007
1

Program- calculate # of days between two days

Expand Post »
"Develop a program that can be used to calculate the number of days between any two days from 1/1/1900 through 12/31/2099

The main program will call the same function twice- once for each date and have an integer returned to it each time.

The function will prompt for a year, month and day(in that seqeunce)

After receiving both dates, the program will then determine the number of days between the two dates.

The normal year array should contain:
C++ Syntax (Toggle Plain Text)
  1. {0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 304, 335, 366};

any help is much appreciated
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
youngone is offline Offline
8 posts
since Feb 2007
Feb 26th, 2007
0

Re: Program- calculate # of days between two days

We'd love to help you, but you must first read this. Then post your latest attempt, encased within code tags (and tell us what's not working so we don't have to guess).

Thanks
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 27th, 2007
0

Re: Program- calculate # of days between two days

I dunno, maybe actually try it for yourself rather than wandering from board to board in the hope that someone will spoon-feed you the answer.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Feb 28th, 2007
0

Re: Program- calculate # of days between two days

I'm not looking to be spoon fed an answers
i'm looking for help!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
youngone is offline Offline
8 posts
since Feb 2007
Feb 28th, 2007
0

Re: Program- calculate # of days between two days

>I'm not looking to be spoon fed an answers
Then show that you're willing to try. Post your latest coding attempt at it.

>i'm looking for help!!!
We'll help you once you post what you've already tried.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 28th, 2007
0

Re: Program- calculate # of days between two days

OK , but how do i paste my code into this message box?
it's not giving me the option
Reputation Points: 10
Solved Threads: 0
Newbie Poster
youngone is offline Offline
8 posts
since Feb 2007
Feb 28th, 2007
0

Re: Program- calculate # of days between two days

>OK , but how do i paste my code into this message box?
  1. First of all type your message text
  2. Type [code]
  3. Insert some spaces, then type [/code]
  4. Paste your code in between the space that you made between the code tags
I'm assuming of course, that you know how to use your clipboard to copy items.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 28th, 2007
0

Re: Program- calculate # of days between two days

Ok here is the code

I am new to the array concept, i know how to initialize them but that is it
How would i use the below arrays to add the days together

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. int main(void)
  6. {
  7. int month = 0;
  8. int month2 = 0
  9. int day = 0;
  10. int day2 = 0;
  11. int year = 0;
  12. int year2 = 0;
  13. numofdays = 0;
  14. julianDate = 0;
  15.  
  16. const int DaysInMonth[13] = {0,31,28,31,30,31,30,31,31.30,31,30,31};
  17. const int DaysInYear[13] = {0,0,31,59,90,120,151,181,212,243,273,304,334,365};
  18. const int DaysInLeapYear[13] = {0,0,31,60,91,121,152,182,213,244,274,305,335,366};
  19.  
  20. cout << "\n Calculates # of days between dates" << endl;
  21. cout << "\n give a date (ex: 2 28 2007):"<< endl;
  22. cin >> month >> day >> year;
  23.  
  24. if (month < 0 || month > 12 || day < 0 || day >31 || year > 1900 ||| year < 2099 )
  25. {
  26. cout << "One of the numbers you entered is incorrect\n";
  27. cout << "try entering it again (ex: 2 28 2007): ";
  28. cin >> month >> day >> year;
  29. }
  30.  
  31. cout << "\n give another date" << endl;
  32. cin >> month2 >> day2 >> year2 << endl;
  33.  
  34. if (month2 < 0 || month2 > 12 || day2 < 0 || day2 >31 || year2 > 1900 ||| year2 < 2099 )
  35. {
  36. cout << "One of the numbers you entered is incorrect\n";
  37. cout << "Try entering it again (ex: 2 28 2007): ";
  38. cin >> month2 >> day2 >> year2;
  39. }
  40.  
  41. juliandate =
  42.  
  43.  
  44.  
  45. cout << month << day << year << endl;
  46. cout << month2 << day2 << year2 << endl;
  47. cout << "# of days between these dates is: " << numofdays;
  48.  
  49. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
youngone is offline Offline
8 posts
since Feb 2007
Feb 28th, 2007
0

Re: Program- calculate # of days between two days

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <math.h>
  3. #include <stdlib.h>
Alright - so which language are you trying to write in: C or C++? stdlib.h is a C header, and if you really need to use C functions (which I can't seem to find any in your code below) then you can use include <cstdlib> instead.

Since when are there 13 months in a year? And why are you keeping track of the number of days in each year; all you need is a second array for leap year.

Then you can calculate which years are leap years by using a starting year that you know is a leap year (drat, I can't remember when was our last leap year) and then comparing it to the year entered by the user. Hint: you'll need the % (modulos) operator.

The rest should be fairly easy; all you need to do is subtract years and multiply it by the number of days in the year you found earlier, multiply the difference of months by looking up the month array, and then adding the remainig days (if that made any sense at all).
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 1st, 2007
0

Re: Program- calculate # of days between two days

Oops, i don't know why I put 13 in that array

Code i am using is C++

Last night was finally able to get the julian date formula from my instructor is it as follows:
C++ Syntax (Toggle Plain Text)
  1. jdate = year - 1900;
  2. jdate = year * 365 +((year-1)/4) + DaysInYear[month] + day

or for leap year:
C++ Syntax (Toggle Plain Text)
  1. jdate = year - 1900;
  2. jdate = year * 365 +((year-1)/4) + DaysInLeapYear[month] + day

so later tonight I'm going to add this to my assignment and hopefully it compile just fine, ending this assignemtn and it's important to since I have now fallen behind the rest of the class
Reputation Points: 10
Solved Threads: 0
Newbie Poster
youngone is offline Offline
8 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Is this situation a 'bad' use of the precompiler
Next Thread in C++ Forum Timeline: Calculator project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC