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