Originally Posted by
Sayth
I have updated the program however it is warning that my second else needs a "(" or identifier first.
Like this...
Code:
#include <stdio.h>
int main(void)
{
int hour, min, myhour;
char * postfix;
printf("Print the time in 24hour format: ");
scanf("%d:%d", &hour, &min);
if ((hour < 25 && hour > 0) && (min < 61 && min > 0 ))
{
if (hour > 12)
{
myhour = (hour - 12);
postfix = "PM";
}
else
{
myhour = hour;
postfix = "AM";
}
printf("The time is %.2d:%.2d %s \n", myhour, min, postfix);
}
else
printf("Your input was invalid \n");
return 0;
}
A much simpler way would be like this...
Code:
#include <stdio.h>
int main(void)
{
int hour, min, myhour;
char * postfix;
printf("Print the time in 24hour format: ");
scanf("%d:%d", &hour, &min);
if ((hour > 24 || hour < 0) || (min < 1 || min > 59 ))
{
printf("Your input was invalid \n");
return 0;
}
if (hour > 12)
{
hour -= 12);
postfix = "PM";
}
else
postfix = "AM";
printf("The time is %.2d:%.2d %s \n", hour, min, postfix);
return 0;
}