1
這裏是我的一點ç片斷生成段故障:Ç - 分段故障
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int checkTiime(char time[]);
int main(int argc, char** argv) {
char time1[6];
char time2[6];
printf("Tempo 1 [hh:mm]: ");
fgets(time1, sizeof time1, stdin);
printf("Tempo 2 [hh:mm]: ");
fgets(time2, sizeof time2, stdin);
printf("Checktime: %d", checkTime(time1));
return (EXIT_SUCCESS);
}
int checkTime(char time[]){
long hours = 0;
long minutes = 0;
if(time[2] == ':') {
if(isdigit(time[3]) && isdigit(time[4]) && isdigit(time[0]) && isdigit(time[1])) {
hours = strtol(time[0], NULL, 10) + strtol(time[1], NULL, 10);
minutes = strtol(time[3], NULL, 10) + strtol(time[4], NULL, 10);
if((hours >= 0 && hours <= 23) && (minutes >= 0 && minutes <= 59)){
return 0;
} else {
return 1;
}
} else {
return 1;
}
} else {
return 1;
}
}
有人可以幫助我。我真的不知道爲什麼給我的問題。我也注意到,當我輸入時,例如,「12:34」它要求我輸入第二個輸入,但是當我編寫「12:34」時,然後使用退格刪除「34」並輸入「34 「它再次寫入第二個printf,但不允許我輸入第二個輸入並退出程序。
個人點評:
我注意到,這是更好地使用gets()
功能,輸入字符串東陽它不計算\n
字符。
感謝您的幫助。無論如何,我剛剛使用'gets()'函數解決了我的問題,並將分解時間分解爲包含小時和分鐘的2個字符串。 – siannone
'gets'是危險的。不要使用它。學習如何使用'fgets'或'scanf'。 – dirkgently