inputting the date using C

This is a discussion on inputting the date using C within the C Programming forums, part of the General Programming Boards category; im a relatively new person where C programming is concerned and im doin a bit of experimenting ..but im having ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    5

    Question inputting the date using C

    im a relatively new person where C programming is concerned and im doin a bit of experimenting ..but im having a bit of a problem finding or rather coming up with a code that could be used to input the date in the format day/month/year
    could anyone help me out pls??
    Last edited by jeunefemme; 01-25-2003 at 12:55 PM.

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    to get time of the comp
    Code:
    #include <time.h>
    #include <conio.h>
    #include <stdio.h>
    int main()
    {
    int hour,min,sec;
    struct time t;
    do{
          gettime(&t);
          hour=t.ti_hour;
          min=t.ti_min;
          sec=t.ti_sec;
          printf("%2d:%2d:%d\r",hour,min,sec);
          }
           while(!kbhit());
          return 0;
          }
    if its wrong then the error is left in there especially for you try to find it and correct it, as i dont wont to do the whole thing for you

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    5
    thanx but im not sure if that's what i wanted
    i was looking for something with date not time

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    hmm check the reference the thing i just wrote down was also taken from the manual ( a piece though) so just check the lib reference for time ;-) or do a search because mayb this topic has been here before also dont forget to check the Faq's

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    5
    lib reference ???

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Have both

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
       struct tm *newtime;
       time_t aclock;
       
       time( &aclock );                
       newtime = localtime(&aclock); 
       
       printf( "date and time: %s", asctime(newtime));
       
       return 0;
    }

  7. #7
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Originally posted by jeunefemme
    lib reference ???
    i dont know if its standard but with my compiler Borland compiler 4.52 there's a library refernce =>as the word itself says u can check what a function returns and IMO the lib reference is very handy

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>to input the date in the format day/month/year
    Do you mean you want to get the date from the user (by them typing it in)?


    @ronin
    Don't bother posting poor quality code like that. It contains a lot of non-standard stuff that is totally going to confuse a newbie. Also, void main???!!!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    This reads a date and then does simple checking to see if the date is more or less valid
    Code:
    #include <stdio.h>
    
    int valid_date(int dd, int mm, int yy)
    {
      static days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
      static ldays[] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
    
      int daymax;
      int leap = yy%4 == 0 && yy%100 != 0 || yy%400 == 0;
    
      if (mm < 1 || mm > 12)
      {
        return 0;
      }
    
      daymax = (leap == 1) ? ldays[mm] : days[mm];
    
      if (dd < 1 || dd > daymax)
      {
        return 0;
      }
    
      return 1;
    }
    
    int main(void)
    {
      int dd, mm, yy;
      char buf[20];
    
      if (fgets(buf, 20, stdin) == 0)
      {
        return 1;
      }
    
      if (sscanf(buf, "%d%*c%d%*c%d", &dd, &mm, &yy) != 3)
      {
        return 1;
      }
    
      if (valid_date(dd, mm, yy))
      {
        printf("Day -- %d\nMonth -- %d\nYear -- %d\n", dd, mm, yy);
      }
    
      return 0;
    }
    *Cela*

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    5
    Originally posted by Hammer
    >>to input the date in the format day/month/year
    Do you mean you want to get the date from the user (by them typing it in)?


    i want the user to type in the date and the program should display it in that format



    Cela i tried your code but i dont know if im doing something wrong because after i enter the date nothing is being printed

  11. #11
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>i dont know if im doing something wrong because after i enter the date nothing is being printed
    You didn't enter a valid date then. :-) Remember that it has to be day, then month, then year and the day has to be valid for the month which has to be between 1 and 12. Let's say I want my birthday, I would type
    Code:
    % 7/1/1984
    *Cela*

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    5
    Originally posted by Cela
    >>i dont know if im doing something wrong because after i enter the date nothing is being printed
    You didn't enter a valid date then. :-) Remember that it has to be day, then month, then year and the day has to be valid for the month which has to be between 1 and 12. Let's say I want my birthday, I would type
    Code:
    % 7/1/1984
    ok will try that

  13. #13
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by Hammer
    @ronin
    Don't bother posting poor quality code like that. It contains a lot of non-standard stuff that is totally going to confuse a newbie. Also, void main???!!!
    Done and Done. I didn't write void main though
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by jeunefemme
    Cela i tried your code but i dont know if im doing something wrong because after i enter the date nothing is being printed
    Maybe you should try debugging it... or better still write your own version that you understand. No offence, but there's no point in running other people's code if you don't understand it.

    As you're new, I suggest you start out slow. There's a lot in Cela's code for a newbie to get to grips with, and you've had two other very different examples. Do some research and choose a path that you understand, and post some of your code when you have trouble.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 09:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  4. CDate Class - handle date manipulation simply
    By LuckY in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 09:35 AM

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