I'm trying to validate a day of the month, so that if the user enters a negative value or a value above 31, the program prints an error message and prompts for another day. The code below doesn't work as expected. Instead, it gets caught inside an infinite loop, printing something of the form Enter day and reminder: Invalid Date
, and I can't seem to figure out why. The same technique worked like a charm previously.
while(1) {
if(num_remind == MAX_REMIND) {
printf("-- No Space Left --\n");
break;
}
printf("Enter day and reminder: ");
scanf("%2d", &day);
if(day == 0) {
break;
} else if(day < 0 || day > 31) {
printf("Invalid Date\n");
continue;
}
sprintf(day_str, "%2d", day);
read_line(msg_str, MSG_LENGTH);
for(i = 0; i < num_remind; i++) {
if(strcmp(day_str, reminders[i]) < 0) {
break;
}
}
for(j = num_remind; j < i; j--) {
strcpy(reminders[j], reminders[j - 1]);
}
strcpy(reminders[i], day_str);
strcpy(reminders[i], msg_str);
num_remind++;
}