Im having trouble creating a code on C that after letting the user input a date it determines if the date is in a valid 24 hour format, any help would be greatly appreciated. Here's my code.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>

bool format_correct(char format[]);
int looper;
char hour[] = "11:11";

int main()
while (looper == 1);{

printf("insert time in format HH:MM\n");
scanf("%s", &hour);
  if ((hour < 25) && format_correct(hour)){
    printf("%s is a correct hour",hour);
    looper ++;
    printf("incorrect format, introduce time in format HH:MM \n");
  return 0;

bool format_correct(char format[])
  if (strlen(format) != 5) return false;
  if (!isdigit(format[0])) return false;
  if (!isdigit(format[1])) return false;
  if (format[2] != ":") return false;
  if (!isdigit(format[3])) return false;
  if (!isdigit(format[4])) return false;


1 Answer 1



good = 0;   // 0 evaluates to false, any other number to true
   ask for input
   good = check input
} while (!good)

Wrap that around your existing code and it should repeat as needed.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .