Thread: Data structure for storing dates

  1. #16
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    if i want to get the date from the user.... say, "Enter the date of joining", how to get the input from the user using struct tm???

  2. #17
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    also how should the user enter the date ....... should it be like "ddmmyyyy" or how so that the day, month and year are stored correctly in the specified struct members

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The user should enter the date however you want him to enter the date, and obviously you should tell him via the prompt how you want him to enter the date. Be prepared to do some input validation no matter what, though.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is no standard way. You have to parse whatever the user enters and store it in some date format.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    okie.... i'll do that.... thanks guys

  6. #21
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    can you pls tell me how to rectify this error

    line 8: error: field 'date' has incomplete type

    Code:
    #include<stdio.h>
    
    struct EMP
    {
      int no;
      char name[30];
      char address[50];
      struct tm date;
      int salary;
    }emp[2], *ptr[2];
    
    int main()
    {
      int i,date;
      for(i=0;i<2;i++)
         {
    	ptr[i]=&emp[i];  
         	printf("\nEnter the employee number of employee %d:\t",i+1);
            scanf("%d",&ptr[i]->no);
    	printf("\nEnter the name:\t");
    	scanf("%s",ptr[i]->name);
    	printf("\nEnter the address:\t");
    	scanf("%s",ptr[i]->address);
    	printf("\nEnter thee Date of joining (ddmmyyyy):\t");
    	scanf("%d",&date);
    	printf("\nEnter the salary:\t");
     	scanf("%d",&ptr[i]->salary);
    	ptr[i]->date.tm_mday=date/1000000;
            date=date%1000000;
    	ptr[i]->date.tm_mon=date/10000;
            date=date%10000;
    	ptr[i]->date.tm_year=date;
         }
      for(i=0;i<2;i++)
         {
    	printf("\n Employee %d details\n",i+1);
    	printf("Employee No => %d",ptr[i]->no);
    	printf("\nEmployee Name => %s",ptr[i]->name);
    	printf("\nDate of Joining => %d-%d-%d",ptr[i]->date.tm_mday,ptr[i]->date.tm_mon,ptr[i]->date.tm_year);
         }
      return 0;
    }

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You didn't include time.h, so the compiler doesn't know how struct tm looks like.
    And as for problems with the code:
    >>printf("\nEnter thee Date of joining (ddmmyyyy):\t");
    >>scanf("%d",&date);
    This will absolutely never, ever work. First off, an integer is NOT a date. Secondly, as we told you, there IS no way of storing dates directly in a tm struct. You have to get the input and parse it yourself.
    You also have a huge problem with scanf: http://sourceforge.net/apps/mediawik...tle=Scanf_woes
    Last edited by Elysia; 08-30-2009 at 11:05 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    thanks!!!!

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to talk about a struct tm, you have to include the <time.h> header.

    Edit: WAY slow.

  10. #25
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    why does the compiler ask for an & sign before a pointer????
    Code:
    printf("\nEnter the employee number of employee %d:\t",i+1);
    scanf("%d",&ptr[i]->no);

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not before a pointer, but scanf requires a pointer to the target to write the data, so you need to pass the address of the no member, which is what you're doing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    but when we use a pointer doesn't it actually refer only the address???

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by phoneix_hallows View Post
    but when we use a pointer doesn't it actually refer only the address???
    Yes. However, since "no" is not a pointer, but an int, that is completely irrelevant to the discussion. (The fact that ptr[i] is a pointer is also completely irrelevant to the discussion, since we're not trying to read in a ptr[i], but a no.)

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by phoneix_hallows View Post
    but when we use a pointer doesn't it actually refer only the address???
    But the pointer points to the struct, so to get to the struct, you dereference the pointer and then access the no member, which isn't a pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parent in a binary search tree
    By roaan in forum C Programming
    Replies: 4
    Last Post: 08-26-2009, 07:08 PM
  2. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  3. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 12:42 PM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  5. Dynamic Data Structure -- Which one is better?
    By Yin in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2002, 11:38 PM