Simple Time/Date Program

Reply

Join Date: Oct 2004
Posts: 11
Reputation: tyalangan is an unknown quantity at this point 
Solved Threads: 0
tyalangan tyalangan is offline Offline
Newbie Poster

Simple Time/Date Program

 
0
  #1
Nov 2nd, 2004
Hello Everyone,

I've been desperatly trying to teach myself C and I've come along quite nicely. I wanted to start making my own programs (not from internet tutorial/Cd Rom).

I want to make a program that will display two dates that the user inputs then calculate the number of days between those two dates. Then once I do that I can go onto make days/weeks/months/years between the two dates. However, I don't really know how to start this problem. I've worked on it for a while but nothing is coming together. Could someone kindly show me how I would write this problem or at least tell me how to get started. I'm really looking forward to making this work! ^^ thx
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,954
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 917
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Simple Time/Date Program

 
0
  #2
Nov 2nd, 2004
Test drive this one! It's old and dusty, but works on Dev-C++ version 4.9.9.0
[php]// Find number of days between dates
// Turbo C converted to run on Dev-Cpp (vegaseat)

#include <iostream>
#include <stdlib.h>

using namespace std;

long days(int sm, int sd, int sy, int em, int ed, int ey);
long factor(int mm, int dd, int yy);

int main(int argc, char *argv[])
{
int sm, sd, sy; // starting date
int em, ed ,ey; // ending date
long old;

printf("\n Calculate days between two dates .....");
printf("\n\n Enter first date in mm/dd/yyyy format: ");
scanf("%d/%d/%d",&sm,&sd,&sy);
printf("\n Enter second date in mm/dd/yyyy format: ");
scanf("%d/%d/%d",&em,&ed,&ey);
old = days(sm,sd,sy,em,ed,ey);

printf("\n There are %ld days between %d/%d/%d and %d/%d/%d\n",
old,sm,sd,sy,em,ed,ey);

system("PAUSE");
return 0;
}

long days(int sm, int sd, int sy, int em, int ed, int ey)
{
long fac1, fac2, elap;

fac1 = factor(sm, sd, sy);
fac2 = factor(em, ed, ey);
elap = fac2 - fac1;
return (elap);
}

long factor(int mm, int dd, int yy)
{
long xx;

xx = 365 * yy + dd + 31 * (mm - 1);
if (mm < 3)
xx = xx + ((yy -1)/4) - (.75 * ((yy - 1)/100)+1);
else
xx = xx - (.4 * mm + 2.3) + (yy / 4) - (((.75 * (yy / 100) + 1)));
return (xx);
}
[/php]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: tyalangan is an unknown quantity at this point 
Solved Threads: 0
tyalangan tyalangan is offline Offline
Newbie Poster

Re: Simple Time/Date Program

 
0
  #3
Nov 4th, 2004
Thx a bunch! Some quick question though.

1. What are all the "long" functions for? Do I have to use "long"?
And what is the "old" refering to?
2. (just trying to understand this program) what are your "e"s and "s"s?
ex: em, ed, ey
3. What does elap stand for?
4. I understand this code for the most part but I only have a free unix system I got off the internet, can I use this code on that?

thx again
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Simple Time/Date Program

 
0
  #4
Nov 4th, 2004
>1. What are all the "long" functions for? Do I have to use "long"?
They're for the date and time calculations. You don't have to use long, but it helps to avoid integer overflow.

>2. (just trying to understand this program) what are your "e"s and "s"s?
Poor naming. s is a prefix meaning start (ex. sy == start year), and e is a prefix meaning end.

>3. What does elap stand for?
Time elapsed.

>can I use this code on that?
Provided it's fixed, yes. At present it relies on the implementation of iostream including stdio.h, not to mention also assuming that this code is C++ and not C. Replace <iostream> with <stdio.h> and remove "using namespace std;" and the code should compile under any C implementation.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: tyalangan is an unknown quantity at this point 
Solved Threads: 0
tyalangan tyalangan is offline Offline
Newbie Poster

Re: Simple Time/Date Program

 
0
  #5
Nov 8th, 2004
Works great guys, thx! This is pretty cool stuff ^^
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC