a time.h problem

This is a discussion on a time.h problem within the C Programming forums, part of the General Programming Boards category; I am trying to calculate how many seconds elapsed since a month/year inputed by the user I want to use ...

  1. #1
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    a time.h problem

    I am trying to calculate how many seconds elapsed since a month/year inputed by the user

    I want to use time.h only for this exercise!!!

    I also have to account for century leap year as defined in #define

    This is my codes so far...don't know how to make it work

    Any suggestions!!!

    Code:
    #include <stdio.h>
    #include <time.h>
    
    #define LEAP(yy) ((yy)%400==0) 
    
    int main(void)
    {
    	char *line;
    	int mm,dd,yyyy;
    		
    	printf("enter date:");
    	gets(line);
    
    	elapsed= double difftime(time_t t1,time_t t0);
    
    	t0=current date & time;
    	t1=line;	
    	
    	printf("%3.1lf %s\n", elapsed, "seconds");
    
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Where to start...

    1) This is a pointer. It does not contain any actual allocated memory:
    char *line;

    2) Never ever do this:
    gets(line);

    You should never use 'gets'. If you must read a line, use fgets instead.

    3) You don't have a variable called 'elapsed', so don't start using one:
    elapsed= double difftime(time_t t1,time_t t0);

    The same goes for other variables in your program.

    Quzah.
    Last edited by quzah; 10-14-2002 at 06:41 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    why not use gets??

    updated codes:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    #define LEAP(yy) ((yy)%400==0) 
    
    int main(void)
    {
    	char line;
    	int elapsed,t1,t0;
    		
    	printf("enter date:");
    	fgets(line,sizeof line,stdin);
    
    	elapsed= double difftime(time_t t1,time_t t0);
    
    	t0=current date & time;
    	t1=line;	
    	
    	printf("%3.1lf %s\n", elapsed, "seconds");
    
    	return 0;
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >why not use gets??
    No buffer overflow checking.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're using fgets incorrectly. The reason is you only have ONE CHARACTER, not an array:

    char line;

    The reason you never want to use gets is because gets doesn't check boundries. Thus, if you have an array of 5 characters that you want to read into, and I type 5000 characters on a single line (because I have bumped up the size of my keyboard buffer or have redirected standard input [ie: ignore this since it will hold no meaning to you]), it will blow my program to tiny little pieces, and likely do Bad Things(TM) to the stability of my operating system as a side effect.

    [edit]Curses! Foiled again![/edit]

    Quah.
    Hope is the first step on the road to disappointment.

  6. #6
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    I will use scanf then.......

    so what about my problem now?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Max
    I will use scanf then.......

    so what about my problem now?
    Your problems are that you have no idea what the hell you're doing. For example, what on earth is this supposed to do:


    t0=current date & time;
    t1=line;


    "current date & time" ?? What? You can't do that. First off, you have no 'current' no 'date' and no 'time' variables. Second, that line is horribly inaccurate even if you did have them.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    t0 & t1 are variables from the difftime() in time.h

    I want to make t1 the date of the user input and t0 the current date but i did not write codes for it I don't know how so I put a comment...sorry!! this was not meant to be a line of code

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok. Here are a few things to keep in mind:

    All variables must be declared before any other lines of code in the funtion. You cannot use create variables "inline" like you can in C++.
    Code:
    /* Valid C code. */
    int x;
    for( x = 0; x < 10; x++ )
        printf("X is %d.\n", x );
    See, here you declare 'x' before you ever use it.

    Code:
    /* Valid C++ code. Not valid in C. */
    for( int x = 0; x < 10; x++ )
        printf("X is %d.\n", x );
    Here, we declare the variable 'x' in the for( ) loop. This is invalid in C. The same goes for your function:

    elapsed= double difftime(time_t t1,time_t t0);

    You cannot declare those variables at the time of the function call. Create those two variables up with the rest of your variables.

    And to add comments do your code, do:

    /*
    Your comment here.
    */

    The /* starts a comment.
    The */ ends a comment.
    They must be used in pairs.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    ok...thanks quzah

    Code:
    #include <stdio.h>
    #include <time.h>
    
    #define LEAP(yy) ((yy)%400==0) 
    
    int main(void)
    {
    	int line;
    	int elapsed;
    	double t1,t0;
    		
    	printf("enter date:");
    	scanf("%d",line);
    
    	difftime(time_t t1,time_t t0);
    
    	t0=                 //current date & time;
    	t1=line;	
    	
    	printf("%3.1lf %s\n", elapsed, "seconds");
    
    	return 0;
    }

  11. #11
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    I have updated codes...I think I am getting close!!!

    Code:
    #include <stdio.h>
    #include <time.h>
    
    #define LEAP(yy) ((yy)%400==0) 
    
    int main(void)
    {
    	int mm,yy,mmyy,now,date;
    	double elapsed;
    	time_t t1,t0;
    
    	printf("enter date:");
    
    	mmyy=scanf("%d/%d",&mm,&yy);  
    
    	if (mmyy==2) printf("%ld/%ld\n",mm,yy);     
    		else 
    			{
    			printf("date mm/yy not found\n"); exit(1);
    			}
    
    	if (mm>12)             
    		{
    		printf("invalid date\n"); exit(1);
    		}
    
    		
                        now=time(NULL);
    	date=mmyy;	
    
    	elapsed=difftime(now,date);
    
    	
    	printf("%ld seconds\n",elapsed);
    
    	return 0;
    }
    Last edited by Max; 10-14-2002 at 10:13 PM.

  12. #12
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Need help with this program...how can i make it work!!!

    I am trying to calculate time in seconds passed since the date input it by user. I want to use time.h for this!!

  13. #13
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    does not help me much!!!!!

    in my program the user is unputing a date in the following format: mm/yy ex: 12/02

    My program runs but it gives me 0 seconds which I know is wrong

    I was wondering whether the difftime() function can read the user date format or should I have to concert it in a different format understandable by teh function....if so....how do i do that....and what is the correct format?

    Thanks

  14. #14
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Can't find mktime() in my textbook....

    How do i use mktime()?

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by Max
    why not use gets??
    I will use scanf then.......
    Why does everyone say not to use gets()?
    Why does everyone say not to use scanf? What should I use instead?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

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