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???
Printable View
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???
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
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.
There is no standard way. You have to parse whatever the user enters and store it in some date format.
okie.... i'll do that.... thanks guys
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;
}
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
thanks!!!!
If you want to talk about a struct tm, you have to include the <time.h> header.
Edit: WAY slow.
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);
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.
but when we use a pointer doesn't it actually refer only the address???