在一個C程序中,我試圖查詢用戶的日期,該日期應放入結構中的char陣列中(pIndex-> startDate)。如果日期是今天的日期,則用戶只需要按下輸入 - 如果日期是其他日期,用戶將必須鍵入它。用戶查詢日期,如果用戶按下輸入使用當前日期
輸入由fgets完成。我認爲我會檢查用戶輸入是否等於'\ n' - 在這種情況下,我將數組設置爲等於今天的日期。如果不是,則該數組將是用戶鍵入的任何內容。
要查找的日期(一般),我使用:
time_t now = time(NULL);
struct tm *t = localtime(&now);
..和我有一個臨時字符數組的if語句:
char temp_date[11]="2014-01-01"; //initialized with something, guess there is a better solution
我曾嘗試如下:
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");
但是,唯一出現在輸出中的是char temp_date的初始值爲:2014-01-01
這是錯誤的做法嗎?任何人都知道更好的(工作)方式?
這是什麼'(pIndex->的startDate)== temp_date;'? –
也許你想'if(pIndex-> startDate [0] =='\ n')'而不是'if(pIndex-> startDate =='\ n')'?此外,你正在做比較而不是在iharob在他的評論中指出的任務。 –
@CoolGuy pIndex-> startDate [0]的確是解決方案。謝謝。 – Espen