mardi 30 décembre 2014

Query date from user, use today's date if user press enter

In a C-program I am trying to query user for a date which shall be put into a char-array in a structure (pIndex->startDate). If the date is today's date the user should only need to press enter - and if the date is some other, the user will have to type it in.


The input is done by fgets. What I think is that I'll check if the user input is equal '\n' – in that case I set the array equal to today's date. If not, the array will be whatever the user typed.


To find the date (in general), I use:



time_t now = time(NULL);
struct tm *t = localtime(&now);


..and I have a temp char-array for the if-statement:



char temp_date[11]="2014-01-01"; //initialized with something, guess there is a better solution


I have tried the following:



printf("Enter start date (press Enter for todays date) > ");
fgets(pIndex->startDate, MAXDATE, stdin);
if (pIndex->startDate=='\n'){
sprintf (temp_date, "%d-%02d-%02d", t->tm_year+1900, t->tm_mon+1, t->tm_mday);
(pIndex->startDate)==temp_date;
}
/*Check the result*/
printf("CHECK TEMP_DATE: %s\n> ",temp_date);
system("pause");


However, the only thing that appears in the output is the value char temp_date was initialized with: 2014-01-01


Is this a wrong approach? Anyone knows a better (working) way?


Thanks a lot!


-Espen


Aucun commentaire:

Enregistrer un commentaire